By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
"jeroen" <je****@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
:
: 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
Nov 21 '05 #3

P: n/a
thanks, i rewrote it a little. and it works now :)
"_AnonCoward" wrote:
"jeroen" <je****@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
:
: 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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.