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

Thread: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

  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" at Map.getMap(Map.java:48)

    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.setLocationRelativeTo(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
    import java.util.*;
    import java.awt.*;
    import java.io.*;
     
    import javax.swing.ImageIcon;
     
    public class Map
    {
     
    	private Scanner m;
    	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.ja
    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;
    	}
    }


    Error Report
    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)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Map.getMap(Map.java:48)
    	at Board.paint(Board.java:42)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
    	at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
    	at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    	at javax.swing.JComponent.paint(JComponent.java:1040)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    	at java.awt.Container.paint(Container.java:1967)
    	at java.awt.Window.paint(Window.java:3877)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    	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)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Map.getMap(Map.java:48)
    	at Board.paint(Board.java:42)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paint(JComponent.java:1063)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    	at javax.swing.JComponent.paintChildren(JComponent.java:887)
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
    	at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
    	at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    	at javax.swing.JComponent.paint(JComponent.java:1040)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    	at java.awt.Container.paint(Container.java:1967)
    	at java.awt.Window.paint(Window.java:3877)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    	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 curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    Your Map array never gets filled. That's likely because the Map class has no constructor. Give it a valid constructor and this problem may well be fixed.

    Remember that constructors have no return type and that Java is case sensitive, in other words map != Map.

    Also you should edit your code so that your identifier naming conforms with Java naming standards: class names begin with upper case letters while variable and method names begin with lower case letters. Else your code will be very difficult to read and understand.

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

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    i added this as the constructer

    public 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();
     
    	}


    but im still gettting errors
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Scanner.java:907)
    	at java.util.Scanner.next(Scanner.java:1416)
    	at Map.readFile(Map.java:68)
    	at Map.<init>(Map.java:27)
    	at Board.<init>(Board.java:19)
    	at Maze.main(Maze.java:9)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	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)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at Map.readFile(Map.java:68)
    At line 68 the code calls the Scanner class's next() method. The method throws: NoSuchElementException
    Read the API doc for a description of what that means.

    Basically you need to test if there is something to be read before trying to read it. The Scanner class has some methods: (names begin with has) that will tell you if there is something available to read so you can skip calling next() when there is no more to read.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    Quote Originally Posted by Norm View Post
    At line 68 the code calls the Scanner class's next() method. The method throws: NoSuchElementException
    Read the API doc for a description of what that means.

    Basically you need to test if there is something to be read before trying to read it. The Scanner class has some methods: (names begin with has) that will tell you if there is something available to read so you can skip calling next() when there is no more to read.
    I don't understand how to put this method into my code. im still a beginner. If you could tell me where it would go into the code and/or what to put. Thanks!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    Before calling next() you need to make sure there is data to be read. Call the hasNext method first to determine if there is anything to read. If hasNext() returns false, do not call the next() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    Quote Originally Posted by Norm View Post
    Before calling next() you need to make sure there is data to be read. Call the hasNext method first to determine if there is anything to read. If hasNext() returns false, do not call the next() method.
    i have added has next so it looks like this :
    public void readFile()
    	{
    		while(m.hasNext())
    		{
    			for(int i = 0; i < 14; i++)
    			{
    				Map[i] = m.next();
    			}
    		}
    	}

    but i get the same error i think i have not implemented it correctly if you know how to fix this then please help me thx

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    You must call hasNext() for each time that you call next(). The posted code only calls it once before the inner loop calls it 14 times.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    How do i write that in code??

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" at Map.getMap(Map.java:48)

    Some thing like this:
    while(input.hasNext() {
      data = input.next();
     // more stuff here
    }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM