Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Making A 2D Game Engine But There Is A NullPointerException From Input

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Location
    GB
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Making A 2D Game Engine But There Is A NullPointerException From Input

    Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.chara.engine.Window.getCanvas()" because the return value of "com.chara.engine.GameContainer.getWindow()" is null
    at module.chara/com.chara.engine.Input.<init>(Input.java:32)
    at module.chara/com.chara.engine.GameContainer.start(GameContainer .java:25)
    at module.chara/com.chara.engine.GameContainer.main(GameContainer. java:118)

    MainComponent.java
    package com.chara.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.0;
    	private int width = 800, height = 600;
    	private float scale = 1f;
    	private String title = "Chara Engine v1.0";
     
    	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;
    		float firstTime = 0;
    		float lastTime = System.nanoTime() / 1000000000f;
    		double passedTime = 0;
    		double unprocessedTime = 0;
     
    		double frameTime = 0;
    		int frames = 0;
    		int fps = 0;
     
    		while(running)
    		{
    			render = false;
     
    			firstTime = System.nanoTime() / 1000000000.0f;	
    			passedTime = firstTime - lastTime;
    			lastTime = firstTime;
     
    			unprocessedTime += passedTime;
    			frameTime += passedTime;
     
    			while(unprocessedTime >= UPDATE_CAP)
    			{
     
     
    				unprocessedTime -= UPDATE_CAP;
     
    				render = true;
     
    				if(frameTime >= 1.0) 
    				{
    					frameTime = 0;
    					fps = frames;
     
    					frames = 0;
     
    					System.out.println("FPS:"+ fps);
    				}
    				//TODO: Update game
    				if(input.isKey(KeyEvent.VK_A))
    				{
    					System.out.println("A");
    				}
     
    				input.update();
     
    			}
     
    			if(render)
    			{
    				renderer.clear();
    				window.update();
    				frames++;
    				//TODO: Render game
    			}else
    			{
    				try 
    				{
    					Thread.sleep(1);
    				} catch (InterruptedException e) 
    				{
    					e.printStackTrace();
    				}
    			}
    		}
    		dispose();
     
    	}
     
    	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;
    	}
     
    	public Input getInput() {
    		return input;
    	}
    }

    Input.java
    package com.chara.engine;
     
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
     
    public class Input implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener
    {
    	private GameContainer gc;
     
    	private final int NUM_KEYS = 256;
    	private final int NUM_BUTTONS = 5;
    	private boolean[] keys = new boolean[NUM_KEYS];
    	private boolean[] keysLast = new boolean[NUM_KEYS];
    	private boolean[] buttons = new boolean[NUM_BUTTONS];
    	private boolean[] buttonsLast = new boolean[NUM_BUTTONS];
     
    	private int mouseX, mouseY;
    	private int scroll;
     
    	public Input(GameContainer gc)
    	{
    		this.gc = gc;
    		mouseX = 0;
    		mouseY = 0;
    		scroll = 0;
     
    		gc.getWindow().getCanvas().addKeyListener(this);
    		gc.getWindow().getCanvas().addMouseMotionListener(this);
    		gc.getWindow().getCanvas().addMouseListener(this);
    		gc.getWindow().getCanvas().addMouseWheelListener(this);
    	}
     
    	public void update()
    	{
    		for(int i = 0; i < NUM_KEYS; i++)
    		{
    			keysLast[i] = keys[i];
    		}
    		for(int i = 0; i < NUM_BUTTONS; i++)
    		{
    			buttonsLast[i] = buttons[i];
    		}
    	}
     
    	public boolean isKey(int keyCode)
    	{
    		return keys[keyCode];
    	}
    	public boolean isKeyUp(int keyCode)
    	{
    		return !keys[keyCode] && keysLast[keyCode];
    	}
    	public boolean isKeyDown(int keyCode)
    	{
    		return keys[keyCode] && !keysLast[keyCode];
    	}
     
    	public boolean isButton(int button)
    	{
    		return buttons[button];
    	}
    	public boolean isButtonUp(int button)
    	{
    		return !buttons[button] && buttonsLast[button];
    	}
    	public boolean isButtonDown(int button)
    	{
    		return buttons[button] && !buttonsLast[button];
    	}
     
    	@Override
    	public void mouseWheelMoved(MouseWheelEvent e) 
    	{
    		scroll = e.getWheelRotation();
    	}
     
    	@Override
    	public void mouseDragged(MouseEvent e) 
    	{
    		mouseX = (int)(e.getX() / gc.getScale());
    		mouseY = (int)(e.getY() / gc.getScale());
    	}
     
    	@Override
    	public void mouseMoved(MouseEvent e) 
    	{
    		mouseX = (int)(e.getX() / gc.getScale());
    		mouseY = (int)(e.getY() / gc.getScale());
    	}
     
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mousePressed(MouseEvent e) 
    	{
    		buttons[e.getButton()] = true;
    	}
     
    	@Override
    	public void mouseReleased(MouseEvent e) 
    	{
    		buttons[e.getButton()] = false;
    	}
     
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyPressed(KeyEvent e) 
    	{
    		keys[e.getKeyCode()] = true;
    	}
     
    	@Override
    	public void keyReleased(KeyEvent e) 
    	{
    		keys[e.getKeyCode()] = false;
    	}
     
    	public boolean[] getKeys() {
    		return keys;
    	}
     
    	public boolean[] getButtons() {
    		return buttons;
    	}
     
    	public int getMouseX() {
    		return mouseX;
    	}
     
    	public int getMouseY() {
    		return mouseY;
    	}
     
    	public int getScroll() {
    		return scroll;
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making A 2D Game Engine But There Is A NullPointerException From Input

    The posted code does not compile for testing because there are missing classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Making A 2D Game Engine But It Getting 0 FPS Help!
    By TrazerX in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 24th, 2021, 06:50 AM
  2. How To Add Communication From My GUI To My Game Engine
    By LarrySellers in forum AWT / Java Swing
    Replies: 1
    Last Post: September 26th, 2014, 02:58 AM
  3. Programming a game engine?
    By Tea.EarlGrey.Hot. in forum Java Theory & Questions
    Replies: 4
    Last Post: May 21st, 2014, 08:41 AM
  4. what are game engine
    By game06 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 17th, 2013, 05:05 AM
  5. Making custom search engine
    By bochra in forum Web Frameworks
    Replies: 4
    Last Post: June 20th, 2011, 07:37 AM

Tags for this Thread