Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 dimensional array

Status
Not open for further replies.

KirtSawaya

Technical User
Mar 7, 2005
7
US
Hello,

I'm a newbie to VB.NET and I'm having troubles loading an 2 dimensional array from a file. My code looks something like this:
Dim rows As Integer = 4
Dim columns As Integer = 5
Dim myArray(,) As Integer
Dim x As Integer = 0
Dim y As Integer = 0
For x = 0 To rows - 1
For y = 0 To columns - 1
Input(1, myArray(x,y))
Next
Next
But when I try to compile my code I get this error:
An unhandled exception of type 'System.NullReferenceException' occurred in Array.exe

Additional information: Object reference not set to an instance of an object.

If somebody could explain to me what I'm doing wrong I would really appreciate the help.


Thank you,
Kirt
 
It seems that your 'myArray(,)' isn't declared properley - try this

Dim myArray(4,5) as integer
Dim Column As Integer = 5
Dim Row As Integer = 4

For Column = 0 to 4
For Row = 0 to 3
Input(1, myArray(Row, Column))
Next
Next



Paul
 
Thank you Paul for you help. I was trying to use the same format that I learned in C++. But thanks to your help my program is now running.

Kirt
 
Watch the allocation. Arrays in VB are allocated by UpperBound not Length. You ave an extra row and an extra column there.

So change
Dim myArray(4,5) as integer
To
Dim myArray(3,4) as integer



- free online Compare/Diff of snippets
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top