So I've been working on a game where each game object has a trail, but I was having a problem where the trails where being rendered on top of the objects rather than behind. So, I changed from one for loop to two - one where I render all the trails first and then another where I render the objects themselves. Since doing this, I keep getting NullPointerExceptions, even though I'm checking for them. Here is the error I get:
Exception in thread "Thread-3" java.lang.NullPointerException
at com.tutorial.main.Handler.render(Handler.java:53)
at com.tutorial.main.Game.render(Game.java:199)
at com.tutorial.main.Game.run(Game.java:132)
at java.lang.Thread.run(Unknown Source)
and here is the class where the error is happening:
package com.tutorial.main;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.Random;
import com.tutorial.main.Game.STATE;
public class Handler
{
LinkedList<GameObject> object = new LinkedList<GameObject>();
Random r = new Random();
public void tick()
{
// if to ensure there is something in the handler
if (object.size() != 0)
{
// for loop to update each object
for (int i = 0; i < object.size(); i++)
{
GameObject tempObject = object.get(i);
tempObject.tick();
} // end of for loop to update each object
} // end of if to ensure there is something in the handler
} // end of tick
public void render(Graphics g)
{
// sequential ifs to ensure there is something in the handler
if (object != null && object.size() != 0)
{
// for loop to render trails first
for (int j = 0; j < object.size(); j++)
{
GameObject tempObject = object.get(j);
// if to verify object is a trail
if (tempObject.getID() == ID.Trail)
{
tempObject.render(g);
} // end of if to verify object is a trail
} // end of for loop to render trails first
}
if (object != null && object.size() != 0)
{
// for loop to render everything else
for (int k = 0; k < object.size(); k++)
{
GameObject tempObject = object.get(k);
// if to verify object is not a trail
if (tempObject.getID() != ID.Trail)
{
tempObject.render(g);
} // end of if to verify object is not a trail
} // end of for loop to render everything else
} // end of sequential ifs to ensure there is something in the handler
} // end of render
public void clearEnemies()
{
int timer = 10;
// for loop to clear enemies
for (int i = 0; i < object.size(); i++)
{
GameObject tempObject = object.get(i);
// if-else-if to check if enemy
if (tempObject.getID() != ID.Player)
{
removeObject(tempObject);
i--;
}
else if (tempObject.getID() == ID.Player)
{
// if to ensure game is over
if (Game.gameState != STATE.Game)
{
removeObject(tempObject);
i--;
} // end of if to ensure game is over
} // end of if-else-if to check if enemy
} // end of for loop to clear enemies
} // end of clearEnemies
public void addObject(GameObject object)
{
this.object.add(object);
} // end of addObject
public void removeObject(GameObject object)
{
this.object.remove(object);
} // end of removeObject
} // end of class
so, I'm having trouble figuring out why I'm getting the exceptions despite having if (object != null && object.size() != 0)
. Please let me know if I should include my other classes too. Thanks so much!
Edit: fixed a typo