Jump to content
  • Advertisement
Sign in to follow this  
p997

[.net] Whats wrong with my code?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
A couple of things that would help us.

1. Could you post the data file you are loading
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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++)
{
gameMapArray[mapSize - i] = gameBitmapFile.ReadLine();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!