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

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
 
T

tdavisjr

To check for null sting

..NET 2.0 String.IsNullOrEmpty(mystring)

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

darrel

.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
 
T

tdavisjr

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.
 
D

darrel

System.Diagnostics.Debug.WriteLine(ds.Tables(0).Rows(0)("myField").ToString())
and see what prints out in the output window.

Nothing. An empty value.

-Darrel
 
D

darrel

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
 
G

Guest

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.
 
D

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top