2 Replies - 260 Views - Last Post: 01 August 2013 - 07:11 AM Rate Topic: -----

#1 vinnitro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-July 13

AWT-EventQueue-0 NullPointerException why this is showing

Posted 01 August 2013 - 06:19 AM

I am currently developing a Car Racing game in java as my project.

For that I am referring this tutorial : https://www.youtube....h?v=oadONzRVXN4

This is the code of file VRacers.java which actually does everything such as moving car around.
The next thing I am trying to implement as per the tutorial is moving the opponent car.
I have followed everything right & didn't missed anything as I think.
This has started showing up after I added the Timer tm & tm.start().
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class VRacers1 extends DoubleBuffer implements KeyListener, ActionListener
{
	Timer tm = new Timer(5, this);
	private Rectangle player;
	private Rectangle opponent;
	private Image back;
	private Image CarCurrent;
	private Image CarN, CarE, CarW, CarS;
	private int nx = 800;
	private int ny = 0;
	private Rectangle topLeft, bottomLeft, topRight, bottomRight;
	int carX = 0;
	int carY = 0;
	int pSpeed = 24;
	int oSpeed = 20;

	public VRacers1()
	{
		tm.start();
		back = new ImageIcon("FRC new2.png").getImage();
		CarCurrent = new ImageIcon("BlueR//BlueRE.png").getImage(); 
		CarN = new ImageIcon("Blue//BlueRN.png").getImage();
		CarE = new ImageIcon("Blue//BlueRE.png").getImage();
		CarW = new ImageIcon("Blue//BlueRW.png").getImage();
		CarS =new ImageIcon("Blue//BlueRS.png").getImage(); 

		player = new Rectangle(390, 30, 50, 50);
		opponent = new Rectangle(390, 99, 50, 50);
		
		topLeft = new Rectangle(20, 60, 220, 20);
		bottomLeft = new Rectangle(60, 1440, 20, 220);
		bottomRight = new Rectangle(1920, 1600, 220, 20);
		topRight = new Rectangle(2080, 20, 20, 220);

		this.addKeyListener(this);
	}

	public void paintBuffer(Graphics g)
	{
  		g.setColor(Color.green);
		g.fillRect(0, 0, 34567, 34567);
		g.drawImage(back, 800-nx, ny, null);

		Graphics2D g1 = (Graphics2D) g;
		g1.drawImage(CarCurrent, player.x, player.y, null);

		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.RED);
		g2.fill(opponent);
	}

	public void keyTyped(KeyEvent e)	{	}

	public void keyPressed(KeyEvent e)
	{

		if( e.getKeyCode() == KeyEvent.VK_RIGHT )
		{
			nx = nx + pSpeed;
			CarCurrent = new ImageIcon("BlueR\\BlueRE.png").getImage();
			topLeft.setLocation(topLeft.x - pSpeed, topLeft.y); 
			bottomLeft.setLocation(bottomLeft.x - pSpeed, bottomLeft.y); 
			bottomRight.setLocation(bottomRight.x - pSpeed, bottomRight.y); 
			topRight.setLocation(topRight.x - pSpeed, topRight.y); 
			opponent.setLocation(opponent.x - pSpeed, opponent.y);
		}

		if( e.getKeyCode() == KeyEvent.VK_LEFT )
		{
			nx = nx - pSpeed;
			CarCurrent = new ImageIcon("BlueR\\BlueRW.png").getImage();
			topLeft.setLocation(topLeft.x + pSpeed, topLeft.y); 
			bottomLeft.setLocation(bottomLeft.x + pSpeed, bottomLeft.y); 
			bottomRight.setLocation(bottomRight.x + pSpeed, bottomRight.y); 
			topRight.setLocation(topRight.x + pSpeed, topRight.y); 
			opponent.setLocation(opponent.x + pSpeed, opponent.y);
		}

		if( e.getKeyCode() == KeyEvent.VK_UP )
		{
			ny = ny + pSpeed;
			CarCurrent = new ImageIcon("BlueR\\BlueRN.png").getImage();
			topLeft.setLocation(topLeft.x, topLeft.y + pSpeed); 
			bottomLeft.setLocation(bottomLeft.x, bottomLeft.y + pSpeed); 
			bottomRight.setLocation(bottomRight.x, bottomRight.y + pSpeed); 
			topRight.setLocation(topRight.x, topRight.y + pSpeed); 
			opponent.setLocation(opponent.x, opponent.y + pSpeed);
		}

		if( e.getKeyCode() == KeyEvent.VK_DOWN )
		{
			ny = ny - pSpeed;
			CarCurrent = new ImageIcon("BlueR\\BlueRS.png").getImage();
			topLeft.setLocation(topLeft.x, topLeft.y - pSpeed);
			bottomLeft.setLocation(bottomLeft.x, bottomLeft.y - pSpeed); 
			bottomRight.setLocation(bottomRight.x, bottomRight.y - pSpeed); 
			topRight.setLocation(topRight.x, topRight.y - pSpeed); 
			opponent.setLocation(opponent.x, opponent.y - pSpeed);		}

		repaint();
	}

	public void keyReleased(KeyEvent e)	{	}

	public void actionPerformed(ActionEvent e)
	{
		carY = opponent.x + oSpeed;
		carX = opponent.y + oSpeed;

		opponent.setLocation(carX, opponent.y);

		repaint();
	}
}


I have also used a Double Buffering tutorial from a website which I used to create a DoubleBuffer class which extends Panel
This is the code of DoubleBuffer.java
import java.awt.*;

public class DoubleBuffer extends Panel
{
	private int bufferWidth;
	private int bufferHeight;
	private Image bufferImage;
	private Graphics bufferGraphics;

	public DoubleBuffer()
	{
		super();
	}

	public void paint(Graphics g)
	{
		if(bufferWidth != getSize().width || bufferHeight != getSize().height || bufferImage == null || bufferGraphics == null)
		{
			resetBuffer();
		}

		if(bufferGraphics != null)
		{
			//this clears the offscreen image, not the onscreen one
			bufferGraphics.clearRect(0, 0, bufferWidth, bufferHeight);

			//class paintbuffer method with offscreen graphics as a param
			paintBuffer(bufferGraphics);

			//we finally paint the offscreen image onto the onscreen image
			g.drawImage(bufferImage, 0, 0, this);
		}
	}

	private void resetBuffer()
	{
		//always keep track of image size
		bufferWidth = getSize().width;
		bufferHeight = getSize().height;

		//clean up previous image
		if(bufferGraphics != null)
		{
			bufferGraphics.dispose();
			bufferGraphics = null;
		}

		if(bufferImage != null)
		{
			bufferImage.flush();
			bufferImage = null;
		}

		System.gc();

		//create new image with the size of panel
		bufferImage = createImage(bufferWidth, bufferHeight);
		bufferGraphics = bufferImage.getGraphics();
	}

	public void paintBuffer(Graphics g)
	{
	}
}


This is the MainFrame.java which loads the VRacers.java on the JFrame
import java.awt.*;
import javax.swing.*;

public class MainFrame
{

	public MainFrame()
	{
		f = new JFrame("VRacers");
		VRacers1 v=new VRacers1();
		f.add(v);

		f.setSize(800, 600);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String []args)
	{
		new MainFrame();	
	}
}


As per the tutorial I have to extend my class with an Applet but as I found out I cannot call an Applet from the JFrame I switched to using Panel of which I can create an object & call whenever I want with one click on any button.

The problem actually occured when I added the Timer.
This is what is showing on the Console:

Quote

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at VRacers1.actionPerformed(VRacers1.java:112)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: AWT-EventQueue-0 NullPointerException why this is showing

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,348
  • Joined: 20-September 08

Re: AWT-EventQueue-0 NullPointerException why this is showing

Posted 01 August 2013 - 07:00 AM

Move the initialization of the Timer to the very end of the ctor and then call start on it. this is not properly defined at the point at which you currently initialize it.

This post has been edited by g00se: 01 August 2013 - 07:01 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#3 vinnitro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-July 13

Re: AWT-EventQueue-0 NullPointerException why this is showing

Posted 01 August 2013 - 07:11 AM

Quote

Move the initialization of the Timer to the very end of the ctor and then call start on it. this is not properly defined at the point at which you currently initialize it.


thanks g00se, it is now working fine & the rectangle is also moving.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1