Object Reference is not set to an instance of an object

BRS0903

Member
Joined
Jun 12, 2006
Messages
11
Programming Experience
3-5
I am writing a program that I picked up from somebody else and I have a couple of questions. I run the program and when I try to execute it I am hit with an error that tells me in this Sub that the Object Reference is not set to an instance of an object. The tips it gives me are the following...

Use the "new" keyword to create an object instance.
Check to determine if the object is null before calling the method.
Get general help for this exception.

It points to the line that I have bolded below. Somebody help please!

Sub MoveIt(ByVal inpStartLoc As Integer, ByVal outLength As Integer, ByRef buildString As String)
Dim intFrom As Integer = inpStartLoc - 1
Dim intTo As Integer = intFrom + outLength - 1
For k As Integer = intFrom To intTo
buildString &= strInput(k)
Next
End Sub
 
byref & byval

the buildString is being passed as a reference instead of the vb.net default by val. To explain why you can get a nullreference exception I'll first explain the difference between byref and byval.

byref: you get some sort of pointer to an object. Let's you have 2 methods. In method A you set var = 1 then when you call method B from within method A and pass var as a reference method B gets some sort of pointer to var. Now when method B changes the value of var to var = 2 the value of var in method A will also be 2.

byval: you get some sort of pointer to an object. Let's you have 2 methods. In method A you set var = 1 then when you call method B from within method A and pass var as a value method B gets a copy of var. Now when method B changes the value of var to var = 2 the value of var in method A will still be 1.

To get to your problem: verify if the buildString is declared like
VB.NET:
dim buildString as string = ""
in the method that calls the method MoveIt. Be aware buildString can also be an instance variable but I doubt that, should it be you should find something like
VB.NET:
private buildString as string = ""

Now an optimalisation advise. I see you use the buildString in a For next loop. Theoretically that isn't wrong, but for better performance you should have a look at the StringBuilder class that can be found in System.Text.
It's a specialized class to concatenate multiple strings without allocating memory for each concatenation. More information: http://www.vbdotnetheaven.com/Code/Jun2003/2011.asp
 
I tested the code and I think that the variable

strInput(k)

is the reason why I am getting a null reference. How would I change it so that it works properly? How would I declare this? Would you like to see my code?
 
suddenelfilio said:
yes to help you solve your problem I need some more code to work with
It's a lot of code..LOL! What would you need? This variable was declared at the top of the module. MoveIt is just a sub program within that module. I also have a form that refers back to MoveIt and then that is when I get stuck. I also used the strInput variable to pull information from StreamReaders in order to write data to a text file. Well, here goes.
StrInput was declared at the top of the Module.

Directly below is coming from another form -
Do'Process 114
strInput = TextBox1.Text
If strInput IsNothingThenExitDo
strOutput = "11401002496 "
' Social Security Number
'employeeSSN = ""
'employeeSSN = String.Empty
Dim employeeSSN AsNewString("")
MoveIt(1, 9, employeeSSN)
strOutput &= employeeSSN & Space(12)
'Employee Deferral
'strWorkField = ""
Dim strWorkField AsNewString("")
MoveIt(12, 9, strWorkField)
employeeDeferral = Convert.ToInt64(FormatIt(strWorkField))
strWorkField =
"000000000" & ConvertToEBCDIC(employeeDeferral)
strOutput &=
"**A" & strWorkField.Substring(strWorkField.Length - 9, 9)
'Employer Matching
strWorkField = ""
MoveIt(23, 9, strWorkField)
employerMatching = Convert.ToInt64(FormatIt(strWorkField))
strWorkField =
"000000000" & ConvertToEBCDIC(employerMatching)
strWorkField = strWorkField.Substring(strWorkField.Length - 9, 9)

Below is the MoveIt Sub from within the Module
Sub MoveIt(ByVal inpStartLoc AsInteger, ByVal outLength AsInteger, ByRef buildString AsString)
Dim intFrom AsInteger = inpStartLoc - 1
Dim intTo AsInteger = intFrom + outLength - 1
For k AsInteger = intFrom To intTo
strInput =
buildString &= strInput(k)
Next
EndSub




 
suddenelfilio said:
the strInput is it an array of strings or chars?
At the very top where I declared all the variables, I set the value = "".

So I declared

Dim strInput As String = ""

Now it seems as if I no longer have a problem with strInput per say, but now I have a problem with the Index. The following is the problem when I have strInput(k). The following is my code.

Sub MoveIt(ByVal inpStartLoc As Integer, ByVal outLength As Integer, ByRef buildString As String)
Dim intFrom As Integer = inpStartLoc - 1
Dim intTo As Integer = intFrom + outLength - 1
For k As Integer = intFrom To intTo
buildString &= strInput(k)
Next
End Sub

The problem I get is "IndexOutofRangeException was unhandled."

Troubleshooting tips:

Make sure that the maximum index on a list is less than the list size.

Make sure data column names are correct.

Make sure the index is not a negative number.

Get general help for this exception.

I don't know what to do from here. I appreciate your kind gesture of helping. I know you're not in my area, but if you were here I would by you a beer.:D
 
Euhm I'm guessing that the last iteration is to high let's say you've got a string called "teststring" which is 10 characters long so if you want to iterate you will have the indexes from 0 to 9
Let's say that should I try index 10 I will get an IndexOutOfRangeException.
 
Back
Top