object refrence not set to an instance of an object ??

G

Guest

hello,

i'm having a problem with the following piece of code. i use it to read a
string in an array, then search that string for ";;" symbols, then i read
what comes after that to a variable, witch goes to a text box. but every time
i run the subroutine, i get an "Object reference not set to an instance of an
object." error at the time it reaches "start = line.IndexOf(";;")"

also, wenn i set a breakpoint to debug it, de breakpoint red point's get a
question mark in it(meaning the code isn't avalible?). what's that all about ?

Code:
Dim line, start2 As String
Dim start As Integer
Dim counter As Integer = 0

If comments.Count = 0 Then
Exit Function
End If

While counter < countedcomments
counter += 1
line = comments(counter)
If line.StartsWith(fotos(positionarray)) Then
Exit While
End If
End While

start = line.IndexOf(";;")

If Not start = -1 Then
txtbijschrift.Text = ""
Exit Function
End If
start2 = Mid(line, start)
txtbijschrift.Text = start2

thanks for the help :)
jeroen
 
C

Cor Ligthert

Jeroen,

Your string in this code Is Nothing

The string is a strange thing (it is a value), however it is not set to
Something by default. And therefore your string is Nothing and not empty.

I would use a more modern code by the way and use the indexed for, your code
becomes more readable and goes faster (although the last is probably not
that important here).

I hope this helps,

Cor
 
A

_AnonCoward

:
: hello,
:
: i'm having a problem with the following piece of code. i use it to
: read a string in an array, then search that string for ";;" symbols,
: then i read what comes after that to a variable, witch goes to a text
: box. but every time i run the subroutine, i get an "Object reference
: not set to an instance of an object." error at the time it reaches
: "start = line.IndexOf(";;")"
:
: also, wenn i set a breakpoint to debug it, de breakpoint red point's
: get a question mark in it(meaning the code isn't avalible?). what's
: that all about ?
:
: Code:
: Dim line, start2 As String
: Dim start As Integer
: Dim counter As Integer = 0
:
: If comments.Count = 0 Then
: Exit Function
: End If
:
: While counter < countedcomments
: counter += 1
: line = comments(counter)
: If line.StartsWith(fotos(positionarray)) Then
: Exit While
: End If
: End While
:
: start = line.IndexOf(";;")
:
: If Not start = -1 Then
: txtbijschrift.Text = ""
: Exit Function
: End If
: start2 = Mid(line, start)
: txtbijschrift.Text = start2
:
: thanks for the help :)
: jeroen


This error is indicating that the 'line' object is a null reference
(Nothing). You did not instantiate your line object when you declared it
and, evidently, it is not being assigned inside your while loop. My
guess is this is because 'counter' starts out as greater than
'countedcomments'. In that case, the statement 'line = comments(counter)
is never beening reached. If so, then when you attempt to access the
indexOf function of the string class, the code objects because 'line' is
uninstantiated (it remains a null value).


Try instantiating the 'line' variable when you declare it (Dim line As
String = "") or check to ensure the variable is referencing a live
object prior to the state where your error is occuring. For example:


If line Is Nothing Then
'add whatever logic you feel
'is appropriate here

Else
start = line.IndexOf(";;")

If Not start = -1 Then
txtbijschrift.Text = ""
Exit Function
End If
start2 = Mid(line, start)
txtbijschrift.Text = start2

End If

HTH

Ralf
 

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

Top