javaimagenullpointerexceptionslick2d

Java Slick2D drawImage nullpointer


Here's my code

package game.src;


import java.util.ArrayList;

import org.lwjgl.opengl.Drawable;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;


public class Main extends BasicGame{

    public static final int WIDTH = 1920;
    public static final int HEIGHT = 1080;
    private Image grass = null; //Initializing the image







    public Main() {
            super("Wizard game");
        }

        public static void main(String[] arguments)
        {
            try
            {
                AppGameContainer app = new AppGameContainer(new Main());
                app.setDisplayMode(WIDTH, HEIGHT, false);
                app.setAlwaysRender(true);
                app.start();

            }
            catch (SlickException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void init(GameContainer container) throws SlickException
        {

            Image grass = new Image("res/grass.png"); //Location of picture (see link below)

        }

        @Override
        public void update(GameContainer container, int delta) throws SlickException
        {
        }

        public void render(GameContainer container, Graphics g) throws SlickException{
        g.drawImage(grass, 0, 0); //This is the crash location



        }
    }

Here's the console error

Fri Mar 28 22:21:36 CDT 2014 INFO:Slick Build #237
Fri Mar 28 22:21:36 CDT 2014 INFO:LWJGL Version: 2.9.0
Fri Mar 28 22:21:36 CDT 2014 INFO:OriginalDisplayMode: 1920 x 1080 x 32 @60Hz
Fri Mar 28 22:21:36 CDT 2014 INFO:TargetDisplayMode: 1920 x 1080 x 0 @0Hz
Fri Mar 28 22:21:36 CDT 2014 INFO:Starting display 1920x1080
Fri Mar 28 22:21:36 CDT 2014 INFO:Use Java PNG Loader = true
WARNING: Found unknown Windows version: Windows 7
Attempting to use default windows plug-in.
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
Fri Mar 28 22:21:37 CDT 2014 INFO:Found 5 controllers
Fri Mar 28 22:21:37 CDT 2014 INFO:0 : USB Multimedia Keyboard
Fri Mar 28 22:21:37 CDT 2014 INFO:1 : USB Multimedia Keyboard
Fri Mar 28 22:21:37 CDT 2014 INFO:2 : USB Multimedia Keyboard
Fri Mar 28 22:21:37 CDT 2014 INFO:3 : Turtle Beach PX21 Headset
Fri Mar 28 22:21:37 CDT 2014 INFO:4 : USB  AUDIO  
Fri Mar 28 22:21:37 CDT 2014 ERROR:null
java.lang.NullPointerException <--- I'm guessing it can't find the picture so it thinks it's null.
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1384)
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1433)
    at game.src.Main.render(Main.java:65)
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
    at game.src.Main.main(Main.java:41)
Fri Mar 28 22:21:37 CDT 2014 ERROR:Game.render() failure - check the game code. <-- No shit
org.newdawn.slick.SlickException: Game.render() failure - check the game code. <-- No shit
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
    at game.src.Main.main(Main.java:41)

I don't have a clue what is going on, and i'm pretty sure I installed LWJGL and Slick correctly. I suspect that for some reason the path is not working, making the picture equal null.

Image of my pathing https://i.sstatic.net/PxSXC.jpg


Solution

  • In your init method, you shadow the grass variable. Try changing Image grass = new Image("res/grass.png"); to just grass = new Image("res/grass.png");, in order to set the field grass rather than create a new variable in the method.

    Because you never change the grass field from null, you get a NullPointerException when you try and draw it.