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

2014/9/12

Null Pointer while creating World

1
2
S02 S02

2014/9/12

#
Inspector i already added to my world. you are right, 'inspector' in line 6 doesn't refer to same object. How can i use the Inspector object which is added in my world or In other words how can i call update() method (line 73 ) from class GumballMachine at line 6.
Super_Hippo Super_Hippo

2014/9/12

#
You can save the reference in your world class and have a getter method which returns this object. In the world class code:
private Inspector inspector;

//in the constructor or where you create the inspector
inspector = new Inspector();
addObject(inspector, /*x*/, /*y*/);


//as a method
public Inspector getInspector()
{
    return inspector;
}
Then you only need to get a reference to the world and you can call the method:
WorldName w = (WorldName) getWorld();
w.getInspector().update();
(Of course you can also make the 'inspector' field as 'public static' and just call 'WorldName.inspector.update()' from anywhere.) By the way, if your 'update' method doesn't do anything else than calling another method in the same class ('inspect'), then you could also call 'setMessage' inside the 'update' method and delete 'inspect'.
S02 S02

2014/9/12

#
will this add one more object to my world or i can use the inspector i already added to my world from GumballMachine class. Is there any way i can use that
danpost danpost

2014/9/13

#
From your GumballMachine class, you can use:
((Inspector)getWorld().getObjects(Inspector.class).get(0)).update((Coin)haveCoin);
This will get the inspector already in the world instead of the one created in the GumballMachine class (which should be removed).
S02 S02

2014/9/13

#
Thanks it resolved. Alos thanks for keeping patience and explaining so well, a am new to greenfoot but now loving to work on it.
You need to login to post a reply.
1
2