Search code examples
javatextureslwjgl

Cannot Load Texture in Java (lwjgl)


I have been going about on this for the past 3 hours. I'm trying to make a 2D Strategy game in Java using the lwjg libraries, and for starters, I've got stuck at loading a texture for an object (Farmer). Its giving me this:

Exception in thread "main" java.lang.NullPointerException
    at _2nd_Branch.Farmer.render(Farmer.java:42)
    at _1st_Branch.CoreGame.render(CoreGame.java:30)
    at _1st_Branch.Game.GameLoop(Game.java:33)
    at _1st_Branch.Game.main(Game.java:13)

Here is the code I've been working on (As a way to organize things. Got this from someone on youtube explaining some bits about lwjgl):

Game.java:

package _1st_Branch;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

public class Game {

private static CoreGame coreGame;

public static void main(String [] args)
{
    CD();   
    createGame();
    GameLoop();
    CleanUp();
}

private static void CD()
{
    Window.create(800, 600);
}
private static void createGame()
{
    coreGame = new CoreGame();
}

public static void GameLoop()
{
    while(!Window.isCloseRequested())
    {
        Window.Clear();
        coreGame.input();
        coreGame.logic();
        coreGame.render();
        Display.update();
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
        {
            Display.destroy();
            System.exit(0);
        }
    }
}
private static void CleanUp()
{
    coreGame.dispose();
    Window.Destroy();
}
}

CoreGame.java:

package _1st_Branch;
import _2nd_Branch.Farmer;
import _2nd_Branch.Player;
public class CoreGame {

public final static int TILE_SIZE = 64;

private static Player player;   
private static Farmer farmer;   

public CoreGame()
{

    farmer = new Farmer();
}

public void input()
{

}

public void logic()
{

}

public void render()
{

    farmer.render();
}

public void dispose()
{


}

}

Window.java:

package _1st_Branch;


import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;


import static org.lwjgl.opengl.GL11.*;


public class Window {

public static void create(int width, int height)
{
    try 
    {
        Display.setDisplayMode(new DisplayMode(width, height));
        Display.setTitle("Artyas RTS");
        Display.create();
        initGL();
        initInput();

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


public static void initGL()
{
    glClearColor(0.5f, 0.5f, 1, 1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glLoadIdentity();

}

private static void initInput()
{
    try 
    {
        Keyboard.create();
    } 
    catch (LWJGLException e) 
    {
        e.printStackTrace();
    }
}

public static void Clear()
{
    glClear(GL_COLOR_BUFFER_BIT);
}

public static void Destroy()
{
    Keyboard.destroy();
    Display.destroy();
    System.exit(0);
}

public static void update()
{
    Display.update();
    Display.sync(60);
}

public static boolean isCloseRequested()
{
    return Display.isCloseRequested();
}

}

Farmer.java:

//CIVILIAN UNIT
package _2nd_Branch;


import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Farmer {


public Farmer() {};
private Texture texture;

public void init() {



    try {

    // load texture from PNG file

    texture = TextureLoader.getTexture("PNG",     ResourceLoader.getResourceAsStream("Farmer.png"));


    } catch (IOException e) {

    e.printStackTrace();

    }

    }

public void render()
{
    Color.white.bind();

    texture.bind(); 
    GL11.glEnable(GL11.GL_TEXTURE_2D);  
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
    GL11.glBegin(GL11.GL_QUADS);

    GL11.glTexCoord2f(0,0);
    GL11.glVertex2f(400,500);
    GL11.glTexCoord2f(1,0);
    GL11.glVertex2f(450,500);
    GL11.glTexCoord2f(1,1);
    GL11.glVertex2f(450, 550);
    GL11.glTexCoord2f(0,1);
    GL11.glVertex2f(400, 550);

    GL11.glEnd();
}


}

I am literally stuck here. If someone could indicate what needs to be done, I will be eternally grateful! I really need to get going on making this project, to learn more about Java.

The texture.bind() function from the Farmer class gives me the error.

Being stuck here, I don't know what it says about me as a programmer.


Solution

  • You never call Farmer.init() which loads the texture. You only call the constructor in CoreGame. Either you should call init() from inside the Farmer constructor or call init after calling the constructor in CoreGame. I recommend calling init() from CoreGame, and continue to keep texture loading away from the constructor simply because if you initialize and have to call the constructor to Farmer before you have created your display and initialized OpenGL (Window.initGL()) you don't want to load the texture. Loading a texture with slick before the display is created and opengl is initialized can cause a NullpointerException in TextureLoader.