473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

myFunction(ds.Tables(0).Rows(0)("myField").ToStrin g)

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
Mar 22 '06 #1
7 1676
To check for null sting

..NET 2.0 String.IsNullOrEmpty(mystring)

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

Mar 22 '06 #2
.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
Mar 22 '06 #3
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).Ro ws(0)("myField").ToString())
and see what prints out in the output window.

Mar 22 '06 #4
System.Diagnostics.Debug.WriteLine(ds.Tables(0).Ro ws(0)("myField").ToString())
and see what prints out in the output window.


Nothing. An empty value.

-Darrel
Mar 22 '06 #5
> 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
Mar 22 '06 #6
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

Mar 22 '06 #7
>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
Mar 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
3
by: j.t.w | last post by:
Hi. I'm following along with the learnvisualstudio.net videos. Everything works in VWD but, when I upload my files to the web server, the pages for the 3rd and 4th videos don't work. For both...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.