Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Some one to help me with this error it says "Object reference not set to an instance of an object." and the line with this code "Page.Header.Controls.Add(javascriptRef); " is red.

Down below i post you all code...

LiteralControl javascriptRef = new LiteralControl("<script type='text/javascript' src='~/Dodatoci za Master/scripts/colorBoxWidth.js'></script>");
Page.Header.Controls.Add(javascriptRef);
Posted

1 solution

Some of the reference-type instances is null while you assume it is not null. This situation is created not in the code you show. The exception is thrown when you try to dereference such null member/variable.

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 
Comments
thekoko89 6-Mar-13 19:10pm    
I did that but there is still error on the line Page.Header.Controls.Add(javascriptRef);..

this code works perfectly on other masterpage :S
Sergey Alexandrovich Kryukov 6-Mar-13 19:21pm    
What error? Did't I explain everything? Use the debugger, check up all objects Page, Page.Header, javaScriptRef... If at least one is null, take appropriate measures...
Or use EventLog to log all this information...
—SA
thekoko89 6-Mar-13 19:35pm    
The debagger was good choice there was some mistake in references... tnx a lot
Sergey Alexandrovich Kryukov 6-Mar-13 19:46pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900