Results 1 to 6 of 6
- 04-29-2012, 11:20 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Two dimensional game help. Crashing on player render.
hey everyone,
I'm requesting help with my game that I'm making for an assignment. The console is telling me that it's a problem with the player (when it tries to render).
So, here's the error:
Exception in thread "main" java.lang.NullPointerException
at src.Player.Render(Player.java:57)
at src.Base.doInput(Base.java:63)
at src.Base.launch(Base.java:111)
at src.Base.main(Base.java:21)
And here's my code:
Base.java
Java Code:package src; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; public class Base { private static boolean running = true; private static Player player = new Player(100, 100, 32, 32); private static TextureHandler texturehandler = new TextureHandler(); public static void main(String[] args) { Base baseclass = new Base(); baseclass.launch(); } public static void init(int length, int height) throws LWJGLException { DisplayMode[] dm = Display.getAvailableDisplayModes(); for(DisplayMode display : dm) { if(display.getWidth() == length && display.getHeight() == height) { Display.setDisplayMode(display); } } Display.setTitle("Gelatinous"); Display.create(); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, -1, 1); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); GL11.glEnable(GL11.GL_ALPHA_TEST); GL11.glAlphaFunc(GL11.GL_GREATER, 0.4f); GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glClearColor(0.6f, 1.0f, 0.5f, 1.0f); texturehandler.Load("player1.png", "player"); } public static void doInput() { if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { running = false; } player.Render(); player.Input(); } public static void doRender() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL11.glLoadIdentity(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturehandler.GetTexture("player").getTextureID()); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2f(100.0f, 100.0f); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex2f(132.0f, 100.0f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex2f(132.0f, 132.0f); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex2f(100.0f, 132.0f); GL11.glEnd(); } public static void doUpdate() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); GL11.glLoadIdentity(); if(player.playerHealth == 0) running = false; } public void launch() { try { init(640, 480); } catch (LWJGLException e) { e.printStackTrace(); } while(running) { if(Display.isCloseRequested()) { running = false; } Display.update(); doInput(); doUpdate(); doRender(); } clean(); } public static void clean() { Display.destroy(); } }
TextureHandler.java
Java Code:package src; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.TextureLoader; public class TextureHandler { private HashMap<String, Texture> textures = new HashMap<String, Texture>(); private static TextureHandler texturehandler = new TextureHandler(); public TextureHandler() { } public static TextureHandler Instance() { return texturehandler; } public boolean Load(String path, String name) { Texture texture; try { if((texture = TextureLoader.getTexture("PNG", new FileInputStream(path))) !=null) { System.out.println(texture); textures.put(name, texture); return true; } else return false; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } public Texture GetTexture(String name) { if(textures.containsKey(name)) { return textures.get(name); } else return null; } }
Player.java
Java Code:package src; import java.nio.FloatBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; public class Player { private float x, y; private int w, h; private static TextureHandler texturehandler = new TextureHandler(); public Player(float X, float Y, int W, int H) { x = X; y = Y; w = W; h = H; } public int playerScore; { playerScore = 0; } public int playerHealth; { playerHealth = 100; } public void Input() { if(Keyboard.isKeyDown(Keyboard.KEY_W)) { y -= 1; } else if(Keyboard.isKeyDown(Keyboard.KEY_S)) { y += 1; } else if(Keyboard.isKeyDown(Keyboard.KEY_A)) { x -=1; } else if(Keyboard.isKeyDown(Keyboard.KEY_D)) { x += 1; } else if(Keyboard.isKeyDown(Keyboard.KEY_TAB)) { System.out.println(playerScore); System.out.println(playerHealth); } } public void Render() { GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureHandler.Instance().GetTexture("player").getTextureID()); GL11.glTranslatef(x, y, 0.0f); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2f(0.0f, 0.0f); GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex2f(w, 0.0f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex2f(w, h); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex2f(0.0f, h); GL11.glEnd(); GL11.glTranslatef(-x, -y, 0.0f); } }
Any help is appreciated.
- 04-29-2012, 11:24 AM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Two dimensional game help. Crashing on player render.
TextureHandler.Instance().GetTexture("player").get TextureID()
Check if the Texture is found before you use it and check even before if the TextureHandler instance is available! One of those is the error probably...
- 04-29-2012, 11:39 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Two dimensional game help. Crashing on player render.
Would you care to extend upon that? I'm not quite sure what you mean.
- 04-29-2012, 01:32 PM #4
Re: Two dimensional game help. Crashing on player render.
One of the methods called on line 57 could be returning a null value. Add a println just before line 57 that prints out all the variables and method calls so you can see which one is null.Exception in thread "main" java.lang.NullPointerException
at src.Player.Render(Player.java:57)If you don't understand my response, don't ignore it, ask a question.
- 04-29-2012, 02:10 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Two dimensional game help. Crashing on player render.
TextureHandler.Instance() result could be "null" - so you cannot access it and would get this error, as Norm suggests you may print the outputs for debugging or use a debugger (e.g. in eclipse start with F11)
further the same may happen at TextureHandler.Instance().GetTexture("player") if the texture "player" does not exist for example. Then you cannot call the getTextureID()... got it?
- 04-29-2012, 03:05 PM #6
Re: Two dimensional game help. Crashing on player render.
Quote apart from any problems with Exceptions, you really need to learn and follow the Java coding conventions. Method names start with a lowercase letter.
Code Conventions for the Java Programming Language: Contents
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
2 player java game
By nuubik in forum New To JavaReplies: 1Last Post: 11-07-2011, 02:05 PM -
Platform Game |Player Not Showing Up|
By petur170 in forum New To JavaReplies: 16Last Post: 06-28-2011, 05:16 PM -
Rpg game player wont move help please
By askumi in forum New To JavaReplies: 19Last Post: 12-22-2010, 02:21 AM -
How to add online two player to a Java game?
By michealsmith in forum Java GamingReplies: 1Last Post: 11-30-2010, 07:43 PM -
how to log into a game with my player.java class
By basketball8533 in forum New To JavaReplies: 11Last Post: 10-13-2010, 12:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks