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

2018/3/7

java.lang.NullPointerException

HMH HMH

2018/3/7

#
Hello people, At first my problem was that my score would always remain zero even when I touched objects that were supposed to change the score. So, I changed a few things and now I always get this error : java.lang.NullPointerException at Spieler.act(Spieler.java:62) ------> This is when it is supposed to addScore at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) java.lang.NullPointerException at Spieler.act(Spieler.java:55) ------->here it should be -1 at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) I am very new to this so please keep it simple and if my code doesn't really make sense that's why :D I would really appreciate help!! Also, I have a few comments/aspects in my code that might seem unnecessary to you...that's because I've already tried different ways to make it work, but nothing really fixed it
 public MyWorld()
    {    
        
        super(800, 600, 1); 
        setBackground("milky-way.jpg");
        
        
        
        
        
        this.addObject(new Gegner(), 200,100);
        this.addObject(new Sammel(),80,90);
        
        
        
        Punktestand Punkt= new Punktestand();
        this.addObject(Punkt,110,60);
        
        Spieler S = new Spieler();
        this.addObject(S, 300,300);
        
    }
   
}

public class Punktestand extends Actor

{
    /**
     * Act - do whatever the Punktestand wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    
    
    int Score=0;
    
    
    GreenfootImage bild = new GreenfootImage(110,60);

    
        public void update()
    
    {
        this.setImage (bild);
        //this.S=Spiel;
        this.getImage().clear();
        this.getImage().setColor(Color.RED);
        this.getImage().setFont(new Font("Verdana",20));
        this.getImage().drawRect(0,0,bild.getWidth()-1,bild.getHeight()-1);
       
        this.getImage().drawString("Punkte:" + Score, 12,35);
        

    }   
    
    
    public void addScore()
    {
        
        
    Score++;  
    
    
        
    }
    public void subtractScore()
    {
        Score--;
        
      
    
    }
    
    public Punktestand ()
     {
         update();
     }
        
    public void act()
    {
           this.getImage().clear();
            update();

    }
}  
    
public class Spieler  extends Actor
{
  
  
    /**
     * Act - do whatever the Spieler wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
  // int Score = 0;
 public Punktestand Punkt;
 
    
public Punktestand getPunktestand()
{
    
return Punkt;

}


    public void act() 
    

    {
       
      
        
       if(Greenfoot.isKeyDown("right")){
        setRotation (0);
        this.move (5); }
    
    if(Greenfoot.isKeyDown("left")){
        setRotation (180);
        this.move (5);}
    if (Greenfoot.isKeyDown("up")){
        setRotation (270);
        this.move (5); }
   
    if (Greenfoot.isKeyDown ("down")){
        setRotation (90);
        this.move (5); }
    
        
       if(isTouching (Gegner.class))
        { 
          this.Punkt.subtractScore();
            
          Greenfoot.playSound("gegner.mp3");
           
        }
       if(isTouching(Sammel.class))
       {  
        this.Punkt.addScore();
         
      
            
         Greenfoot.playSound("sammel.wav");
       }
    
    
    
    
    }
   
    
    }
Super_Hippo Super_Hippo

2018/3/7

#
public Punktestand Punkt;
This is a variable of type Punktestand which is not assigned any Punktestand object, so it is null. (It is not the Punktestand object you may add to your world from somewhere else.)
HMH HMH

2018/3/7

#
So what should I do to fix that?
HMH HMH

2018/3/7

#
When I change that to Punktestand Punkt= new Punktestand(); I don't get the error anymore, but the score still stays 0 when it should actually go down or up. How could I fix that? EDIT I changed the int to public static int and now it kind of works :)
Super_Hippo Super_Hippo

2018/3/7

#
Assigning a new Punktestand object to the Punkt variable still doesn't make it the one in the world, it is a new object as the world new tells you. There are a lot of ways to do it: - get a reference at runtime (only working if there is always one Punktestand object in the world:
(Punktestand)(getWorld().getObjects(Punktestand.class).get(0)).addScore();
- pass a reference to the Spieler object upon creation
public Spieler(Punktestand punkte)
{
    Punkte = punkte;
}

//and in the world when creating it
Punktestand p = new Punktestand();
addObject(p,110,60);
addObject(new Spieler(p),300,300);
- save the reference in the world and access it from the world
private Punktestand punkte = new Punktestand();

//...

addObject(punkte, 110, 60);

//...

public void addScore()
{
    punkte.addScore();
}

//and then, when the Spieler needs to increase the score
((MyWorld)getWorld()).addScore();
- save the number of points in your world and use a dummy class which the world controls (setting the image directly). Using a 'public static int' (probably without knowing what it means) only because you don't know how to access a variable from a different class is not a good idea.
You need to login to post a reply.