This site requires JavaScript, please enable it in your browser!
Greenfoot back
MNastasi
MNastasi wrote ...

2016/6/15

Not sure why I am getting a nullPointerException?

MNastasi MNastasi

2016/6/15

#
public void act() { isTouchingGoal(); isTouchingShooter(); //setRotation(direction); setLocation(getX() + xSpeed, getY() - ySpeed); life--; if(life == 0) { Greenfoot.delay(10); //getWorld().removeObject(this); //wait for mouseClick if (Greenfoot.mouseClicked(null)) { MouseInfo mouseInfo = Greenfoot.getMouseInfo(); getWorld().addObject(new Ball(), 311, 365); getWorld().addObject(new Ball(), 540, 321); direction = Greenfoot.getRandomNumber(360) + (-135); xSpeed = Greenfoot.getRandomNumber(10) + 1; ySpeed = Greenfoot.getRandomNumber(10) + 1; life = Greenfoot.getRandomNumber(1000) + 10; } } } public void isTouchingGoal() { Actor goal = getOneIntersectingObject(Goal.class); if(goal != null) { count.add(1); Greenfoot.playSound("applause.wav"); if(count.getValue() == 5) { Greenfoot.playSound("celebration.wav"); Greenfoot.stop(); } } }
danpost danpost

2016/6/15

#
MNastasi wrote...
Not sure why I am getting a nullPointerException?
What line is the exception being thrown at? Also, show the error trace printed in the terminal window.
MNastasi MNastasi

2016/6/15

#
danpost wrote...
What line is the exception being thrown at? Also, show the error trace printed in the terminal window.
public void act() { isTouchingGoal(); isTouchingShooter(); //setRotation(direction); setLocation(getX() + xSpeed, getY() - ySpeed); life--; if(life == 0) { Greenfoot.delay(10); //getWorld().removeObject(this); //wait for mouseClick if (Greenfoot.mouseClicked(null)) { MouseInfo mouseInfo = Greenfoot.getMouseInfo(); getWorld().addObject(new Ball(), 311, 365); getWorld().addObject(new Ball(), 540, 321); direction = Greenfoot.getRandomNumber(360) + (-135); xSpeed = Greenfoot.getRandomNumber(10) + 1; ySpeed = Greenfoot.getRandomNumber(10) + 1; life = Greenfoot.getRandomNumber(1000) + 10; } } } public void isTouchingGoal() { Actor goal = getOneIntersectingObject(Goal.class); if(goal != null) { count.add(1); Greenfoot.playSound("applause.wav"); if(count.getValue() == 5) { Greenfoot.playSound("celebration.wav"); Greenfoot.stop(); } } } Run-time error: java.lang.NullPointerException at Ball.isTouchingGoal(Ball.java:53) at Ball.act(Ball.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2016/6/15

#
I am guessing that line 53 in the class is the one where you call 'add' on some 'count' field reference (I believe that this is the only place in the 'isTouchingGoal' method where a 'nullPointerException' could be thrown as you are checking that 'goal' is not null). Apparently 'count' is 'null', in that it has not been initialized to refer to any actual object.
MNastasi MNastasi

2016/6/15

#
Thanks Dan, that was it!
nikhil_sundaram nikhil_sundaram

2016/6/16

#
danpost wrote...
MNastasi wrote...
Not sure why I am getting a nullPointerException?
What line is the exception being thrown at? Also, show the error trace printed in the terminal window.
This is the nullPointerException that I keep getting for the code java.lang.NullPointerException at Ball.isTouchingGoal(Ball.java:53) at Ball.act(Ball.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
You need to login to post a reply.