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

2016/4/19

I am having a problem with java.lang.NullPointerException

itavares itavares

2016/4/19

#
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
32
public void act()
    {
        setRotation(direction);
        move(speed);
        touchingTheWall();
        Actor disk1 = getOneIntersectingObject(Player2.class);
       if(disk1 != null)
        {
            World myWorld = getWorld();
            Level1 level1 = (Level1)myWorld;
            Counter counter = level1.getCounter();
            counter.addScore();
            myWorld.removeObject(this);
              
 
        }
         
    }   
    
    public void touchingTheWall()
    {
        Actor contactwithwall = getOneIntersectingObject(PlasmaWalls.class );
        Actor contactwithBoundary = getOneIntersectingObject(BoundaryWalls.class );
             
        if( contactwithwall != null || contactwithBoundary != null )
            {
                getWorld().removeObject(this);
                 
            }
        }
         
        }
its a disk that if it touches the boundary , a wall or the other player it disappears. But it only add a score when it hits the other player , and I can't figure out how to put everything together without getting an error.
danpost danpost

2016/4/19

#
The problem is that you have two sets of code that could remove the disk from the world. If the first one executed removes the disk, then the second check will fail because the disk is no longer in the world. You can avoid the problem by exiting the act method before the execution of the second check if the disk was removed from the world;
1
2
// insert at line 6 ( after 'touchingTheWall();' )
if (getWorld() == null) return;
This asks 'is the actor not in any world?' and execution immediately exits the method if so.
itavares itavares

2016/4/19

#
It worked ! Awesome , thank you so much for the help danpost ! :)
You need to login to post a reply.