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

2017/6/22

Logical Error Problem

ET78759 ET78759

2017/6/22

#
So I'm creating a new scenario similar to one of my other scenarios, Crash Course, and I'm running into a problem. When I test it, I move object Naruto into object Sasuke, and it a terminal error window pops up for some reason that I can't figure out. I neet help... Class MyWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author Edwin 
 * @version 1.0
 */
public class MyWorld extends World
{
    private Naruto naruto;
    private OtherNaruto otherNaruto;
    private ShadowClone shadowClone;
    private Sasuke sasuke;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    public void Naruto()
    {
        if (naruto.getWorld() == this)
        {   naruto.subtractHealth();
        }
    }

    public void Sasuke()
    {
        if (sasuke.getWorld() == this)
        {sasuke.subtractHealth();
        }
    }

    public void OtherNaruto()
    {
        if (otherNaruto.getWorld() == this) 
        {otherNaruto.subtractHealth();
        }
    }

    public void ShadowClone()
    {
        if (shadowClone.getWorld() == this) {
            shadowClone.subtractHealth();
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Naruto naruto = new Naruto();
        addObject(naruto,94,110);
        ShadowClone shadowclone = new ShadowClone();
        addObject(shadowclone,457,110);
        OtherNaruto othernaruto = new OtherNaruto();
        addObject(othernaruto,306,270);
        othernaruto.setLocation(85,272);
        Sasuke sasuke = new Sasuke();
        addObject(sasuke,454,285);
    }
}
Class Naruto:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Naruto here.
 * 
 * @author Edwin 
 * @version 1.0
 */
public class Naruto extends Actor
{
    private int health = 100;
    /**
     * Act - do whatever the Naruto wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        run();
        punch();

    }    
    
    public void run()
    {
        if(Greenfoot.isKeyDown("w"))
        {
            move(4);
        }
        if(Greenfoot.isKeyDown("s"))
        {
            move(-4);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            turn(4);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            turn(-4);
        }
    }
    
    public void punch()
    {
        MyWorld world = (MyWorld) getWorld();
        Actor shadowClone;
        shadowClone = getOneObjectAtOffset(0,0, ShadowClone.class);

        if (shadowClone != null)
        {

            world.OtherNaruto();
            world.Sasuke();
        }
        Actor otherNaruto;
        otherNaruto = getOneObjectAtOffset(0,0, OtherNaruto.class);

        if (otherNaruto != null)
        {

            world.ShadowClone();
            world.Sasuke();

        }
        
        Actor sasuke;
        sasuke = getOneObjectAtOffset(0,0, Sasuke.class);
        if (sasuke != null)
        {

            world.OtherNaruto();
            world.ShadowClone();
        }
    }
    
    public void subtractHealth()
    {
        health = health - 25;
    }

}
Class Sasuke:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sasuke here.
 * 
 * @author Edwin 
 * @version 1.0
 */
public class Sasuke extends Actor
{
    private int health = 100;
    /**
     * Act - do whatever the Sasuke wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        run();
        punch();

    }    
    
    public void run()
    {
        if(Greenfoot.isKeyDown("5"))
        {
            move(4);
        }
        if(Greenfoot.isKeyDown("2"))
        {
            move(-4);
        }
        if(Greenfoot.isKeyDown("1"))
        {
            turn(4);
        }
        if(Greenfoot.isKeyDown("3"))
        {
            turn(-4);
        }
    }
    
    public void punch()
    {
        
    }
    
        public void subtractHealth()
    {
        health = health - 25;
    }
}
The Naruto class is the only one with code in the method punch for testing purposes.
danpost danpost

2017/6/22

#
ET78759 wrote...
a terminal error window pops up for some reason that I can't figure out..
What does it say? (copy/paste terminal contents)
valdes valdes

2017/6/22

#
It must be a NullPointerException. The error is in method prepare() of the World class. You're using local variables instead of the member variables
Yehuda Yehuda

2017/6/22

#
You didn't say what the message in the terminal was. The problem that I can see in your code is the classes that you used in the prepare() method are a different instance then those of the rest of the class. You already initialized an instance of each class so you don't need to create a new instance of each class in the prepare() method since that just makes it only accessible in that method. The fields created outside all methods are accessible everywhere, but they weren't initialized. (A separate problem is all those methods in MyWorld don't really make sense. Instead of calling a method in MyWorld from Naruto which calls a method in Naruto, you should just have all that code in their respective classes.)
valdes valdes

2017/6/22

#
private void prepare()
    {
        naruto = new Naruto();
        addObject(naruto,94,110);
        shadowClone = new ShadowClone();
        addObject(shadowClone,457,110);
        otherNaruto = new OtherNaruto();
        addObject(otherNaruto,306,270);
        otherNaruto.setLocation(85,272);
        sasuke = new Sasuke();
        addObject(sasuke,454,285);
    }
Yehuda Yehuda

2017/6/22

#
I started typing my post before there were any others then when I posted mine there were two before it.
ET78759 ET78759

2017/6/22

#
The error is a NullPointerException, and it says: java.lang.NullPointerException at MyWorld.OtherNaruto(MyWorld.java:43) at Naruto.punch(Naruto.java:52) at Naruto.act(Naruto.java:19) 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)
danpost danpost

2017/6/22

#
User valdes explained the error here and gave the fix here.
Yehuda Yehuda

2017/6/22

#
A solution was already given to this problem. The instance on line 43 of the MyWorld class called 'otherNaruto' was never initialized, the one added to the world is a different one. You have to use the same instance in the whole class. That's what valdes and I said already.
ET78759 ET78759

2017/6/25

#
Thanks! It works now.
You need to login to post a reply.