NullPointerException when using .rotate Method of Motor

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: imaqine, 99jonathan, roger

Killerzwelch
New User
Posts: 11
Joined: Mon May 24, 2010 9:49 am

NullPointerException when using .rotate Method of Motor

Postby Killerzwelch » Mon Jul 25, 2011 6:30 am

Hi,

I once developed a Roboter but as I had lot to do the project was one year paused. Now as my Bachelor Thesis comes nearer and nearer I have to resume with the project to have a good base that I can start with the thesis.
I had a old version of the Lejos (the version of may 2010) but as it is - PCs get reinstalled and yesterday I reinstalled everything.
Now as I start the application the robot is doing one movement and then ends up with a NullPointerException:
Exception in thread "main" java.lang.NullPointerException
at lejos.nxt.remote.RemoteMotor.waitComplete(RemoteMotor.java:190)
at lejos.nxt.remote.RemoteMotor.rotate(RemoteMotor.java:171)
at lejos.nxt.remote.RemoteMotor.rotate(RemoteMotor.java:205)
at hal.MotorHAL.LiftUp(MotorHAL.java:54)
at controller.Maintenance.openLift(Maintenance.java:24)
at controller.Maintenance.Maintain(Maintenance.java:100)
at controller.Controller.main(Controller.java:169)

MotorHAL:

Code: Select all

48   public void LiftUp(int distance)
49   {
50      if(liftInitialized==false)
51      {
52         initializeLift();
53      }
54      lift.rotate(distance);
55   }



Maintenance:

Code: Select all

21   private static void openLift()
22   {
23      System.out.println("Entering Maintenance Mode");
24      motorHAL.LiftUp(3800);
25      while(buttonHAL.MaintainPressed()==false)
26      {
27         Thread.yield();
28      }
29      System.out.println("Leaving Maintenance Mode");
30      motorHAL.LiftDown(3800);
31      System.out.println("Maintenance mode left");
32   }

97   case 3:
98      //Open lift
99      soundHAL.beep();
100      openLift();
101      buttonCount=1;
102      break;


Controller

Code: Select all

165     if(Controller.getButtonHAL().MaintainPressed()==true)
166     {
167          Thread.sleep(500);
168          Maintain.Maintenance();
169     }


The initializeLift just sets the speed and power

is that a known bug or is that my fault? (as I said - the old version has worked)

And a second question: I have had the ColorLightSensor that does not exist anymore. Is it correct that it was replaced by the ColorSensor?


[EDIT:]
I just decompiled the pccomm.jar (am I allowed to do that?) and found following issue (i assume)
Line 32: protected RegulatedMotorListener listener = null;

but the listener is just set in
public void addListener(RegulatedMotorListener listener)
{
this.listener = listener;
}

My assumption is, that the listener is not properly initialized and then crashes here:

Code: Select all

186  public void waitComplete()
187  {
188    while (isMoving())
189      Thread.yield();
190    this.listener.rotationStarted(this, getTachoCount(), false, System.currentTimeMillis());
191  }


Thanks & Kind regards
Dominik

pczekalski
New User
Posts: 7
Joined: Sun Jan 11, 2009 10:26 am
Location: Gliwice, Poland
Contact:

Re: NullPointerException when using .rotate Method of Motor

Postby pczekalski » Mon Jul 25, 2011 9:01 am

Hi there.
I acknowledge - just found the same behavior yesterday.
I found temporary solution, use

Code: Select all

rotateTo(angle,true)
(pay attention that it is non blocking call, however).

Hope that helps at least a little.

P.

P.S. You're right - it was working in 0.8.x releases.
----------------
Ph.D. Eng. Piotr Czekalski
Silesian University of Technology
Gliwice, Poland

Killerzwelch
New User
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Re: NullPointerException when using .rotate Method of Motor

Postby Killerzwelch » Tue Jul 26, 2011 7:41 am

Hi,
I Found a Solution on my own. Just created a Class that is applicable for The listener Object and Set The listender While initiating the Remotemotor.
I'll post The Code tday evening when i'm back from work.
But nevertheless i would apreciate having an official fix for that.

Rgds
Dominik

User avatar
kirkpthompson
leJOS Team Member
Posts: 305
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: NullPointerException when using .rotate Method of Motor

Postby kirkpthompson » Tue Jul 26, 2011 12:57 pm

Greetings All.

I checked the svn repository and this is indeed a bug and was fixed with rev. 4891 on 5/20/11. The 0.9 release was done on 5/16/11 so it does not include the fix. The workaround by Killerzwelch is valid and should be used until the next release (or if you decide to use the current DEV snapshot).

Best,
-K
Leg Godt!

Killerzwelch
New User
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Re: NullPointerException when using .rotate Method of Motor

Postby Killerzwelch » Tue Jul 26, 2011 5:57 pm

Here's what I did:

I created a class that implements the RegulatedMotorListener interface and implemented the methods.


Code: Select all

package nxp;

import lejos.robotics.RegulatedMotor;
import lejos.robotics.RegulatedMotorListener;


public class MotorListener implements RegulatedMotorListener{

   @Override
   public void rotationStarted(RegulatedMotor arg0, int arg1, boolean arg2,
         long arg3) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void rotationStopped(RegulatedMotor arg0, int arg1, boolean arg2,
         long arg3) {
      // TODO Auto-generated method stub
      
   }


}


When I initiate the motor it looks like that

Code: Select all

/**
 * initializes updown motor (y axis)
 */
   private void initializeUpDown() {
      updown.setSpeed(10);
      updown.setPower(25);
      RegulatedMotorListener listener = new MotorListener();
      updown.addListener(listener);
      updownInitialized=true;
   }

It's working fine :)

roger
Moderator
Posts: 381
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: NullPointerException when using .rotate Method of Motor

Postby roger » Wed Jul 27, 2011 6:38 am

Your code has the line

Code: Select all

updown.setPower(25);
which has no effect on the NXTRegulatedMotor. As I understand it, the motor will regulate its power level in order to maintain the set speed.

Killerzwelch
New User
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Re: NullPointerException when using .rotate Method of Motor

Postby Killerzwelch » Wed Jul 27, 2011 6:56 am

Hi,

As Motor Class I used the "RemoteMotor" (using the PC API), but due to that bug it needs an object that implements the interface RegulatedMotorListener. Its needed to assign that object explicitly, otherwise it throws a NullPointerException.

RGds
Dominik

fieranmason
Novice
Posts: 37
Joined: Wed Oct 05, 2011 8:32 pm

Re: NullPointerException when using .rotate Method of Motor

Postby fieranmason » Fri Oct 07, 2011 7:03 pm

I'm not exactly clear on how the workaround which is discussed here is suppose to work, but what I did gather was that if I can get my hands on daily commit of the pccomms jar that that should solve my problems with the null pointer exception. Is this correct? If so where can I download that?

Fieran

User avatar
kirkpthompson
leJOS Team Member
Posts: 305
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: NullPointerException when using .rotate Method of Motor

Postby kirkpthompson » Fri Oct 07, 2011 9:10 pm

Hi Fieran.

The "daily commit" is not necessarily daily. It is the current dev snapshot and if you use the snapshot, you must also flash the NXT firmware because it changes sometimes as well. Be aware that the snapshot is the active dev. state and is constantly moving and may be very different from the version you are working with now. Bottom line, you just can't replace the pccomms.jar and expect it to work.

IMHO, the work-around is probably a better choice for you. What about it do you need help understanding?

Best,
-K
Leg Godt!

fieranmason
Novice
Posts: 37
Joined: Wed Oct 05, 2011 8:32 pm

Re: NullPointerException when using .rotate Method of Motor

Postby fieranmason » Fri Oct 07, 2011 11:10 pm

Thanks for the nudge Kirk,

What my problem with the workaround was was that I didn't spend enough time looking at it. It works fine.

Fieran


Return to “NXJ Software”

Who is online

Users browsing this forum: No registered users and 5 guests