Java Game engine Problem



  • 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



  • @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



  • @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.



  • I Only have one question

    How would I get this to work



  • I would need to see the GameContainer class.



  • 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;
    	}
    }


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



  • This post is deleted!


  • This post is deleted!




  • @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


Log in to reply