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

2017/9/11

showText from an actor

GustavoA GustavoA

2017/9/11

#
Is it possible to use the world method showText from an actor using something like
getWorld().showText(string, int , int) 
? Need to show some sort of health, but it throws following error:
java.lang.NullPointerException at WombatRight.<init>(WombatRight.java:32) at PlayWorld.<init>(PlayWorld.java:26) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:617) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$7(WorldHandlerDelegateIDE.java:430) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502) at greenfoot.core.Simulation.maybePause(Simulation.java:305) at greenfoot.core.Simulation.runContent(Simulation.java:218) at greenfoot.core.Simulation.run(Simulation.java:211)
GustavoA GustavoA

2017/9/11

#
Managed to make it work. Pretty sure I called it in the constructor, which gave this issue.
danpost danpost

2017/9/11

#
GustavoA wrote...
Managed to make it work. Pretty sure I called it in the constructor, which gave this issue.
That is precisely why.
at WombatRight.<init>(WombatRight.java:32)
The '<init>' in this line means that the constructor of the WombatRight class was being executed when the error occurred. The actor being created has yet to be placed into a world and cannot have its 'getWorld' method return any world at that time.
You need to login to post a reply.