Null object reference error

I want to check if a cell read from excel is empty or not.
I have it saved in a generic variable, (var).
I ran an if condition
if var.ToString.IsNullOrEmpty
but this isnt working as I get an error saying the object inst being referred to anything.
I understand why I’m getting the error, But i cant figure out how to get around it.

Take if loop , put condition that string.isnullorempty(your variable)
If true , it means variable is empty
If false , it mean variable us not empty

Pls mark it solved , if you got your answer .

i tried that. It gives me the error that object isnt referenced.

Change datatype to string , it will work.

I did.

My code goes

read cell() with the output Var
i then go if Var.ToString.IsNullOrEmpty
and thats where I get the error

Use this if condition and remove yours .
If string.isnullorempty(Var)

Wasnt working. I ended up turning the excelfile into a datable and reading the value from the corresponding index, instead of getting it from ReadCell. Then used if Var.ToString = “”

I have given you the solution in the very first reply , its not dependent that you are getting value of string from data table or using read cell.
If you are having variable ,In your case it is “var”.
String.IsNullOrEmpty(var) or string.IsNullOrWhiteSpace(var) will work and you can get its null or not.
Its not dependent on reading method.
Please change your declared solved to another reply.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.