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

2021/4/27

java.lang.NullPointerException error

BenjiFoxy BenjiFoxy

2021/4/27

#
Hi, I'm new to programming and I am wanting to create a simple game. It was working fine until I added in a second counter now after the first frame gives me this error in red;
java.lang.NullPointerException at Tardis.tryToEatB(Tardis.java:81) at Tardis.act(Tardis.java:30
I have been trying to find a solution but have been unable to find one could anyone help please? This code is from the main character that the user controls and is where it seems to be where the error is occurring
import greenfoot.*;

import javax.swing.JOptionPane;

/**
 * This is a turtle in a first, simple video game. It can be controlled 
 * with the cursor keys and likes to eat ArtronEnergy.
 */
public class Tardis extends Characters
{
    private Counter counter; // assigning a FIELD
    
    public Tardis(Counter pointCounter ) // CONSTRUCTOR GETS COUNTER REFERECE
    {
       counter = pointCounter; 
    }
    
    private CounterB counterB; // assigning a FIELD
    
    public Tardis(CounterB pointCounterB ) // CONSTRUCTOR GETS COUNTER REFERECE
    {
       counterB = pointCounterB; 
    }
    
    public void act()
    {
        move(4);
        checkKeys();
        tryToEat();
        tryToEatB();
    }
    
    /**
     * Check whether the control keys are being pressed, and turn
     * if they are.
     */
    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-5);
            
        }
        
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(5);
        }
    }
    
    /**
     * Check whether we can see Sea ArtronEnergy. If we can, eat it.
     * WE NEED A REFERENCE TO THE COUNTER VIA WORLD OBJECT
     */
    public void tryToEat()
    {
        if (canSee(ArtronEnergy.class) )
        {
            eat(ArtronEnergy.class);
            counter.add(100);
           
        }
        
        if (counter.getValue() >= 100) {
                Greenfoot.setWorld(new MarsWorld());
               
        }

    }
    
    public void tryToEatB()
    {

         if (canSee(ArtronEnergy.class) )
        {
            eat(ArtronEnergy.class);
            counterB.add(100);
           
        }
        
         if (counterB.getValue() >= 100) {
                JOptionPane.showMessageDialog(null,"Victory is ours!", "You Won!",JOptionPane.INFORMATION_MESSAGE);
        Greenfoot.stop();
               
        }

    }
}    
If you require any more code please feel free to ask. Thanks in advance.
Roshan123 Roshan123

2021/4/27

#
Replace it in line 81
if (counterB!=null && counterB.getValue() >= 100)
It may work...if not then wait for other's to get some help
BenjiFoxy BenjiFoxy

2021/4/27

#
Roshan123 wrote...
Replace it in line 81
if (counterB!=null && counterB.getValue() >= 100)
It may work...if not then wait for other's to get some help
That stopped the error message but the game wouldn't run all together, after 1 frame it still stopped then when it moved to the next world it gave me an error on line 64 & 29
Roshan123 Roshan123

2021/4/27

#
BenjiFoxy wrote...
That stopped the error message but the game wouldn't run all together, after 1 frame it still stopped then when it moved to the next world it gave me an error on line 64 & 29
I think u haven't mentioned what tpye of error is given in line 64 If its nullPointerException then paste it in only line 64
 if (counter!=null && counter.getValue() >= 100) 
BenjiFoxy BenjiFoxy

2021/4/27

#
Roshan123 wrote...
BenjiFoxy wrote...
That stopped the error message but the game wouldn't run all together, after 1 frame it still stopped then when it moved to the next world it gave me an error on line 64 & 29
I think u haven't mentioned what tpye of error is given in line 64 If its nullPointerException then paste it in only line 64
 if (counter!=null && counter.getValue() >= 100) 
Yeah sorry it was nullPointerException. That has worked but now giving another nullPointerException error this time on 60&29 after the counter starts ticking up on the second world Code after amendments have been made;
import greenfoot.*;

import javax.swing.JOptionPane;

/**
 * This is a turtle in a first, simple video game. It can be controlled 
 * with the cursor keys and likes to eat ArtronEnergy.
 */
public class Tardis extends Characters
{
    private Counter counter; // assigning a FIELD
    
    public Tardis(Counter pointCounter ) // CONSTRUCTOR GETS COUNTER REFERECE
    {
       counter = pointCounter; 
    }
    
    private CounterB counterB; // assigning a FIELD
    
    public Tardis(CounterB pointCounterB ) // CONSTRUCTOR GETS COUNTER REFERECE
    {
       counterB = pointCounterB; 
    }
    
    public void act()
    {
        move(4);
        checkKeys();
        tryToEat();
        tryToEatB();
    }
    
    /**
     * Check whether the control keys are being pressed, and turn
     * if they are.
     */
    public void checkKeys()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-5);
            
        }
        
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(5);
        }
    }
    
    /**
     * Check whether we can see Sea ArtronEnergy. If we can, eat it.
     * WE NEED A REFERENCE TO THE COUNTER VIA WORLD OBJECT
     */
    public void tryToEat()
    {
        if (canSee(ArtronEnergy.class) )
        {
            eat(ArtronEnergy.class);
            counter.add(100);
           
        }
    

        
        if (counter!=null && counter.getValue() >= 100) {
                Greenfoot.setWorld(new MarsWorld());
               
        }

    }
    
    public void tryToEatB()
    {

         if (canSee(ArtronEnergy.class) )
        {
            eat(ArtronEnergy.class);
            counterB.add(100);
           
        }
        
        if (counterB!=null && counterB.getValue() >= 100)
        {        JOptionPane.showMessageDialog(null,"Victory is ours!", "You Won!",JOptionPane.INFORMATION_MESSAGE);
                Greenfoot.stop();
               
        }

    }
}    
Risen Risen

2021/4/27

#
I might be wrong, but you are using tryToEat() and tryToEatB() for different worlds, right?
BenjiFoxy BenjiFoxy

2021/4/27

#
Risen wrote...
I might be wrong, but you are using tryToEat() and tryToEatB() for different worlds, right?
Yes I am, I thought originally when I was trying to solve it myself it may of had something to do with that.
Risen Risen

2021/4/27

#
There is method in Greenfoot. It called addedToWorld. Try this in your code
int currentWorld;
public void addedToWorld(World w) {
if (w instanceof World1)
currentWorld = 0;
else
currentWorld = 1;
}
public void act() {
if (currentWorld == 0)
tryToEat();
else
tryToEatB();
}
BenjiFoxy BenjiFoxy

2021/4/27

#
Risen wrote...
There is method in Greenfoot. It called addedToWorld. Try this in your code
int currentWorld;
public void addedToWorld(World w) {
if (w instanceof World1)
currentWorld = 0;
else
currentWorld = 1;
}
public void act() {
if (currentWorld == 0)
tryToEat();
else
tryToEatB();
}
Thank you so much to the both of you, this now works!
danpost danpost

2021/4/27

#
An easier way is to not worry about which counter it is. You only need one Counter class that can create both counters. Your Tardis class would simply be:
import greenfoot.*;
import javax.swing.JOptionPane;

public class Tardis extends Characters
{
    Counter counter;
    
    public Tardis(Counter counter)
    {
        this.counter = counter;
    }
    
    public void act()
    {
        move(4);
        checkKeys();
        tryToEat();
    }
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left")) turn(-5);
        if (Greenfoot.isKeyDown("right")) turn(5);
    }
    
    private void tryToEat()
    {
        if (isTouching(ArtronEnergy.class))
        {
            removeTouching(ArtronEnergy.class);
            counter.add(100);
        }
        if (counter.getValue() >= 100)
        {
            if (getWorld() instanceof MarsWorld)
            {
                JOptionPane.showMessageDialog(null,"Victory is ours!", "You Won!",JOptionPane.INFORMATION_MESSAGE);
                Greenfoot.stop();
            }
            else Greenfoot.setWorld(new MarsWorld());
        }
    }
}
You need to login to post a reply.