Results 1 to 7 of 7
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default maze game error something about unkown source?

    Hey im following a tutorial to get an idea on games in java and when i paint my map to the jframe it loops through the printing but prints this error message. im thinking the images are not initialized? but im sure they are

    error message it repeats this till i close the JFrame

    Java Code:
    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)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at maze.Map.getMap(Map.java:50)
    	at maze.Board.paintComponent(Board.java:43)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$700(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.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 Source)
    	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)
    Java Code:
    package maze;
    
    import javax.swing.*;
    
    public class Maze {
    	
    	public static void main (String[] args) {
    		new Maze();
    		
    	}
    	
    	public Maze() {
    		JFrame f= new JFrame();
    		f.setTitle("1st Maze game by CJE ver 1.0.0"); //sets frame title
    		f.add(new Board()); //adds a game Board()
    		f.setSize(1000, 800);
    		f.setLocationRelativeTo(null); //sets location, relative refers to a component, but choose null, so it centers
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes jframe on exit and terminates jre
    		
    		
    	}
    
    }
    Java Code:
    package maze;
    
    import java.awt.*; //graphics etc
    import java.awt.event.*; //action listeners etc
    import javax.swing.*;
    
    public class Board extends JPanel implements ActionListener{
    
    	private Timer timer; //used to time events
    	private Map map;
    
    	public Board() {
    
    		map = new Map();
    
    		timer = new Timer(25, this); //25ms between actions every 25ms it will run action performed
    		timer.start();
    	}
    
    
    	/*
    	 * collections actions performed 
    	 * 
    	 */
    
    	public void actionPerformed(ActionEvent e){ // gets the actions performed
    		repaint();
    
    	}
    
    
    	/*
    	 * Graphics imported from awt.*; allows us to use super methods
    	 * Paint methods, allows us to draw stuff to JFrame
    	 * 
    	 */
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g); //uses imported super class of Graphics
    
    		for(int y = 0;y < 14; y ++){ //do rows first y = a row
    			for(int x = 0; x < 14; x++){ // do tiles in row or x's
    				if(map.getMap(x , y).equals("w")){
    					//will draw a water tile *32 because tile is 32x32
    					g.drawImage(map.getWater(), x * 32, y * 32, null); //null observer
    				}
    
    				if(map.getMap(x , y).equals("b")){
    					//will draw a buoy *32 because tile is 32*32
    					g.drawImage(map.getBuoy(), x * 32, y * 32, null); //null observer
    				}
    			}
    
    		}
    	}
    }
    Java Code:
    package maze;
    
    import java.util.*;
    import java.awt.Image; //for images
    import java.io.*;
    import javax.swing.*; // for imageIcon and icon stuff
    
    public class Map {
    
    	private Scanner scan;
    	private String Map[] = new String [16]; //16 refers to amount fo tiles going down, 16 tiles here
    
    
    	private Image water,
    	buoy;
    
    	public Map() {
    
    		//setting images
    
    		ImageIcon img = new ImageIcon("/maze game/src/maze/maps/images/water.png");
    		water = img.getImage();
    		img = new ImageIcon("/maze game/src/maze/maps/images/buoy.png");
    		buoy = img.getImage();
    
    		//end of setting images
    
    		loadMap(); //opens file
    
    	}
    
    	// gets for returning the image for board method calls
    	public Image getWater() {
    		return water;
    	}
    
    	public Image getBuoy() {
    		return buoy;
    	}
    
    	//end gets
    
    	/*
    	 * looks at map for x and y cords, 
    	 * substring = is x position on y "row" 
    	 * 
    	 */
    
    	public String getMap(int x, int y){
    		String index = Map[y].substring(x ,x + 1); //if y = 2, looks in 2 row of map
    		return index;
    	}
    
    	public void loadMap() {
    		try {
    			scan = new Scanner(new File("/maze game/src/maze/maps/map.txt"));
    			while(scan.hasNext()){ //whilst map has a next element
    				for(int i= 0; i <14; i++){ //
    					Map[i] = scan.next(); //loads the found next element into the array i starts at 0, then 1 etc 
    				}
    			}
    			scan.close(); //SIMPLY CLOSES SCANNER
    		}
    		catch(IOException e){
    			System.err.print("error loading map");
    
    		}
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: maze game error something about unkown source?

    Something is null. You're going to have to step through this with a debugger, or at least add some print statements, to figure out exactly what that is. What's going on in the line that throws the exception? What are you dereferencing? Where is it initialized? Something is null in that line.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: maze game error something about unkown source?

    Quote Originally Posted by KevinWorkman View Post
    Something is null. You're going to have to step through this with a debugger, or at least add some print statements, to figure out exactly what that is. What's going on in the line that throws the exception? What are you dereferencing? Where is it initialized? Something is null in that line.
    i was thinking of doing that, what is more likely to be null, one of the graphic elements? since they get " painted" into the Component?

    line that throws the exception is

    Java Code:
    public String getMap(intx, int y) {
    String index =Map[y].substring(x, x+1);
    return index;
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: maze game error something about unkown source?

    What index of Map (which should be map, by the way) are you trying to access when the error is thrown? What is stored at that index?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: maze game error something about unkown source?

    Quote Originally Posted by KevinWorkman View Post
    What index of Map (which should be map, by the way) are you trying to access when the error is thrown? What is stored at that index?
    i seem to have got rid of the errors, but nothing is painted to the Jframe. potentially thinking it does not actually read the file correctly ill further de bug this now

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: maze game error something about unkown source?

    this works fine

    Java Code:
    public void loadMap() {
            try {
                scan = new Scanner(new File("/maze game/src/maze/maps/map.txt"));
                while(scan.hasNext()){ //whilst map has a next element
                    for(int i= 0; i <14; i++){ //
                        Map[i] = scan.next(); //loads the found next element into the array i starts at 0, then 1 etc
                    }
                }
    but i dont think
    Java Code:
    public String getMap(int x, int y) {      INT X AND Y ARE BOTH 0 IN THE DE BUGGER :/ THIS IS THE ISSUE
    
    String index =Map[y].substring(x, x+1);
    return index;
    OR

    Java Code:
    public void paint(Graphics g) {
    		super.paintComponent(g); //uses imported super class of Graphics
    
    		for(int y = 0;y < 14; y ++){ //do rows first y = a row
    			for(int x = 0; x < 14; x++){ // do tiles in row or x's
    				if(map.getMap(x , y).equals("w")){
    					//will draw a water tile *32 because tile is 32x32
    					g.drawImage(map.getWater(), x * 32, y * 32, null); //null observer
    				}
    
    				if(map.getMap(x , y).equals("b")){
    					//will draw a buoy *32 because tile is 32*32
    					g.drawImage(map.getBuoy(), x * 32, y * 32, null); //null observer
    				}
    			}
    
    		}
    	}
    are working hmmmm...
    Last edited by monkeyjr97; 02-13-2013 at 08:06 PM.

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: maze game error something about unkown source?

    everything seems to read and count okay, but when it reads a "w" nothing is painted, and when it reads a "b" nothing is painted.

Similar Threads

  1. Maze Solver Error in Getting instance variable
    By jijiaremere in forum JCreator
    Replies: 3
    Last Post: 02-04-2013, 10:41 AM
  2. Source Not Found compiling error
    By bradjohns94 in forum New To Java
    Replies: 5
    Last Post: 09-03-2012, 12:08 AM
  3. Source not found error even after including it
    By nehatomar in forum Eclipse
    Replies: 0
    Last Post: 06-03-2012, 11:10 PM
  4. Source tree shows error when there is none
    By madroadbiker in forum NetBeans
    Replies: 0
    Last Post: 05-10-2011, 01:29 PM
  5. Replies: 4
    Last Post: 04-23-2010, 05:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •