# [.net] Whats wrong with my code?

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, could someone pls tell me what is wrong with my code? what i'm trying to do is to read lines from a pre-edited text file and store them into an string Array - gameMapArray, then send off each line to LoadLine method and extract each char out of the string to do the switch statement check. public void InitializeTiles() { gameBitmapFile = new StreamReader(@"..\..\bitmapfield.txt"); mapSize = Convert.ToInt32(gameBitmapFile.ReadLine()); gameMapArray = new string[mapSize]; for (int i = 1; i < mapSize; i++) { gameMapArray[mapSize - i] = gameBitmapFile.ReadLine(); } gameBitmapFile.Close(); for (int i = 0; i < gameMapArray.Length; i++) { LoadLine( gameMapArray); } } public void LoadLine(string lines) { for (int i = 0; i < 12 ; i++) { switch ( lines) <<------ error msg on this line { case '1': tiles.mapRect = tiles.tileRect[0, 1]; break; case '2': tiles.mapRect = tiles.tileRect[0, 2]; break; . . default: break; } } }

##### Share on other sites
What exactly is the error message? Array index out of bounds or similar?

##### Share on other sites
Quote:
 public void LoadLine(string lines){for (int i = 0; i < 12 ; i++){switch ( lines) <<------ error msg on this line

Looks like the variable "lines" holds a string value and you are filling it with an integer (int i) as an array.

##### Share on other sites
Quote:
Original post by IronGryphon
Quote:
 public void LoadLine(string lines){for (int i = 0; i < 12 ; i++){switch ( lines) <<------ error msg on this line

Looks like the variable "lines" holds a string value and you are filling it with an integer (int i)
You can access the individual characters from a string like that in C#/.NET, if I remember correctly...

##### Share on other sites
The error msg it generated: NullReferenceException, use "new" keyword to create the instance.

##### Share on other sites
for (int i = 0; i < 12 ; i++){    switch ( lines) <<------ error msg on this line{

Do you need to verify that i actualy exists in the string?

##### Share on other sites
A couple of things that would help us.

2. What line in the data file does it fail on?

Just a guess off the top of my head... are you sure there are 12 characters on each line of that file? I thought you would have got a ArrayOutOfBoundsException (or whatever its called) for that though...

##### Share on other sites
I was all set to give a smartarse bounds-checking answer, but a NullReferenceException? That means the string is null, not just too short. The only explanation for that is that you tried to read lines that weren't there in your file (ReadLine returns null if you are at the end of the file). In turn, that means that your file didn't contain as many lines as you thought it did, so check the number you typed on its first line is correct.

As an aside, I'd think a better way to do this would be
gameBitmapFile = new StreamReader(@"..\..\bitmapfield.txt");gameMapArray = gameBitmapFile.ReadToEnd().Split('\n');gameBitmapFile.Close();

... removing the need for the length to be specified at all. Since you're loading the whole array anyway I very much doubt your file is big enough to cause any memory headaches.

##### Share on other sites
well, the string is not null. instead of doing: switch(lines), i tried to print the lines string to the screen and it showed up correctly, exactly as whats in the txt file. i'm really lost.

##### Share on other sites
Quote:
 for (int i = 0; i < 12 ; i++) { switch ( lines) <<------ error msg on this line

There aren't 12 lines.
Or maybe you want from 0 to 11 (which is 12 iterations)
In any event, here is a better construct that will work for any number of lines passed in.

for (int i = 0; i < lines.Length; i++)
{
switch ( lines)
...

Also, when you fill the array, you might want to start at zero instead of 1

for (int i = 1; i < mapSize; i++)
{
}

##### Share on other sites

Also, when you fill the array, you might want to start at zero instead of 1

for (int i = 1; i <

##### Share on other sites
ok, that went wrong...

In your loop filling gameMapArray, count upto and include mapSize.
so not lower than, but lower or equal.

Right now you will not fill gameMapArray[0], so that could generate a null reference to a string.