Results 1 to 6 of 6
  1. #1
    ruben381 is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default 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.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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...

  3. #3
    ruben381 is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Two dimensional game help. Crashing on player render.

    Would you care to extend upon that? I'm not quite sure what you mean.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,329
    Rep Power
    25

    Default Re: Two dimensional game help. Crashing on player render.

    Exception in thread "main" java.lang.NullPointerException
    at src.Player.Render(Player.java:57)
    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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. 2 player java game
    By nuubik in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 02:05 PM
  2. Platform Game |Player Not Showing Up|
    By petur170 in forum New To Java
    Replies: 16
    Last Post: 06-28-2011, 05:16 PM
  3. Rpg game player wont move help please
    By askumi in forum New To Java
    Replies: 19
    Last Post: 12-22-2010, 02:21 AM
  4. How to add online two player to a Java game?
    By michealsmith in forum Java Gaming
    Replies: 1
    Last Post: 11-30-2010, 07:43 PM
  5. how to log into a game with my player.java class
    By basketball8533 in forum New To Java
    Replies: 11
    Last Post: 10-13-2010, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •