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

2013/1/11

Null pointer exception to... I don't know.

Minion1 Minion1

2013/1/11

#
I've been looking at this for probably 5 hours now, I need help. There are two classes: Scroller and a subclass Cursor. The goal is to make the background move underneath the Cursor object, and the Cursor will detect the color of a single pixel from the background it is standing over. But for some reason the redCheck method in the Cursor class is stopping here: int red = getWorld().getColorAt(getX(), getY() - 5).getRed(); and giving me a null pointer exception. I can't seem to understand what it's not finding. I've done a check to verify that the background is seen, and it is. It can't seem to find the red color, but i don't know why. Here's the code for Scroller, it's mostly movement code that makes one call to the redCheck method in the Cursor object:
public void checkKeyPress()
    {
        timer++;
        if(Greenfoot.isKeyDown("W") && timer >= 10)        
        {
            setLocation(getX(), getY() + MOVEDISTANCE);
            ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        else if(Greenfoot.isKeyDown("A") && timer >= 10)
        {
            setLocation(getX() + MOVEDISTANCE, getY());
            ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        else if(Greenfoot.isKeyDown("S") && timer >= 10)
        {     
            setLocation(getX(), getY() - MOVEDISTANCE);
            ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        else if(Greenfoot.isKeyDown("D") && timer >= 10)
        {       
            setLocation(getX() - MOVEDISTANCE, getY());
            ((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        if(timer > 20)
        {
            timer = 10;
        }
    }
And here's the code for the Cursor:
public void redCheck()
    {
        Background bg = ((Background)getWorld().getObjects(Background.class).get(0));
        if(bg != null)
        {
            System.out.println("bg detected");
        }
        int red = getWorld().getColorAt(getX(), getY() - 5).getRed();
        System.out.println(red);

        if(red == 95)
        {
            whereAmI = "forest";
        }
        else if(red == 195)
        {
            whereAmI = "cave";
        }
        else if(red == 171)
        {
            whereAmI = "castle";
        }
        else if(red == 0)
        {
            whereAmI = "water";
        }
        else if(red == 198)
        {
            whereAmI = "town";
        }
        else if(red == 220)
        {
            whereAmI = "path";
        }
        else if(red == 166)
        {
            whereAmI = "mountain";
        }
        else if(red == 145)
        {
            whereAmI = "end";
        }
    }
Thanks to help earlier I solved one problem, but now I'm stuck on this one.
Minion1 Minion1

2013/1/11

#
I forgot to mention, the background and the Cursor are already in the world. There's no problem there, the code compiles fine, it only messes up when I attempt to move in any direction. It's looking for the redness of the pixel under the cursor, but not able to find it.
Minion1 Minion1

2013/1/11

#
I've made several changes and rearranged the code a few times, but ultimately keep coming back to the same problem. My only thought is that the Cursor cannot detect the background image. But I put a check in to make sure that it's finding the background object, and it is. But it can't seem to find the redness. I'm absolutely stuck.
danpost danpost

2013/1/11

#
From the description of the method 'getColorAt' in the World API, it says it returns the color at the center of the cell located at (x, y). I did not try it on a scenario with cells larger than one pixel, but from that description, it may require cells larger than one to work. I could not get it to work in a scenario with pixel-sized cells, either. If it does not work on cells larger than one also, then it may be a bug in Greenfoot or BlueJ that is preventing it from working propery. Looks like you may have to get the background image and determine at what point in the image your actor is, and use the 'getColorAt' method on that (using the method in the GreenfootImage API).
Minion1 Minion1

2013/1/11

#
The thing is, this scenario I'm working on is using the exact same code as one I was working on earlier (which you also helped me with). In THAT scenario, there was no scrolling world. The background was stationary and the cursor moved over it. The color detection method worked like a charm. In THIS scenario, I've reversed things, the background now moves, and the cursor stays stationary... and now the color detection doesn't seem to work. I'm trying to make it so that the color detection happens from the Cursor Object, and it will always check the same pixel location underneath the cursor (everything in the world, background and cursor) is made from 32 by 32 tiles, so the pixel location to be checked is exactly the same each time, because everything moves by 32 pixels.
danpost danpost

2013/1/11

#
OK, I was able to get it working in a scenario with pixel-sized cells; both with a bounded world and an un-bounded world. However, these were not in a world where the background scrolls.
Minion1 Minion1

2013/1/11

#
Yeah. It works just fine in the old scenario. The color detection idea worked great. Thanks for that. But for some reason I can't get it to work in the scrolling version.
Minion1 Minion1

2013/1/11

#
Thank you danpost, your observation about needing a more accurate location of the background helped me find a solution. It's not complete yet, since it's looking PAST the background picture and into the all white layer, but all in all I think I can fix it from here. If not, well, I'll probably be back here begging for help.
You need to login to post a reply.