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

2019/4/5

I always get a Null point Exception

ordinaryblade ordinaryblade

2019/4/5

#
I want to make a rpg game where an arrow is shot when i click the mouse button. This is my code so far:
Public class Player extends Actor
{
public void shoot()          // Schießt einen Pfeil
        {
        if(Greenfoot.mouseClicked(getWorld()))      // If mouse is touched
        {
            Actor arrow = new Arrow();
            getWorld().addObject(arrow, getX(), getY());    // PArrow is created
            MouseInfo mouse = Greenfoot.getMouseInfo();
                      // turn to mouse
            arrow.turnTowards(mouse.getX(), mouse.getY());
            
           
            
        }
}
Thankyou for help!
danpost danpost

2019/4/5

#
ordinaryblade wrote...
I always get a Null point Exception I want to make a rpg game where an arrow is shot when i click the mouse button. This is my code so far: << Code Omitted >>
Insufficient code given. Please provide the entire class code.
ordinaryblade ordinaryblade

2019/4/6

#
Okay... It is in German due to a School project.
public class Player extends Actor
{
    public int speed = 3;
    private GreenfootImage nachOben = new GreenfootImage("back.png");   // Bilder Attribute
    private GreenfootImage nachUnten = new GreenfootImage("front.png");
    private GreenfootImage nachRechts = new GreenfootImage("right.png");
    private GreenfootImage nachLinks = new GreenfootImage("left.png");
    private int leben;
    public int timer;
    public Player()
    {
        leben = 100;
    }
    public void act() 
    {
        bewegen();
        schiessen();
        verletzen(); 
        timer += 1;
        tod();
    }
    public void bewegen()
    {        
        if(Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w"))       //nachoben
        {
            setImage(nachOben);
            setLocation(getX(), getY() -speed);
        }
        if(Greenfoot.isKeyDown("down")|| Greenfoot.isKeyDown("s"))       //nach unten gehen
        {
            setImage(nachUnten);
            setLocation(getX(), getY() +speed);
        }
        if(Greenfoot.isKeyDown("right")|| Greenfoot.isKeyDown("d"))       //rechtsgehen
        {
            setImage(nachRechts);               //Bild wird gesetzt
            setLocation(getX() + speed, getY());    // Es wird bewegt
        }
        if(Greenfoot.isKeyDown("left")|| Greenfoot.isKeyDown("a"))        // Links gehen
        {
            setImage(nachLinks);
            setLocation(getX() - speed, getY());
            
        }
        
    }
    public void schiessen()          // Schießt einen Pfeil
        {
        if(Greenfoot.mouseClicked(getWorld()))      // Wenn Maus geklickt wird:
        {
            Actor arrow = new Arrow();
            getWorld().addObject(arrow, getX(), getY());    // Pfeil erstellen
            MouseInfo maus = Greenfoot.getMouseInfo();
                      // in Richtung Maus zeigen
            arrow.turnTowards(maus.getX(), maus.getY());
            
            
        }
        if(!Greenfoot.mouseClicked(getWorld()))return;  
    }
    public void verletzen() // Enemy verletzt Spieler
    {
        if(timer == 60)
        {      
            timer = 0;
            if(isTouching(Enemy.class) || isTouching(Ork.class))
            {
                leben = leben - 5;
            
            } 
            
        }
        getLeben();
    }
    public int getLeben()
    {
        return leben;
    }
    public boolean tod() // checkt ob stirbt
    {
        if(leben == 0)
        {
            Greenfoot.stop();
            Messages gameover = new Messages();
            getWorld().addObject(gameover,500,300);
            return true;
        } else return false;
    }
    public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Obstacle.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }
}
Super_Hippo Super_Hippo

2019/4/6

#
The error message tells you in what line the error happened. You should provide those details. Try this:
MouseInfo maus = Greenfoot.getMouseInfo();
if (maus != null)
{
    if (Greenfoot.mousePressed(null))
    {
        Actor arrow = new Arrow();
        getWorld().addObject(arrow, getX(), getY());
        arrow.turnTowards(maus.getX(), maus.getY());
    }
}
Btw, what are you trying to achieve with lines 59 and 73?
danpost danpost

2019/4/6

#
Super_Hippo wrote...
The error message tells you in what line the error happened. You should provide those details. Try this: << Code Omitted >>
Copying/pasting the terminal error trace output would absolutely help. However the provided code fix is no good as dragging the mouse will produce a barrage of bullets. Using mouseClicked is better (unless you add a boolean field to track mouse button down state. Also, it is not needed to check for MouseInfo object being null. Any mouse action detected guarantees it not to be (detecting a click is enough to ensure that a MouseInfo object will be returned. If any change is to be done to the original code, it would be this for line 49:
if (Greenfoot.mouseClicked(null))
as an arrow should probably be shot even if the click is performed on an actor.
Super_Hippo Super_Hippo

2019/4/7

#
danpost wrote...
Also, it is not needed to check for MouseInfo object being null. Any mouse action detected guarantees it not to be (detecting a click is enough to ensure that a MouseInfo object will be returned.
I thought so, too. Was just a bit unsure how else it could create a Nullpointer exception from that code.
danpost danpost

2019/4/7

#
Super_Hippo wrote...
Was just a bit unsure how else it could create a Nullpointer exception from that code.
We will have to wait for error trace to find out.
Super_Hippo Super_Hippo

2019/4/7

#
danpost wrote...
However the provided code fix is no good as dragging the mouse will produce a barrage of bullets.
Btw, this should not happen and I also think that it does not. The API says this:
True if the mouse has been pressed (changed from a non-pressed state to being pressed) on the given object.
danpost danpost

2019/4/7

#
Super_Hippo wrote...
Btw, this should not happen and I also think that it does not. The API says this: << Quote Omitted >>
Come to think of it, I believe you (and the API) are correct. I sincerely apologize and retract my recant.
Super_Hippo Super_Hippo

2019/4/7

#
I tested it a bit. Sometimes it actually happens that getMouseInfo returns null even though you have a mousePressed condition before. Didn't really figure it out why or how exactly it happens, but sometimes it happens right at the beginning. Seems to be a bug, but can't reproduce it in a reliable way yet.
You need to login to post a reply.