[Solved]TorqueXBook XNATetris, problem with book
by GxG6(Mark-Anthony) · in Torque X 2D · 05/02/2010 (2:11 pm) · 6 replies
ok so i bought the book everything was going smoothly till i got to the
Tetris Game on chapter 7.
i followed the steps completely and even double checked my work.
but when I run the program I get a
"NullReferenceException" Object reference not set to an instance of an object." error.
and it points to this line of code:
this is my code i entered:
so i went to the TorqueXbook's website and downloaded the source and compared the files but the thing is
the code mentioned above is written the same way but it runs!
what can possibly be wrong?
Tetris Game on chapter 7.
i followed the steps completely and even double checked my work.
but when I run the program I get a
"NullReferenceException" Object reference not set to an instance of an object." error.
and it points to this line of code:
if(tileLayer.GetTileByGridCoords(x, y) !=null)
this is my code i entered:
private bool isValidMove(int deltaX, int deltaY, T2DTileLayer tileLayer)
{
int newX = _currentX + deltaX;
int newY = _currentY + deltaY;
for(int x = 0; x < 4; x++)
{
for(int y =3; y >=0; y--)
{
if(tileLayer.GetTileByGridCoords(x, y) !=null)
{
//check for collisions with walls
if(newX + x <=0 || newX + x >= _wellTileLayer.MapSize.X-1)
return false;
//check for collisions with other blocks
T2DTileObject tile = _wellTileLayer.GetTileByGridCoords(newX + x, newY + y);
if(tile != null)
{
if (tile.TileTypeName == null)
return false;
if(tile.TileTypeName.CompareTo("fixed") == 0)
return false;
}
}
}
}
return true;so i went to the TorqueXbook's website and downloaded the source and compared the files but the thing is
the code mentioned above is written the same way but it runs!
what can possibly be wrong?
About the author
Current Graphic Designer, Musician, Pawnbroker,Game Developer.
#2
That might seem to be a common sense statement but if you do this:
and someReference is null, you will get the null reference exception (NRE).
In short, look to the left of the dot (.) and that's your problem.
In the above code, tileLayer is null. Since tileLayer is an argument passed into isValidMove(), then you need to go to the place in the code where you are CALLING isValidMove() and check to see if there are any conditions where the value of the 3rd argument is null.
In other words, the problem is most likely not in the above code. It's in the code that CALLS the above code.
You can avoid the NRE from occurring by adding this to line 11:
But that might just mask a larger problem. The NRE is usually indicative of a problem. While the above code will keep your software from crashing, it still leaves the question: "Why is my software crashing?"
Clear as mud? ;)
Hope this helps. Good luck!
--RB
05/03/2010 (12:49 pm)
In general, when you see a Null Reference Exception it's because you are accessing a property or a method of a reference that has null as its value.That might seem to be a common sense statement but if you do this:
someReference.someProperty or someReference.someMethod()
and someReference is null, you will get the null reference exception (NRE).
In short, look to the left of the dot (.) and that's your problem.
In the above code, tileLayer is null. Since tileLayer is an argument passed into isValidMove(), then you need to go to the place in the code where you are CALLING isValidMove() and check to see if there are any conditions where the value of the 3rd argument is null.
In other words, the problem is most likely not in the above code. It's in the code that CALLS the above code.
You can avoid the NRE from occurring by adding this to line 11:
if (tileLayer == null)
{
return false;
}But that might just mask a larger problem. The NRE is usually indicative of a problem. While the above code will keep your software from crashing, it still leaves the question: "Why is my software crashing?"
Clear as mud? ;)
Hope this helps. Good luck!
--RB
#3
ugh simple mistakes thanks for helping me look at it over again.
05/03/2010 (5:02 pm)
thank you guys very much! you both were correct. it seems i have to limit the amount of time i spend while programming. i discovered that one of my move() code contained a wrong variable i had it set to positive instead of negative and on top of that i had the code point to different case names. that is defined CASE..set code to look up "case"ugh simple mistakes thanks for helping me look at it over again.
#4
Glad it helped man. Sometimes it's tough to tell which of the numerous reference variables in a stretch of code might be tossing a NRE...but in this case there was only one on that particular line. ;)
Keep up with John's book. It's a really effective resource for learning TX.
Good luck!
--RB
05/04/2010 (4:38 am)
@Mark-AnthonyGlad it helped man. Sometimes it's tough to tell which of the numerous reference variables in a stretch of code might be tossing a NRE...but in this case there was only one on that particular line. ;)
Keep up with John's book. It's a really effective resource for learning TX.
Good luck!
--RB
#5
05/04/2010 (3:36 pm)
yes it is i'm learning alot.
#6
05/05/2010 (7:05 pm)
is there an updated version of the TorqueX Book? the one i got from the book store doesn't translate to the current version of Torque X it seems. even with the code thats downloaded from TorqueXBook.com its still outdated. example The Tutorial about setting a camera doesn't work at all in the current version and in the code downloaded from the site its still wrong. I managed to get around it by looking at the camera code set up in the platformer starter kit i bought. i'm sure there are more people who encountered this.
Torque Owner Scott Cameron
normally when I get a Null Reference Exception on a line that references a Torque Object Its cause I didn't do an null checking on a TorqueObjectDatabase.Instance.FindObject call.
I would check the level data and make sure things are named correctly.
Also the names are case sensitive if I recall correctly.