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

2013/6/7

java.lang.NullPointerException NEED HELP!

tommy95 tommy95

2013/6/7

#
When I start the program as soon as you hit the first ball the game freezes segnalandomi the following error: java.lang.NullPointerException in the method addScore (). Why? Can anyone help me? My goal is to change the background after the timer is set to 30sec ends. The background, however, must change only if the score is greater than or equal to a total defined. I can not make it work. If you need more information just ask! thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void Move(){
        if(Bordo()){ /**rimuove i piatteli vicini ai bordi*/
            getWorld().removeObject(this);
        }
            else
            if(Collisione(Proiettile.class)){ /**rimuove gli oggetti colpiti e il proiettile che l'ha c*/
                Campo x = (Campo) getWorld();
                Actor p = getOneIntersectingObject(Proiettile.class);
                getWorld().removeObject(p);
                getWorld().removeObject(this);  
                addScore();
            }
 
            else{ /**da una determinata velocità ai piattelli*/
                MovePiattello(Greenfoot.getRandomNumber(5)+3);           
                }      
      }
1
2
3
4
5
6
7
public void addScore() 
    if (!getWorld().getObjects(Counter.class).isEmpty()) 
    
        ((Counter) getWorld().getObjects(Counter.class).get(0)).add(1); 
    
}
Gevater_Tod4711 Gevater_Tod4711

2013/6/7

#
In the addScore method the problem probably is that the object is not in the world when you execute the method. In this case getWorld(). would return null and you get a NullPointerException. You can fix it by adding: getWorld() != null && in front of !getWorld().getObjects(... (into the if condition).
tommy95 tommy95

2013/6/7

#
Ok, now the error does not give me more! But there is another problem, the counter is not updated and remains at 0. This is the main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private Counter c = new Counter("Score: ");
    private Timer timer = new Timer();
    private Uomo uomo = new Uomo();
    private Fucile fucile = new Fucile();
    private Piattello p = new Piattello();
    GreenfootImage b = getBackground();
      
     public Campo()
     {   
        super(800, 600, 1);
        b.setColor(Color.GREEN);
        b.fill();
        addObject(timer, 767, 34);
        addObject(c, 47, 34);
        addObject(fucile, 397, 567);
        addObject(uomo, 376, 573);
     }
 
     public void act(){
         Piattelli();
          
          
     }
 
     private void Piattelli(){  /**genera i piattelli all'infinito*/
        if(Greenfoot.getRandomNumber(1000) > 980) {
            addObject(new Piattello(), 775, Greenfoot.getRandomNumber(560)+ 20);
        }
     
      
        }
Gevater_Tod4711 Gevater_Tod4711

2013/6/8

#
In this code you never update the counter. If you want it to count up you have to add something to the counter.
tommy95 tommy95

2013/6/8

#
public void Move(){ if(Bordo()){ /**rimuove i piatteli vicini ai bordi*/ getWorld().removeObject(this); } else if(Collisione(Proiettile.class)){ /**rimuove gli oggetti colpiti e il proiettile che l'ha c*/ Campo x = (Campo) getWorld(); Actor p = getOneIntersectingObject(Proiettile.class); getWorld().removeObject(p); getWorld().removeObject(this); addScore(); <-------------updating counter } else{ /**da una determinata velocità ai piattelli*/ MovePiattello(Greenfoot.getRandomNumber(5)+3); } } I update here the counter
Gevater_Tod4711 Gevater_Tod4711

2013/6/8

#
I need to know more of the code. - the method addScore() - when is the method move called ...
tommy95 tommy95

2013/6/8

#
It all works now. thanks
You need to login to post a reply.