'Object reference not set to an instance of an object.' when checking string length?

  • Thread starter Thread starter darrel
  • Start date Start date
D

darrel

I have a function call that passes a value from a dataset field:

myFunction(ds.Tables(0).Rows(0)("myField").ToString)

The function then receives this:

myFunction(ByVal myValue as string)

and then I try to evaluate that value to see if it containes any characters:

if myValue <> "" then

At this point, I get the "Object reference not set to an instance of an
object"

And I don't know why.

My guess is that my call, when the field is null, is not passing an empty
string, but rather some sort of null value?

But I can't check for isnull on a string. So I'm stumped.

Thoughts?

-Darrel
 
To check for null sting

..NET 2.0 String.IsNullOrEmpty(mystring)

..NET 1.1 mystring Is Nothing ( returns true if value is null)
 
.NET 1.1 mystring Is Nothing ( returns true if value is null)

Well, I'm still having the problem.

I modified my check to look like this:

If Not myString Is Nothing Then
If myString <> "" Then
...do stuff...

The Is Nothing evaluates fine, but then it hangs on the second if.

What I want to do is simply check to see if the string actually contains
characters. It's obviously not nothing, as it makes it through the first
'if' but in then hangs on the second.

I've also tried

if mystring.length > 0

But that still gives me the object reference not set...

-Darrel
 
Humm..you can try if NOT myString.Equals(String.Empty) Then

Also, I would use the debugger to actually find out if a value is
coming out of that datatable cell. Do a
System.Diagnostics.Debug.WriteLine(ds.Tables(0).Rows(0)("myField").ToString())
and see what prints out in the output window.
 
Nothing. An empty value.

Here's something odd:

THIS:

Response.Write(myString.Length > 0)

writes to screen 'true'

This, on the otherhand:

if myString.Length > 0 then
or
myString.Length > 0 = True then

gives me the object reference error.

Another odd issue, is that my string actually *isn't* longer than 0. In
fact, it's being passed as an empty string. So that is just confusing me
more.

-Darrel
 
I think that the basis of your problem is that if there is no string to
return from the ToString function it seems to return null so if you initalise
an object using a ToString function and it doesn't actually return a string
then you get an object that has been declared but not initialised. Have you
tried testing to see if the ToString funtion returns a string before calling
your function, and only calling it if it returns a non null value.
 
I think that the basis of your problem is that if there is no string to
return from the ToString function it seems to return null so if you
initalise
an object using a ToString function and it doesn't actually return a
string
then you get an object that has been declared but not initialised. Have
you
tried testing to see if the ToString funtion returns a string before
calling
your function, and only calling it if it returns a non null value.

The basis of my problem is that I'm an idiot. ;o)

After staring at this for another hour, I finally realized that the code
that I thought it was hanging on was not, in fact, where it was hanging.

This whole time it was actually a stringBuilder.append command that was
causing the problem, the issue being that I declared a stringBuilder, but
not a NEW stringbuilder.

Thanks everyone for bearing with me. Apologies for not seeing this in the
first place!

-Darrel
 
Back
Top