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

2012/11/27

The nullPointerException error...

Stormtrooper299 Stormtrooper299

2012/11/27

#
I am having troubles figuring this one out, because the code that I usually use to fix it isn't working for this time. Please help. Error: java.lang.NullPointerException at Towers.act(Towers.java:71) Line of code:
1
if (Enemies.main.getWorld() == null && Enemies.main == null)
Code's Target (Enemies Class) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Enemies here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemies extends Actor
{
    public static Enemies main;
    public void act()
    {
        main = this;
        moving();
    }  
    public void moving()
    {
        turnTowards(Finish.main.getX(), Finish.main.getY());
        move(2);
    }
}
erdelf erdelf

2012/11/27

#
if enemy.main is null, the method getWorld() cant be called, because there is no object that could call it
Stormtrooper299 Stormtrooper299

2012/11/27

#
Oh, sorry it was supposed to be
1
if (Enemies.main.getWorld() != null && Enemies.main != null)
Stormtrooper299 Stormtrooper299

2012/11/27

#
But does that still have the same problem?
Stormtrooper299 Stormtrooper299

2012/11/27

#
Also, i'm getting the same error here
1
2
3
4
public void spawner()
   {
       getWorld().addObject(new Enemies(), Start.main.getX(), Start.main.getY());
   }
Which this code is under "Start".
erdelf erdelf

2012/11/27

#
for the first
1
2
3
4
5
6
7
if (Enemies.main != null
{
        if(Enemies.main.getWorld() != null)
         {
              //your code
          }
 }
for the second:
1
2
3
4
5
6
7
public void spawner()
{
if (Enemies.main != null
{
getWorld().addObject(new Enemies(), Start.main.getX(), Start.main.getY());
}
}
Stormtrooper299 Stormtrooper299

2012/11/28

#
Thanks! I got the first one working! However the second still doesn't work, what you gave me doesn't give me an error, but that is because now it doesn't run. The code shoulden't need to have an Enemies class, since it is only creating one. I found out what the nullExceptionError is coming from and it is actually the World! Which I have never seen. I rephrased the code to this:
1
2
3
4
5
public void spawner()
   {
       TD myTD = (TD) getWorld();
       myTD.spawner();
   }
Where TD is my World's name. The Error is pointing at
1
myTD.spawner();
specifically, which doesn't make sense to me? It is saying that the World isn't there??
danpost danpost

2012/11/28

#
It is not saying that the world is not there; it is saying that your 'Start' object is not in a world.
Stormtrooper299 Stormtrooper299

2012/11/28

#
This code is in the 'Start' object, So in order to run this code doesn't 'Start' need to be in the World?
Stormtrooper299 Stormtrooper299

2012/11/28

#
Although, I figured out a way to do it not in the Start class, and now it works, so what you are saying makes sense. haha
davmac davmac

2012/11/28

#
1
if (Enemies.main.getWorld() != null && Enemies.main != null)
But does that still have the same problem?
Yes, because if Enemies.main is null the call to Enemies.main.getWorld() causes a NullPointerException. However, you can simply change the order:
1
if (Enemies.main != null && Enemies.main.getWorld() != null)
In this version, due to "lazy evaluation", if Enemies.main is null then Enemies.main.getWorld() will not be evaluated.
You need to login to post a reply.