PDA

View Full Version : Java Null Pointer headache



DrLacero
17-03-2010, 05:38 AM
Hey hey hey,

First off, full disclosure. This is a side project which broke off from an assignment for Uni, part of the code in question is going to be used in an assignment and therefore I won't be able to post much of it in case it shows up on google and I'm though to have plagiarised it. And of course this means I'll be unable to use any code you directly write, therefore textual help > code. :P

On to the meat and veg. Most of the program uses the becker robots package (the assignment is to program a robot that can escape any maze it's put in). The side project I came up with is a method that scans in a .lozmap file which contains all the information required to construct the maze and place the robot (see pic).

http://img41.imageshack.us/img41/100/mapxo.jpg

It loads up the map fine, however once the map is loaded I get a wonderful NullPointer in main line 14, which is:


A1.makeAMove(myRobot);and in A1, line 190 which is:


if(myRobot.frontIsClear())A1 being the class containing the method which escapes the maze, and makeAMove being the method in said class.

Obviously then the problem is with myRobot. I'm certain I've declared it correctly:


public static Robot myRobot;And it doesn't seem to matter where I put it, the end result is the same. I've tried in A1, the main and in my map reader class (LozTools) to no avail. I've also made sure that the Robot is declared before any use of myRobot.

I'm running out of ideas, +rep to anyone who can come up with something to help :P

Tomm
17-03-2010, 08:18 AM
Have you actually created a new instance of "Robot"? From the code you've just declared that myRobot can be of type Robot but not actually set it to be a instance of Robot hence it is null.

DrLacero
17-03-2010, 02:16 PM
Aye I had. I managed to fix the problem at uni. I blame tiredness, I never thought I'd do something as stupid as this:

Line 112:

public static Robot myRobot;Line 159:

Robot myRobot = new Robot(myCity, variableArray2[3], variableArray2[2], robotDirectionImp);I oooonce was bliiiind but now am found. Ta for the respond bro, +rep :)

Edit: need to spread :P

Want to hide these adverts? Register an account for free!