Java Slick2D drawImage nullpointer

时间:2014-03-29 03:32:14

标签: java image nullpointerexception slick slick2d

这是我的代码

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



        }
    }

这是控制台错误

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)

我不知道发生了什么,我很确定我正确安装了LWJGL和Slick。我怀疑由于某种原因路径不起作用,使图片等于null。

我的路径http://imgur.com/vLRGIcf

的图片

1 个答案:

答案 0 :(得分:0)

在init方法中,您隐藏grass变量。尝试将Image grass = new Image("res/grass.png");更改为grass = new Image("res/grass.png");,以便设置字段grass,而不是在方法中创建新变量。

因为你永远不会将草字段从null更改,所以当你尝试绘制它时会出现NullPointerException。