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

2020/5/7

iteration issue

PreciseRook PreciseRook

2020/5/7

#
I need to figure out the solution on my own because this is a project, but can somebody please help me figure out why this if statement isn't working? myWorld class
public class MyWorld extends World
{
    List<Integer> xloclist = new ArrayList<Integer>();
    List<Integer> blockheightlist = new ArrayList<Integer>();
    private int calls = 0;

    //there is a filereader in between that loads integers into xloclist, but I omitted it here to save space

    public int xloc0(){
        return xloclist.get(0);
    }
    
    public int xlocmin(){
        return xloclist.get(Collections.min(xloclist));
    }
    
    //returns the current call count
    public int globalcalls(){
        return calls;
    }
    
    public int globalcallsup(){
        calls++;
        return calls;
    }
    
    public int globalcallreset(){
        calls = 0;
        return calls;
    }
}
sorting bar class
public class SortingBar extends Actor
{
    private int calls = 0; //initialize call to act counter
    MyWorld myWorld = (MyWorld) getWorld(); //instantiate the world locally for easier access

    public void act(){
        //if n key is down and calls is 0 or over 25, then...
        if(Greenfoot.isKeyDown("n") && calls == 0 || Greenfoot.isKeyDown("n") && calls >25){
            //if this x location is 0 or if it is equal to the x location of the shortest block then...
            if(this.getX() == myWorld.xloc0() || this.getX() == myWorld.xlocmin()){ //this line is the problem child
                //turn the two boxes green.
                this.highlight();
            }
            //increment the call counters in the world and locally
            for(int i=0;i<25;i++){ 
                myWorld.globalcallsup(); //increment global call count
                calls++;//increment local call count
                System.out.println(myWorld.globalcalls()); //print out global call count(for debug)
                
            }
            myWorld.globalcallreset(); //reset the global call counter
            calls = 0; //reset the local call counter
        }
    }
}
Zamoht Zamoht

2020/5/7

#
It would make it a lot easier, if you could explain the issue you are having. As well give a few words on what you are trying to do, and why you believe the program is performing differently. Having that said, one problem I am able to see is the use of the myWorld variable. When you initialize it like that, the variable is set before the actor is added to the world, thus the value is going to be null. When you use myWorld in the act method I believe you get a null pointer exception. Instead replace "myWorld" with "getWorld()" or move the variable to inside the act method. If you really want a field in the actor with the given world, you can use the "addedToWorld(World world)" method and set the value from there.
PreciseRook PreciseRook

2020/5/7

#
Ah, you're right. My bad, I thought I mentioned the null pointer. Moving the myWorld into act() fixed it, thank you!
You need to login to post a reply.