0

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

Nicole
  • 3
  • 3
  • `// end of addObject` these comments are not useful, and actively make your code harder to read. A decent IDE like IntellIJ will highlight matching braces, and you can jump between them using the shortcut `CTRL` + `]` – Michael May 07 '20 at 12:06
  • I closed as a dupe of `&` vs `&&` because your text describes using `&`, but your code uses `&&`. Which are you actually using? – Andy Turner May 07 '20 at 12:09
  • Line#53 corresponds to the below code. Your "object" is not null however object.get(k) at line:50 returns null. if (tempObject.getID() != ID.Trail) To fix do a null check at line 53. if (tempObject != null && tempObject.getID() != ID.Trail) You may use Java8 Optional classes that eliminates too many null checks. – javapedia.net May 07 '20 at 12:12
  • Thank you for the note about comments - I'll try to find a better way! – Nicole May 07 '20 at 12:14
  • Sorry about the typo, I'm using &&. – Nicole May 07 '20 at 12:14
  • @Nicole the better way is just not to add the comments. Even with the comments, you still have to search upwards to find where the block starts - that's most easily done just by looking at the indentation. – Andy Turner May 07 '20 at 12:16
  • Thank you, adding the null check at line 53 fixed the issue! – Nicole May 07 '20 at 12:17
  • So instead of the comments at the end of everything, would it be better to just have a comment at the beginning of the method to describe what it does or not even that? Sorry, I'm still relatively new to programming. – Nicole May 07 '20 at 12:19

0 Answers0