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

2022/3/6

Error java.lang.NullPointerException

Slimey Slimey

2022/3/6

#
Im trying to make it so that if I hit a alien I lose health but every time I try to start the game the greenfoot terminalcomes up with this message
Slimey Slimey

2022/3/6

#
I can send images of my code in Healthbar and my World I am also using a older version of greenfoot 2.4.2 I think
Slimey Slimey

2022/3/6

#
The error is in the code hitAliens public void act() { addAliens(); hitAlien(); } public void hitAlien() { healthbar.takeDamage(); }
danpost danpost

2022/3/6

#
What does the error trace show? (copy/paste) However, from what I see by the provided code, it looks like you have classing issues (I could be wrong). But, hitAliens does not sound like it should be a class. Also, the act method calls the hitAlien method repeatedly, which has no condition on taking damage. In other words, damage will be taking at all times and zero the health bar in short order (if the error is not arrived at first). Have the world control game play (adding aliens when needed, etc). Do not have actors change types in mid-stream. A dead or injured alien is still an alien and one class should control aliens. Do not use actors that have no useful purpose in the world. If it is not seen and it does not directly interact with any other actors in the world, it should not be in the world and that Actor subclass should not exist.
Slimey Slimey

2022/3/6

#
java.lang.NullPointerException at Space.hitAlien(Space.java:35) at Space.act(Space.java:30) at greenfoot.core.Simulation.actWorld(Simulation.java:600) at greenfoot.core.Simulation.runOneLoop(Simulation.java:535) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) this is what the error terminal was saying
Slimey Slimey

2022/3/6

#
And how would i make it so that if my character (Robot) is to touch a (alien) I would take damage
Slimey Slimey

2022/3/6

#
This code is inside my World
Slimey Slimey

2022/3/6

#
In my Alien code if i wanted to make it so that if it hits the edge I would lose hp how would I do it This is my current code private void atEdge() { if (isAtEdge()) { getWorld().removeObject(this); } }
danpost danpost

2022/3/6

#
Looks like your healthbar field was not assigned an object. You probably used a local variable inside your world constructor (or prepare method) and not the declared field for the object. So, if you have something like the following in a method:
HealthBar healthbar = new HealthBar();
then change it to:
healthbar = new HealthBar();
so as not to declare a local variable.
Slimey Slimey

2022/3/6

#
I dont have any of that in my code
Slimey Slimey

2022/3/6

#
But i have another question now is there any way i can go into my alien class and make it so that if the aliens are at the edge they will cause me to lose 1 health
Slimey Slimey

2022/3/6

#
Now it is saying non-static method takeDamage() cannot be referenced from a static context
You need to login to post a reply.