Error on Lcd

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Error on Lcd

Postby Eoin » Wed Aug 05, 2009 7:09 pm

Code: Select all
import lejos.navigation.*;
import lejos.nxt.*;


public class DirectionTest {
   public static void main(String [] args) {
     
      Navigator robot = new TachoNavigator(5.6F, 13.0F,Motor.C, Motor.B, false);
      robot.setSpeed(450);
     RobotMover b = new RobotMover();
    b.north();
    
    
   }
   
}



Code: Select all
public class RobotMover
{

Pilot robot;

int NORTH = 0;
int SOUTH = 180;
int WEST  = 270;
int EAST  = 90;
int facing = 0;

public RobotMover()
{
}

/*public int facing()
{
   return NORTH; //or SOUTH EAST WEST...needs a bit of thinking!
}
*/



public void north()
{
   //if facing north : forward ()
      if(facing == NORTH)
      {
         robot.travel(25);
      }
   
   //if facing south : rotate180(); forward ()
      else if(facing ==SOUTH)
      {
         robot.rotate(180);
         robot.travel(25);
      }
 
   //if facing west  : rotate90Anticlockwise(); forward()
      else if(facing ==WEST)
      {
         robot.rotate(90);
         robot.travel(25);
      }
      
   //if facing east  : rotate90Clockwise(); forward()
      else if(facing == EAST)
      {
         robot.rotate(-90);
         robot.travel(25);
      }
}



I am having problems with above snippets to try and get the robot to move in a direction. the code compiles and links to the NXt however when i run it i get the following errors to the Lcd,
i havent seen errors on the LCD so i m stumpted


Java Exception
Class: 8
Method: 22
PC: 2215
Eoin
New User
 
Posts: 4
Joined: Wed Aug 05, 2009 7:03 pm
Location: Ireland

Postby gloomyandy » Wed Aug 05, 2009 7:24 pm

Hi,
To help you understand the meaning of the LCD output take a look at...
http://lejos.sourceforge.net/forum/viewtopic.php?t=1215&highlight=exception

In your case the error is a NullPointerException. This is caused because your RobotMover class has class member robot that is used by the north method, but it does not seem to ever be initialized. You probably need to pass the robot variable from your main method into the RobotMover constructor and use it to initialize the RobotMover version of robot...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4240
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby ChrisB01 » Wed Aug 05, 2009 7:30 pm

This is a null pointer exception, you can decode the class by looking at the faq. I think in this case it dislikes the uninitialised pilot object "robot" in the second code snippet. A null pointer exception occurs if you try to use an object which is null (has no value). Perhaps to fix the problem you could add a pilot object to the constructor:

Code: Select all
RobotMover b = new RobotMover(robot);

Code: Select all
public RobotMover(Pilot newRobot)
{
robot = newRobot;
}


Chris
ChrisB01
Advanced Member
 
Posts: 189
Joined: Sat Mar 15, 2008 12:19 pm
Location: UK

thank You

Postby Eoin » Thu Aug 06, 2009 12:01 pm

Cheers Lads, that sorted it


Eoin
Eoin
New User
 
Posts: 4
Joined: Wed Aug 05, 2009 7:03 pm
Location: Ireland


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff