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

2018/8/19

Door opening

Dr.Dre Dr.Dre

2018/8/19

#
Hi everyone in my game I'm trying to make a door open with a key i put it like this. I have a class door, a class Player and a class key and i put it like this
private boolean key = false;

 public void key()
    {
        if(isTouching(key.class))
        {
            boolean key = true;
        }
    }

public void remove_door()
    {
        if(isTouching(door.class))
        {
            if(key=true)
            {
                w1.removeObject(d);
            }
        }
    }
It's not working i have a java.lang.NullPointerException when i touch the door can anyone help please thanks
Super_Hippo Super_Hippo

2018/8/20

#
Show how you call these methods. Also, remove the "=true" in line 15 (or use "==" instead of "="). Where are "w1" and "d" defined and set to anything?
danpost danpost

2018/8/20

#
Remove the word "boolean" from line 7. You are inadvertently creating a new variable there (which is NOT that declared on line 1). By removing the type on line 7, it will then refer to the one declared on line 1. Replace line 13 with the following:
Actor d = getOneIntersectingObject(door.class);
if (d != null)
No NullPointerException error can then be caught here.
You need to login to post a reply.