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 3 of 3

Thread: Exception in thread "AWT-EventQueue-0" and Exception in thread "main"

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "AWT-EventQueue-0" and Exception in thread "main"

    Maze.java
    import javax.swing.*;
     
    public class Maze
    {
    	public static void main(String[] args)
      {
    	 JFrame mainframe = new JFrame();
    	 mainframe.add(new Board());
    	 mainframe.setTitle("Noah's Game");
    	 mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 mainframe.setSize(512,512);
    	 mainframe.setLocation(null);
    	 mainframe.setVisible(true);
     
      }
     
    }



    Board.java
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;
    import java.*;
     
    public class Board extends JPanel implements ActionListener {
    	private Map m;
    	private Timer timer;
    	private Player p;
    	private String Message = "Winner!";
    	private boolean Win = false;
    	private Font font = new Font("Serif", Font.BOLD,48);
     
    	public Board() {
    		timer = new Timer(25, this);
    		timer.start();
    		m = new Map();
    		p = new Player();
    		addKeyListener(new Al());
    		setFocusable(true);
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		repaint();
    		if(m.getMap(p.getTileX(), p.getTileY()).equals("f"))
    		{
    			Message = "Winner!";
    			Win = true;
    		}
    	}
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		if(!Win){
    		for (int y = 0; y < 16; y++) {
    			for (int x = 0; x < 16; x++) {
     
    				if (m.getMap(x, y).equals("f")) {
    					g.drawImage(m.getFinish(), x * 32, y * 32, null);
    				}
    				if (m.getMap(x, y).equals("g")) {
    					g.drawImage(m.getDirt(), x * 32, y * 32, null);
    				}
     
    				if (m.getMap(x, y).equals("w")) {
    					g.drawImage(m.getWall(), x * 32, y * 32, null);
    				}
    			}
    		}
    		g.drawImage(p.getPlayer(), p.getTileX() * 32, p.getTileY() * 32, null);
    		}
     
    		if(Win){
    		g.setColor(Color.MAGENTA);
    		g.setFont(font);
    		g.drawString(Message, 150, 300);
     
    		}
     
     
     
    	}
     
    	public class Al extends KeyAdapter {
    		public void keyPresed(KeyEvent e) {
     
    			if (e.getKeyCode() == KeyEvent.VK_W) {
    				if (!m.getMap(p.getTileX(), p.getTileY() - 1).equals("w"))
    					;
    				p.move(0, -1);
     
    			}
     
    			if (e.getKeyCode() == KeyEvent.VK_A) {
    				if (!m.getMap(p.getTileX(), p.getTileY() + 1).equals("w"))
    					;
    				p.move(0, 1);
     
    			}
     
    			if (e.getKeyCode() == KeyEvent.VK_S) {
    				if (!m.getMap(p.getTileX() - 1, p.getTileY()).equals("w"))
    					;
    				p.move(-1, 0);
     
    			}
     
    			if (e.getKeyCode() == KeyEvent.VK_D) {
    				if (!m.getMap(p.getTileX() + 1, p.getTileY()).equals("w"))
    					;
    				p.move(1, 0);
     
    			}
     
    		}
     
    		public void keyReleased(KeyEvent e) {
     
    		}
     
    		public void keyTyped(KeyEvent e) {
     
    		}
     
    	}
    }



    Map.java
    private String 	Map[] = new String[16];
    	private Image dirt ,
    	              wall,
    	             finish;
     
    	public void map()
    	{
    		ImageIcon img = new ImageIcon("C://Noah's World//dirt.png");
    		dirt = img.getImage();
    		img = new ImageIcon("C://Noah's World//wall.png");
    		wall = img.getImage();
    		img = new ImageIcon("C://Noah's World//finish.png");
    		finish = img.getImage();
     
     
    		openFile();
    		readFile();
    		closeFile();
     
    	}
     
    	public Image getFinish()
    	{
    		return finish;
    	}
    	public Image getDirt()
    	{
    		return dirt;
    	}
    	public Image getWall()
    	{
    		return wall;
    	}
     
     
    	public String getMap(int x, int y)
    	{
    			String index = Map[y].substring(x,x + 1);
    			return index;
    	}
     
    	public void openFile()
    	{
    		try{
    		m = new Scanner(new File("C://Noah's World//Map.txt"));
    		}catch(Exception e)
    		{
    			System.out.println("error loading map");
    		}
    	}
     
    	public void readFile()
    	{
    		while(m.hasNext())
    		{
    			for(int i = 0; i < 14; i++)
    			{
    				Map[i] = m.next();
    			}
    		}
    	}
     
    	public void closeFile()
    	{
    		m.close();
    	}
    }



    Player.java
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
     
    public class Player {
    	private int tileX, tileY;
    	private Image player;
     
    	public Player() {
     
    		ImageIcon img = new ImageIcon("C://Noah's World//Player.png");
    		player = img.getImage();
     
    		tileX = 1;
    		tileY = 1;
    	}
     
    	public Image getPlayer() {
    		return player;
    	}
     
    	public int getTileX() {
    		return tileX;
    	}
     
    	public int getTileY() {
    		return tileY;
    	}
     
    	public void move(int dx, int dy) {
     
     
    		tileX += dx;
    		tileY += dy;
    	}
    }


    and here is the error report


    Exception in thread "main" java.lang.NullPointerException
    	at java.awt.Component.setLocation(Component.java:2112)
    	at java.awt.Window.setLocation(Window.java:934)
    	at Maze.main(Maze.java:13)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Map.getMap(Map.java:48)
    	at Board.actionPerformed(Board.java:28)
    	at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    	at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
    	at java.awt.EventQueue.access$000(EventQueue.java:102)
    	at java.awt.EventQueue$3.run(EventQueue.java:662)
    	at java.awt.EventQueue$3.run(EventQueue.java:660)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,960
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" and Exception in thread "main"

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Component.setLocation(Component.java:2112 )
    at java.awt.Window.setLocation(Window.java:934)
    at Maze.main(Maze.java:13)
    Look at line 13 in Maze for a null value. The java program expects a non-null value there.
    Read the API doc to see if you are using the method correctly or if you should use another method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Exception in thread "AWT-EventQueue-0" and Exception in thread "main"

    mainframe.setLocation(null);
    Why? Maybe you mean setLocationRelativeTo(null), which will centre your window. That is your first problem anyhow.
    Your "Map" array is of length 16, 0 - 15, how do you ensure that when you request a substring from the array that the index is valid?

    As for how you name your variables, I suggest reading over Java naming conventions:
    Code Conventions for the Java Programming Language: 9. Naming Conventions
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 04:36 PM
  4. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM