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

2014/11/26

Question about arrays

1
2
Favna Favna

2014/11/26

#
So I've got a for as far as playing the game goes fully working boolean array that tracks if an item has been picked up already or not, but whenever I go to inspect on my "player" and click the entry of the array I get a nullpointer exception. I'm pretty sure this should be fixable. The error report is as follows:
Exception in thread "Timer-46" java.lang.NullPointerException at bluej.debugmgr.inspector.ObjectInspector.listElementSelected(ObjectInspector.java:413) at bluej.debugmgr.inspector.Inspector.update(Inspector.java:337) at greenfoot.gui.inspector.UpdateTask.run(UpdateTask.java:45) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505)
The code of the player that creates, calls and edits things in the array is the following. I am positive that the issue is not in the Pickup method because when I completely comment that out and inspect the player I get the same error, I added it regardless for completeness of code.
public class Player extends Actor
{

    //Declaring the array for tracking what items have been picked up as well as filling it with default false.
    boolean[] items = new boolean[] {false, false, false};

    public void act() 
    {
        //Calling all methods
        pickup();

    //This method is to check if the player can see the item to be picked up. When it does it stores this in the array.
    public void pickup()
    {
        if (canSee(Apple.class))
        {
            if(items[0] == false) {
                Greenfoot.playSound("item.wav");
                items[0] = true;
            }
        }

        if (canSee(Ketchup.class))
        {
            if(items[1] == false) {
                Greenfoot.playSound("item.wav");
                items[1] = true;
            }
        }

        if (canSee(Cookie.class))
        {
            if(items[2] == false) {
                Greenfoot.playSound("item.wav");
                items[2] = true;
            }
        }
    }
davmac davmac

2014/11/26

#
What Greenfoot version are you running?
danpost danpost

2014/11/26

#
The class shown should not compile. Your bracketing is off -- well, you are missing a close squiggly bracket at the end of your act method (and another one at the end of the entire class, which may have been left out of the copy/pasting).
Favna Favna

2014/11/26

#
danpost wrote...
The class shown should not compile. Your bracketing is off -- well, you are missing a close squiggly bracket at the end of your act method (and another one at the end of the entire class, which may have been left out of the copy/pasting).
Uh yeah, it must have fallen off while copy+pasting indeed. Disregard that.
danpost danpost

2014/11/26

#
What about the one at the end of the act method (in the middle of your code)?
Favna2 Favna2

2014/11/26

#
geez <_< apparently posting image links to puush images is considered spam so my actual account got deactivated ... Aaanwaay, the following: Yeah they fell off because I removed the other methods that are being called. The game compiles perfectly, like I said, for as far as the game goes it can be played, lost, and won. The error only pops up when right clicking the player then inspect and then trying to inspect the array. If this wouldn't be for a school project I'd disregard the error but the last thing I want is the teacher suddenly seeing that error pop up when he is checking it haha.
Favna2 Favna2

2014/11/26

#
davmac wrote...
What Greenfoot version are you running?
Version 2.4.0 as can be seen here: http://i.imgur.com/CVesVY0.jpg (p.s. check my other reply if you're wondering why I'm not replying from my main account)
danpost danpost

2014/11/26

#
I do not think the teacher would down-grade you because of something that is, most probably, not your fault. Also, from I can tell, your Favna account appears to still be active.
Favna Favna

2014/11/26

#
davmac wrote...
What Greenfoot version are you running?
Greenfoot version 2.4.0
Favna2 Favna2

2014/11/26

#
danpost wrote...
I do not think the teacher would down-grade you because of something that is, most probably, not your fault. Also, from I can tell, your Favna account appears to still be active.
This is what I get when trying to comment with it :\
davmac davmac

2014/11/26

#
This is what I get when trying to comment with it
It says "post pending approval" not "account deactivated". Please read the message, and please do not create secondary accounts. The post has now been approved.
davmac davmac

2014/11/26

#
It looks like a Greenfoot bug, by the way. I will try to reproduce it when I have some spare time.
Favna Favna

2014/11/26

#
davmac wrote...
It looks like a Greenfoot bug, by the way. I will try to reproduce it when I have some spare time.
If it helps I can upload my game to the scenario's? I.. kinda don't see the option to do that however.
amjad amjad

2014/11/27

#
As davmac mentioned, it is a Greenfoot bug. However, you can solve it for now by changing: boolean items = new boolean {false, false, false}; to boolean items = {false, false, false};
danpost danpost

2014/11/27

#
amjad wrote...
As davmac mentioned, it is a Greenfoot bug. However, you can solve it for now by changing:
boolean[] items = new boolean[] {false, false, false}; 
// to
boolean[] items = {false, false, false};
I put code tags around the code so it can be viewed properly.
There are more replies on the next page.
1
2