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

Discussion in 'ASP .Net' started by darrel, Mar 22, 2006.

  1. darrel

    darrel Guest

    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
    darrel, Mar 22, 2006
    #1
    1. Advertising

  2. darrel

    tdavisjr Guest

    To check for null sting

    ..NET 2.0 String.IsNullOrEmpty(mystring)

    ..NET 1.1 mystring Is Nothing ( returns true if value is null)
    tdavisjr, Mar 22, 2006
    #2
    1. Advertising

  3. darrel

    darrel Guest


    > .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
    darrel, Mar 22, 2006
    #3
  4. darrel

    tdavisjr Guest

    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.
    tdavisjr, Mar 22, 2006
    #4
  5. darrel

    darrel Guest


    > 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
    darrel, Mar 22, 2006
    #5
  6. darrel

    darrel Guest

    > 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
    darrel, Mar 22, 2006
    #6
  7. Re: 'Object reference not set to an instance of an object.' when c

    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.

    "darrel" wrote:

    > > 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
    >
    >
    >
    =?Utf-8?B?Y2xpY2tvbg==?=, Mar 22, 2006
    #7
  8. darrel

    darrel Guest

    Re: 'Object reference not set to an instance of an object.' when c

    >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
    darrel, Mar 22, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    664
    Kalyan
    Jul 2, 2004
  2. Suresh Kojhani
    Replies:
    1
    Views:
    2,354
    Anushi
    Jul 29, 2004
  3. Chris Fink
    Replies:
    2
    Views:
    4,056
    David Waz...
    Jul 3, 2003
  4. yysiow
    Replies:
    1
    Views:
    414
    Kevin Spencer
    Jul 12, 2003
  5. Replies:
    1
    Views:
    457
    Mark Rae [MVP]
    Sep 20, 2007
Loading...

Share This Page