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

2020/12/30

error

Sathya_srikanth Sathya_srikanth

2020/12/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class monster_1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster_1 extends main_actors
{
    private int speed = 2; // adjust '3' as needed

    public void act()
    {
        
        setLocation(getX(), getY()+speed);
        if (isTouching(jerry.class))
        {getWorld().removeObject(this);
        World MainGameBackground = getWorld();
        MainGameBackground mainGameBackground = (MainGameBackground)MainGameBackground;
        Counter counter = mainGameBackground.getCounter();
        counter.addScore();}
        else if (isAtEdge()) getWorld().removeObject(this);
    }   
}
when i run this code, it shows some error ( not syntax)
Sathya_srikanth Sathya_srikanth

2020/12/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    public void act() 
    {
    setImage(new GreenfootImage("Score : " + score, 27, greenfoot.Color.GREEN,greenfoot.Color.BLACK));
    }    
     
    public void addScore()
    {
        score++;
    }
}
this is the counter object's code.
Sathya_srikanth Sathya_srikanth

2020/12/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainGameBackground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainGameBackground extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class MainGameBackground.
     * 
     */
    public MainGameBackground()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        addObject(new jerry(),35,503);
        addObject(new tom(),923,473);
        prepare();
    }
    public Counter getCounter()
    {
        return counter;
        
    }
    
    private void prepare()
    {
        addObject(new Counter(),119,63); 
    }
    public void act()
    {
        if (Greenfoot.getRandomNumber(80) == 0) // adjust '200' as needed
        {
            int x = Greenfoot.getRandomNumber(getWidth()-50)+25;
            addObject(new Monster_1(), x, 0);
        }

    }
}
this is the world's code.
Sathya_srikanth Sathya_srikanth

2020/12/30

#
can anybody tell me whats wrong with this?
danpost danpost

2020/12/30

#
Sathya_srikanth wrote...
can anybody tell me whats wrong with this?
In Monster_1 class above, remove line 20 and modify line 19 to be:
MainGameBackground mainGameBackground = (MainGameBackground) getWorld();
(need to let compiler know that the world will actually be of type MainGameBackground before it can be assigned to that type variable) Change Counter class to be:
import greenfoot.*;

public class Counter extends Actor
{
    int score;
    
    public Counter()
    {
        updateImage();
    }
    
    public void addScore()
    {
        score++;
        updateImage();
    }
    
    private void updateImage()
    {
        setImage(new GreenfootImage("Score : "+score, 27, Color.GREEN, Color.BLACK));
    }
}
(only need to set a new image when object is created and when the score changes) In MainGameBackground class, modify line 32 to be:
addObject(counter, 119, 63);
{otherwise, getCounter will not return the same Counter object as that which is in the world)
Sathya_srikanth Sathya_srikanth

2020/12/30

#
it still shows that java.lang.NullPointerException at Monster_1.act(Monster_1.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2020/12/30

#
Sathya_srikanth wrote...
it still shows that java.lang.NullPointerException at Monster_1.act(Monster_1.java:20)
Switch lines 19 and 20 (minus any brackets).
Sathya_srikanth Sathya_srikanth

2020/12/30

#
i tried interchanging the lines, showed some syntax error so i left it as it is..( it still shows the "java.lang.NullPointerException") pls tell me if this code will be proper
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class monster_1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster_1 extends main_actors
{
    private int speed = 2; // adjust '3' as needed
 
    public void act()
    {
         
        setLocation(getX(), getY()+speed);
        if (isTouching(jerry.class))
        {getWorld().removeObject(this);
        MainGameBackground mainGameBackground = (MainGameBackground) getWorld();
        Counter counter = mainGameBackground.getCounter();
        counter.addScore();}
        else if (isAtEdge()) getWorld().removeObject(this);
    }   
}
Sathya_srikanth Sathya_srikanth

2020/12/30

#
        Counter counter = mainGameBackground.getCounter();
this is the line which has the "java.lang.NullPointerException"
danpost danpost

2020/12/30

#
Sathya_srikanth wrote...
i tried interchanging the lines, showed some syntax error so i left it as it is..( it still shows the "java.lang.NullPointerException") pls tell me if this code will be proper << Code Omitted >>
I had the wrong lines to switch. Switch lines 18 and 19 (minus any brackets).
Sathya_srikanth Sathya_srikanth

2020/12/30

#
thanks so much :D it worked!
You need to login to post a reply.