Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

    Java Game engine Problem

    Java
    2
    10
    38
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Lpokimo
      Lpokimo last edited by avan

      So I have A Error that I cant fix

      Error{
      Exception in thread "main" java.lang.NullPointerException
      	at com.tbge.engine.Input.<init>(Input.java:32)
      	at com.tbge.engine.GameContainer.start(GameContainer.java:26)
      	at com.tbge.engine.GameContainer.main(GameContainer.java:124)
      }
      
      Line 32{
      	public Input(GameContainer gc) {
              this.gc = gc;
              this.mouseX = 0;
              this.mouseY = 0;
              this.scroll = 0;
      
              gc.getWindow().getCanvas().addKeyListener(this); ( And THIS)
              gc.getWindow().getCanvas().addMouseListener(this); (THIS)
              gc.getWindow().getCanvas().addMouseMotionListener(this); (THIS)
              gc.getWindow().getCanvas().addMouseWheelListener(this);  (THIS)
      
      
              for (int i = 0; i < keys.length; i++) {
                  keys[i] = false;
              }
      
      		
      		for(int i  = 0; i < NUM_BUTTONS; i++) {
      			buttonslast[i] = buttons[i];
      		}
      
      }
      
      Line 26{
      	public void start() 
      	{
      		input = new Input(null); (This is the problem )
      		window = new Window(this);
      		renderer = new Renderer(this);
      		
      		thread = new Thread(this);
      		thread.run();
      	}
      }
      
      Line 124{
      
      	public static void main(String args[])
      	{
      		GameContainer gc = new GameContainer();
      		gc.start(); (This is the problem)
      	}
      
      }
      

      If you can Help Thank you

      Reply Quote 0
        1 Reply Last reply

      • avan
        avan last edited by

        @Lpokimo said in Java Game engine Problem:

        input = new Input(null); (This

        In your version you have

        input = new Input(null);
        

        in original version it is correct

        input = new Input(this);
        

        Line 26 in GameContainer.java file

        Reply Quote 0
          1 Reply Last reply

        • avan
          avan last edited by

          @Lpokimo said in Java Game engine Problem:

          public Input(GameContainer gc) {

          public Input(GameContainer gc) {
          

          You provide it with a null argument so

          gc.getWindow().getCanvas().addKeyListener(this); ( And THIS)
          

          or any other reference to gc will produce an error because gc is null and therefore you cannot reference any methods or attributes.

          I hope that makes sense. Please let me know.

          Reply Quote 0
            1 Reply Last reply

          • Lpokimo
            Lpokimo last edited by

            I Only have one question

            How would I get this to work

            Reply Quote 1
              1 Reply Last reply

            • avan
              avan last edited by

              I would need to see the GameContainer class.

              Reply Quote 0
                1 Reply Last reply

              • Lpokimo
                Lpokimo last edited by avan

                package com.tbge.engine;
                
                import java.awt.event.KeyEvent;
                
                public class GameContainer implements Runnable
                {
                	private Thread thread;
                	private Window window;
                	private Renderer renderer;
                	private Input input;
                	private boolean running = false;
                	private final double UPDATE_CAP = 1.0/60;
                	private int width = 320, height = 240;
                	private float scale = 3f;
                	private String title = "TBGE Alpha 1";
                	
                	
                	
                	public GameContainer()
                	{	
                	
                	}
                	
                	public void start() 
                	{
                		input = new Input(this);
                		window = new Window(this);
                		renderer = new Renderer(this);
                		
                		thread = new Thread(this);
                		thread.run();
                	}
                	
                	public void stop()
                	{
                		
                	}
                	
                	public void run()
                	{
                		running = true;
                		
                		boolean render = false;
                		double firstTime = 0;
                		double lastTime = System.nanoTime() / 1000000000.0;
                		double passedTime = 0;
                		double unprocessedTime = 0;
                		
                		double frameTime = 0;
                		int frames = 0;
                		@SuppressWarnings("unused")
                		int fps = 0;
                		
                		while(running)
                		{
                			render = false;
                			
                			firstTime = System.nanoTime() / 1000000000.0;
                			passedTime = firstTime - lastTime;
                			lastTime = firstTime;
                			
                			unprocessedTime += passedTime;
                			frameTime += passedTime;
                			
                			while(unprocessedTime >= UPDATE_CAP) {
                				
                				unprocessedTime -= UPDATE_CAP;
                				render = true;
                				
                				
                				//TODO Update Game
                				if(input.isKey(KeyEvent.VK_A))
                				{
                					System.out.println("YOU PRESSED A");
                				}
                				
                				
                				
                				if(frameTime >= 1.0)
                				{
                					frameTime = 0;
                					fps = frames;
                					frames = 0;
                					
                					
                					
                				}
                			}
                			
                			if(render)
                			{
                				renderer.clear();
                				//TODO render game
                				
                				
                				window.update();
                				frames++;
                			}
                			
                			else
                			{
                			try {
                					Thread.sleep(1);
                				} 
                				catch (InterruptedException e) 
                				{
                					e.printStackTrace();
                				}
                			}
                		}
                		
                		dispose();
                	}
                	
                	@SuppressWarnings("unused")
                	private void dispose()
                	{
                		
                	}
                	
                	public static void main(String args[])
                	{
                		GameContainer gc = new GameContainer();
                		gc.start();
                	}
                
                	public int getWidth() {
                		return width;
                	}
                
                	public void setWidth(int width) {
                		this.width = width;
                	}
                
                	public int getHeight() {
                		return height;
                	}
                
                	public void setHeight(int height) {
                		this.height = height;
                	}
                
                	public float getScale() {
                		return scale;
                	}
                
                	public void setScale(float scale) {
                		this.scale = scale;
                	}
                
                	public String getTitle() {
                		return title;
                	}
                
                	public void setTitle(String title) {
                		this.title = title;
                	}
                
                	public Window getWindow() {
                		return window;
                	}
                }
                Reply Quote 0
                  1 Reply Last reply

                • avan
                  avan last edited by

                  Hey @Lpokimo

                  I would love to help however I am afraid I would need all and complete java classes in order to help. I am trying to replicate the problem on my computer.

                  Reply Quote 0
                    1 Reply Last reply

                  • Lpokimo
                    Lpokimo last edited by

                    This post is deleted!
                    Reply Quote 0
                      1 Reply Last reply

                    • Lpokimo
                      Lpokimo last edited by

                      This post is deleted!
                      Reply Quote 0
                        1 Reply Last reply

                      • Lpokimo
                        Lpokimo last edited by

                        I found it

                        Reply Quote 0
                          1 Reply Last reply

                        • avan
                          avan last edited by

                          @Lpokimo said in Java Game engine Problem:

                          input = new Input(null); (This

                          In your version you have

                          input = new Input(null);
                          

                          in original version it is correct

                          input = new Input(this);
                          

                          Line 26 in GameContainer.java file

                          Reply Quote 0
                            1 Reply Last reply

                          • First post
                            Last post