I have a Asp.net-C# Web app, with 4-5 associated projects all written in c#. I'm using .net 3.5.
My problem is, when I try running my application from the solution, it shows an error:
"Error 17 Object reference not set to an instance of an object"
When I build/rebuild the solution, it will be gone. When I click the error it takes me no where. And another interesting thing is, there is no location specified for the error.
Please help.
The error means that some object is null, and you try to call a method or access a property on it.
For example this code will give the same error message:
string myString = null;
int length = myString.Length; //gives error since myString is null
If you can't find out where, and for some reason don't have any stack trace, try adding a try/catch block around the code you think is suspicious and see if the error is there or not.
Another solution is to just add a breakpoint, and go through your code line by line untill it explodes.
Without more details, that is the best advice I can give you.
Related
I know similar questions have been asked a million times. I've read them and I still cannot get to the bottom of this. I'm using the Novell.Directory.Ldap c# library in my code (Winforms, c#). It works very similar to the microsoft ldap libraries but it can also access the attributes specific to Novell eDirectory.
using Novell.Direcotry.Ldap; is in there. I also added the reference and pointed it to the Novell.Directory.Ldap.dll file.
string LDAPServerIP = "12.34.56.78";
string serverUserName = "cn=Rrunner,o=acme";
string serverPassword = "#nvi1";
LdapConnection ldapConn = new LdapConnection();
MessageBox.Show(ldapConn.ToString());
ldapConn.SecureSocketLayer = true;
ldapConn.Connect(LDAPServerIP, 636);
ldapConn.Bind(serverUserName, serverPassword);
I get a NullReferenceException when it gets to the ldapConn.Connect() method.
Stepping through in the debugger is can see the ldapConn object, and it is a Novell.Directory.Ldap.LdapConnection object and seems to have sensible values assigned to it. For example, it does get SSL set to true, and at the point of the exception it even has already had the Host(string) variable set to the LDAPServerIP, and the Port(int) set to 636 just as the ldapConn.Connect() ordered. Given this information, I put a try/catch on just that statement to get past it since it is putting the attribute in anyways. It then will also get the exception on the Bind() method, and bind did put the appropriate values (userDN and password) into the object as well.
That section of code I copied directly from another program I use/write and it works perfectly fine in that so I don't know why it shouldn't work in this program. The only difference is that this is a differnt project in Visual Studio.
Given that, I've tried going through the things again that could be different because its a different project:
I've tried verifying that the reference to the Novell supplied is in the project. I'm fairly certain that part must be working because the object in memory is of the correct type and has a bunch of attributes that my program doesn't tell it about so they had to come from that dll file.
The SSL cert exists on my computer in a spot where .net can find it as this is the same computer that I develop the program that does connect to LDAP successfully.
What am I misssing? It seems like it has to be something really simple.
I found it finally. It was something really simple, and something that had to be set due to being a different project. I needed to add Mono.Security.dll and the reference to it.
I am implementing a error logger for a web shop and just logging a NullReferenceException in a specific class is only useful to a certain level. I am not really interested in how to prevent the exception, as I am aware of that, but sometimes it still happens thus the error logger.
Then the question is: How do I find the source of a System.NullReferenceException inside all the exception information.
Make sure you log the full stack trace. Assuming you've got debug information turned on (no reason not to for a web app...) you should be able to get to the line which caused the problem.
Of course, that won't always give you all the information you need, if you've got:
if (foo.Bar.Baz && person.Address.Road.Length)
in a single line... but it's the best starting point you'll get.
Additionally, adding argument validation to methods can make it a lot simpler to pin down what's wrong. Personally I'm a fan of helper methods for this. For example, in Noda Time we have Preconditions, so I can just call:
Preconditions.CheckNotNull(foo, "foo");
(which also returns the value of foo, which is handy in constructors which are copying arguments into fields).
The earlier you can detect the unexpectedly-null reference, the better.
If I understand the question correctly, in Visual Studio, go to Debug > Exceptions, and check all options to throw exceptions. This will allow you to see everything that is being thrown while debugging. You can possibly use the contents of InnerException to determine what the root location of the error is being caused.
While attempting to debug an application, I keep noticing that two of my arrays and one of my lists seems to be mysteriously... Not there. The error given for that (upon pausing the application and looking through my compiler's variable list) is "A class is not loaded HRESULT: 0x80131303".
After googling, I found out that that particular HRESULT is named "CORDBG_E_CLASS_NOT_LOADED", however I found nothing about it's possible cause, or how to solve it.
I would normally paste the relevant code here, but from what I can find, this error happens directly at the declaration of the effected arrays and list.
Can anyone here help?
You may be loading a class implicitly at startup, which causes an error because not everything is initialized yet. Make sure you are not accessing anything in an unloaded class that could cause this.
So I got the dreaded "yellow screen of death"(? that's what the .NET guys called it) with the error message:
[NullReferenceException: Object reference not set to an instance of an object.]
OK, that's fine, I can understand that, but the error references a line of code which reads:
<namespace1>.<namespace2>.XMLDohickey responseXML =
new <namespace1>.<namespace2>.XMLDohickey();
(names obscured to protect the innocent (: ).
I can easily see how the line after, Session[<value>].ToString();, could cause this error, but I don't understand how the error could be caused by the line it claims to be caused by.
So, is it that C# is telling me the wrong line number, or can a namespace actually be null?
As a side note -- this seems to work fine locally, on my company's DEV and QA servers, but it seems like it failed on our client's QA server...
EDIT
So... here's the deal.
Apparently, when .NET crashes, sometimes it returns the last successful line called instead of the line which actually held the error. In this case, the Session[<value>] was null (Why? No idea. that "Should never happen").
A namespace can never be null, it will never generate any runtime errors of any kind (in the way you are describing it). So your null reference is probably in Session[key].ToString(), or the constructor of XMLDoHickey. I would consider checking if the value in the session state exists before calling a method on it.
Are you sure the exception doesn't come from inside the XMLDohickey constructor?
You need to force a recompilation and probably also clear the temporary internet files.
THere are often mismatches in the line numbers and the actual instruction causing the exception when source and binaries get out of sync and this must be the case here since namespaces surely dont cause null reference exceptions :)
I am creating a microsoft WPF project and ran into the following hindrance:
Null reference was unhandled Object reference not set to an instance of an object
My VS 2010 points to the following line of code:
string connectionString = ConfigurationManager.ConnectionStrings["Lab06Wpf.Properties.Settings.AppConnectionString"].ConnectionString;
Not sure why this is happening, I have a similar project where I have a similar connection string and it runs itself just fine.
The ConnectionStrings argument Lab06Wpf.Properties.Settings.AppConnectionString is valid and the connection has been tested.
Any leads anyone?
ConfigurationManager.ConnectionStrings["Lab06Wpf.Properties.Settings.AppConnectionString"]
That is returning null and your are calling ConnectionString on it. You need to find out why it is returning null. Use your debugger to see what ConnectionStrings contains and why your call is failing.