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

2021/2/1

I get an weird error

XApple15 XApple15

2021/2/1

#
Hi ! It`s me again :D . I rebuit the code for this actor , but after i keep pressing the C button on my keyboard and i lift it at a random moment , i get an weird error My code :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Job1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Job1 extends Jobs
{
    boolean FinishedCutting = false;
    long LastTime, CurrentTime;
    private int i, j, bar_number;
    private int CutDelay = 50;
    
    public void act() 
    {
        if( Greenfoot.isKeyDown("c") && FinishedCutting == false) 
            Cutting();

    }    

    public  void Cutting() 
    {
        for ( bar_number = 1 ; bar_number <=4 ; bar_number++ )
        {
            for( i=1 ; i<=10;i++) 
            {
                setImage("cuts/cut" + bar_number + "_"  + i + ".png" );
                if( Greenfoot.isKeyDown("c") == false )
                { getWorld().removeObject(this);}
                LastTime = System.currentTimeMillis();
                while ( LastTime + CutDelay >= System.currentTimeMillis() ) {}
                getWorld().repaint();
            }
            for(i=9 ; i>=1 ;i-- ) 
            {
                setImage("cuts/cut" + bar_number + "_"  + i + ".png" );
                if( Greenfoot.isKeyDown("c") == false )
                {  getWorld().removeObject(this);}
                LastTime = System.currentTimeMillis();
                while ( LastTime + CutDelay >= System.currentTimeMillis() ) {}
                getWorld().repaint();
            }

        }
        if( bar_number == 4) 
        {
            getImage().clear();
            goToNextPoint();
            FinishedCutting = true;
        }
    }

    private void goToNextPoint()
    {
        getWorld().removeObjects( getWorld().getObjects(Bars.class));
    }


    private void DisplayText()   
    {
        if( Greenfoot.isKeyDown("x") == true )
        {
            getWorld().removeObject(this);  
        }
    }

}
Error :
java.lang.NullPointerException
	at Job1.Cutting(Job1.java:35)
	at Job1.act(Job1.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)
I think this error has to deal something with the fact that the compiler cannot find the Actor, but the actor is there :D Help me fix this ! Thank you!
danpost danpost

2021/2/1

#
XApple15 wrote...
I rebuit the code for this actor , but after i keep pressing the C button on my keyboard and i lift it at a random moment , i get an weird error << Code Omitted >> I think this error has to deal something with the fact that the compiler cannot find the Actor, but the actor is there
It is not the actor, but the world that it cannot find. Once the actor is removed from the world (line 31), the getWorld method will return a null value. Change lines 31 and 40 to:
{ getWorld().removeObject(this); return; }
to abort the method when the actor is removed from the world.
You need to login to post a reply.