Object reference not set to an instance of an object

470 views Asked by At

Hello am calling from a class to test my product however i keep getting this error: "Object reference not set to an instance of an object."

Testing:

global::OfficeSystem.Employees employees = new OfficeSystem.Employees();

employees.Delete(Convert.ToInt64("17")); <--- Primary Key
employees.GetEmployeeByOfficeID(Convert.ToInt64("6"));

What is the problem here?

2

There are 2 answers

1
Jonathan Wood On

This simply means you are using an object that is null. Accessing a member on null is never valid.

I'm not sure which line is causing the error. Is it the one that says "Primary Key"? If so, could employees be null? It should be extremely trivial to find out using the debugger.

If needed, check for null before using the object to prevent this error.

1
Chris On

It sounds like for what ever reason you are not instantiating your object. I suggest run you code in debug and add a watch on employees to make sure its not zero. Hope that helps.