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

2013/5/28

Getting an Error

programmer274 programmer274

2013/5/28

#
Hey I am receiving this error and I am wondering if anyone can tell me what I am doing wrong. It comes up as soon as I start the game. There isn't any code in the if statements yet but that is on purpose. I do not know what i want to do in the if statements yet. Here is the error and my code for my Ball class. java.lang.NullPointerException at Ball.Warning(Ball.java:38) at Ball.act(Ball.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) Here is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { private int uSpeed = -8; private int acceleration = 1; private int dSpeed = 8; /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); Warning(); } public void move() { if(Greenfoot.isKeyDown("up")) { setLocation ( getX(), getY() + uSpeed); } else { setLocation ( getX(), getY() + dSpeed); } } public void Warning() { Actor zone = getOneIntersectingObject(Zone.class); if((zone.getY() - (zone.getImage().getHeight() / 2)) > (getY() + (getImage().getHeight() / 2))) { } if((zone.getY() + (zone.getImage().getHeight() / 2)) < (getY() - (getImage().getHeight() / 2))) { } } }
danpost danpost

2013/5/28

#
It appears you are trying to use 'getY' on 'null'. Since 'null' is not in the world, you will get this error. You should check to make sure 'zone' has an actual object (and not 'null') before using any method that requires the object be in the world. You can insert as the second line in the Warning method the following line:
if (zone == null) return;
programmer274 programmer274

2013/5/28

#
Okay so I have a new question. I have added your line of code to mine and it compiled. However, it does not seem to be reading the code at all. Inside the if statements I set the uSpeed and dSpeed to 0. When I run the game and set an instance of the Zone class next to the ball and go outside of the safe range the code in the if statements does not execute and the game continues to function normally. Why is this happening?
danpost danpost

2013/5/28

#
What code did you put in the 'Warning' method?
programmer274 programmer274

2013/5/29

#
I have since edited the method and set my two movement variables uSpeed and dSpeed to 0 inside both if statements
programmer274 programmer274

2013/5/29

#
I have a feeling why this might not work. Is there any way to just stop the game at that point once one of the if statements is satisfied? I have tried Greenfoot.stop() but that doesn't seem to be working
danpost danpost

2013/5/29

#
There are two things you could try. (1) with the Greenfoot.stop() statement, comment out the rest of the method (and anything else the 'act' method might do if not already in that method), so nothing else does execute; or, (2) add 'System.out.println' statement(s) at that point to see what value are being held by your fields at that point in the execution.
programmer274 programmer274

2013/5/29

#
I think the statements aren't even being read because any code I put in them including your suggestions do not work. Would publishing my game help in any way?
danpost danpost

2013/5/29

#
Insert this at the beginning of the Warning method, then run, observe, and post back informing on results:
System.out.println(getWorld().getObjects(Zone.class).isEmpty() ? "empty" : "not empty");
System..out.println(getOneIntersectingObject(Zone.class) != null ? "intersecting" : "not intersecting");
Greenfoot.stop();
if (true) return;
danpost danpost

2013/5/29

#
Ignore my last post. It is no wonder why nothing happens. First, you are getting an intersecting zone object, if any. Then, if there was one, you are asking if the ball is totally outside, above and below, the zone object. It cannot both be intersecting and not intersecting at the same time!
danpost danpost

2013/5/29

#
I just saw the description of your scenario. If you had explained that here, it would have been much easier. I think this is what you wanted:
public void Warning()
{
    if (getOneIntersectingObject(Zone.class) == null)
    {
        // game over
    }
}
programmer274 programmer274

2013/5/30

#
Thank you for the reply. I shall test out the line as soon as I can and then I'll get back to you.
programmer274 programmer274

2013/5/30

#
Okay I have tested it and it works. Thanks again.
You need to login to post a reply.