c# - How do I avoid 'Object reference not set to an instance of an object'? -


I keep pressing the same problem over and over again, and I have done it again. I wrote a line of code:

  int laid = db.GetLAByLatLong (address.litidu, address.alogya);   

... and there is no problem with the line in Visual Studio.

But when I run the code, it reports:

The object reference is not set for an example of an object

What am I doing wrong? How does the Visual Studio code feel right, but does it report an error in the runtime? I am doing a lot of this and I will be very happy to understand what I am doing, so I can save it.

You have two objects in your code:

  db < / Code>  

and

  address   

You are referring to both the objects in the code shown. At least one of them is empty.

To avoid this problem, make sure that you have started both the objects before the code is run. You can also add a check like

  if (DB == empty) throw a new exception ("variable DB is empty");    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -