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

Thread: JFrame errors

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JFrame errors

    I have been working on a program to run a basic 2 dimensional sidescrolling game. Right now I am trying to get the basic image window working. I was able to get a simple version to run, but now that I have developed a more complicated code, I am getting errors when I try to run the program. Here is the bit of code for the main program (I can give the constructors for environment() class if needed).


    import java.awt.BorderLayout;
     
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
     
    public class gameTester extends JFrame {
    	private KeyboardPanel keyboardPanel = new KeyboardPanel();
    	private static int charPoint[] = {30, 120};
    	private static int bgPoint[] = {0, -300};
    	private static environment[] environment;
     
     
    	public static int[] getCharPoint() {
    		return charPoint;
    	}
    	public static void setCharPoint(int[] charPoint) {
    		gameTester.charPoint = charPoint;
    	}
    	public static int[] getBgPoint() {
    		return bgPoint;
    	}
    	public static void setBgPoint(int[] bgPoint) {
    		gameTester.bgPoint = bgPoint;
    	}
    	public static environment[] getEnvironment() {
    		return environment;
    	}
    	public static void setEnvironment(environment[] environment) {
    		gameTester.environment = environment;
    	}
     
    	public static void main(String[] args){
     
    		int[][] layout = getMap0();
     
    		int temp = 0;
     
    		for (int i = 0; i < layout.length; i++){
    			for (int j = 0; j < layout[1].length; j++){
    				if (layout[i][j] == 1){
    					temp++;
    				}
    			}		
    		}
     
    		environment[] tempEnvironment = new environment[temp]; 
    		for (int i = 0; i < layout.length; i++){
    			for (int j = 0; j < layout[1].length; j++){
    				if (layout[i][j] == 1){
    					int point[] = {i * 50, j * 50};
    					tempEnvironment[i] = new environment("basicBlock", point);
    				}
    			}
    		}
     
    		environment = tempEnvironment;
     
     
    		gameTester frame = new gameTester();
    		frame.setTitle("Tester");
    		frame.setSize(400, 300);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	public gameTester(){
    		String vp = "null";
    		setLayout(new BorderLayout(10, 20));
    		add(new AnimatedPanel(vp), BorderLayout.CENTER);
    		add(keyboardPanel, BorderLayout.SOUTH);
    		keyboardPanel.setFocusable(true);
    	}
     
    	static class AnimatedPanel extends JPanel{
     
    		ImageIcon playerIcon = new ImageIcon("Documents/Projects/Photoshop/NinjaRight.gif");
    		Image playerIcon2 = playerIcon.getImage();
     
     
    		ImageIcon background = new ImageIcon("Documents/Projects/Photoshop/backgroundtest.gif");
    		Image background2 = background.getImage();
     
     
    		ImageIcon basicBlock = new ImageIcon("Documents/Projects/Photoshop/block.gif");
    		Image basicBlock2 = basicBlock.getImage();
     
    		public AnimatedPanel(String message){
    			Timer timer = new Timer(62, new TimerListener());
    			timer.start();
    		}
     
    		protected void paintComponent(Graphics g){
    			super.paintComponent(g);
     
     
    			g.drawImage(background2, bgPoint[0], bgPoint[1], 800, 600, this);
     
    			for (int i = 0; i < environment.length; i++){
    				int[] temp = environment[i].getPoint();
    				g.drawImage(basicBlock2, temp[0], temp[1], 50, 50, this);
    			}
     
    			g.drawImage(playerIcon2, charPoint[0], charPoint[1], 90, 50, this);
     
     
    		}
     
    		class TimerListener implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				repaint();
    			}		
    		}
     
    	}
     
    	static class KeyboardPanel extends JPanel{ 
    		private int x;
    		private int y;
    		private int x2;
    		private int y2;
     
    		public KeyboardPanel(){
    			addKeyListener(new KeyAdapter(){
    				public void keyPressed(KeyEvent e){
    					switch (e.getKeyCode()){
    					case KeyEvent.VK_DOWN:
    						break;
     
    					case KeyEvent.VK_UP:
    						break;
     
    					}
    					repaint();
    				}
    			});
    		}	
    	}	
     
    	public static int[][] getMap0(){
    		int[][] map = new int[32][12];  
    		map[0][0] = 1; map[1][0] = 0; map[2][0] = 0; map[3][0] = 0; map[4][0] = 0; map[5][0] = 0; map[6][0] = 0; map[7][0] = 0;
    		map[8][0] = 0; map[9][0] = 0; map[10][0] = 0; map[11][0] = 0; map[12][0] = 0; map[13][0] = 0; map[14][0] = 0; map[15][0] = 0; 
    		map[16][0] = 0; map[17][0] = 0; map[18][0] = 0; map[19][0] = 0; map[20][0] = 0; map[21][0] = 0; map[22][0] = 0; map[23][0] = 0;
    		map[24][0] = 0; map[25][0] = 0; map[26][0] = 0; map[27][0] = 0;	map[28][0] = 0; map[29][0] = 0; map[30][0] = 0; map[31][0] = 1;
     
    		map[0][1] = 1; map[1][1] = 0; map[2][1] = 0; map[3][1] = 0; map[4][1] = 0; map[5][1] = 0; map[6][1] = 0; map[7][1] = 0;
    		map[8][1] = 0; map[9][1] = 0; map[10][1] = 0; map[11][1] = 0; map[12][1] = 0; map[13][1] = 0; map[14][1] = 0; map[15][1] = 0; 
    		map[16][1] = 0; map[17][1] = 0; map[18][1] = 0; map[19][1] = 0; map[20][1] = 0; map[21][1] = 0; map[22][1] = 0; map[23][1] = 0;
    		map[24][1] = 0; map[25][1] = 0; map[26][1] = 0; map[27][1] = 0;	map[28][1] = 0; map[29][1] = 0; map[30][1] = 0; map[31][1] = 1;	
     
    		map[0][2] = 1; map[1][2] = 0; map[2][2] = 0; map[3][2] = 0; map[4][2] = 0; map[5][2] = 0; map[6][2] = 0; map[7][2] = 0;
    		map[8][2] = 0; map[9][2] = 0; map[10][2] = 0; map[11][2] = 0; map[12][2] = 0; map[13][2] = 0; map[14][2] = 0; map[15][2] = 0; 
    		map[16][2] = 0; map[17][2] = 0; map[18][2] = 0; map[19][2] = 0; map[20][2] = 0; map[21][2] = 0; map[22][2] = 0; map[23][2] = 0;
    		map[24][2] = 0; map[25][2] = 0; map[26][2] = 0; map[27][2] = 0;	map[28][2] = 0; map[29][2] = 0; map[30][2] = 0; map[31][2] = 1;	
     
    		map[0][3] = 1; map[1][3] = 0; map[2][3] = 0; map[3][3] = 0; map[4][3] = 0; map[5][3] = 0; map[6][3] = 0; map[7][3] = 0;
    		map[8][3] = 0; map[9][3] = 0; map[10][3] = 0; map[11][3] = 0; map[12][3] = 0; map[13][3] = 0; map[14][3] = 0; map[15][3] = 0; 
    		map[16][3] = 0; map[17][3] = 0; map[18][3] = 0; map[19][3] = 0; map[20][3] = 0; map[21][3] = 0; map[22][3] = 0; map[23][3] = 0;
    		map[24][3] = 0; map[25][3] = 0; map[26][3] = 0; map[27][3] = 0;	map[28][3] = 0; map[29][3] = 0; map[30][3] = 0; map[31][3] = 1;	
     
    		map[0][4] = 1; map[1][4] = 0; map[2][4] = 0; map[3][4] = 0; map[4][4] = 0; map[5][4] = 0; map[6][4] = 0; map[7][4] = 0;
    		map[8][4] = 0; map[9][4] = 0; map[10][4] = 0; map[11][4] = 0; map[12][4] = 0; map[13][4] = 0; map[14][4] = 0; map[15][4] = 0; 
    		map[16][4] = 0; map[17][4] = 0; map[18][4] = 0; map[19][4] = 0; map[20][4] = 0; map[21][4] = 0; map[22][4] = 0; map[23][4] = 0;
    		map[24][4] = 0; map[25][4] = 0; map[26][4] = 0; map[27][4] = 0;	map[28][4] = 0; map[29][4] = 0; map[30][4] = 0; map[31][4] = 1;	
     
    		map[0][5] = 1; map[1][5] = 0; map[2][5] = 0; map[3][5] = 0; map[4][5] = 0; map[5][5] = 0; map[6][5] = 0; map[7][5] = 0;
    		map[8][5] = 0; map[9][5] = 0; map[10][5] = 0; map[11][5] = 0; map[12][5] = 0; map[13][5] = 0; map[14][5] = 0; map[15][5] = 0; 
    		map[16][5] = 0; map[17][5] = 0; map[18][5] = 0; map[19][5] = 0; map[20][5] = 0; map[21][5] = 0; map[22][5] = 0; map[23][5] = 0;
    		map[24][5] = 0; map[25][5] = 0; map[26][5] = 0; map[27][5] = 0;	map[28][5] = 0; map[29][5] = 0; map[30][5] = 0; map[31][5] = 1;	
     
    		map[0][6] = 1; map[1][6] = 0; map[2][6] = 0; map[3][6] = 0; map[4][6] = 0; map[5][6] = 0; map[6][6] = 0; map[7][6] = 0;
    		map[8][6] = 0; map[9][6] = 0; map[10][6] = 0; map[11][6] = 0; map[12][6] = 0; map[13][6] = 0; map[14][6] = 0; map[15][6] = 0; 
    		map[16][6] = 0; map[17][6] = 0; map[18][6] = 0; map[19][6] = 0; map[20][6] = 0; map[21][6] = 0; map[22][6] = 0; map[23][6] = 0;
    		map[24][6] = 0; map[25][6] = 0; map[26][6] = 0; map[27][6] = 0;	map[28][6] = 0; map[29][6] = 0; map[30][6] = 0; map[31][6] = 1;	
     
    		map[0][7] = 1; map[1][7] = 0; map[2][7] = 0; map[3][7] = 0; map[4][7] = 0; map[5][7] = 0; map[6][7] = 0; map[7][7] = 0;
    		map[8][7] = 0; map[9][7] = 0; map[10][7] = 0; map[11][7] = 0; map[12][7] = 0; map[13][7] = 0; map[14][7] = 0; map[15][7] = 0; 
    		map[16][7] = 0; map[17][7] = 0; map[18][7] = 0; map[19][7] = 0; map[20][7] = 0; map[21][7] = 0; map[22][7] = 0; map[23][7] = 0;
    		map[24][7] = 0; map[25][7] = 0; map[26][7] = 0; map[27][7] = 0;	map[28][7] = 0; map[29][7] = 0; map[30][7] = 0; map[31][7] = 1;	
     
    		map[0][8] = 1; map[1][8] = 0; map[2][8] = 0; map[3][8] = 0; map[4][8] = 0; map[5][8] = 0; map[6][8] = 0; map[7][8] = 0;
    		map[8][8] = 0; map[9][8] = 0; map[10][8] = 0; map[11][8] = 0; map[12][8] = 0; map[13][8] = 0; map[14][8] = 0; map[15][8] = 0; 
    		map[16][8] = 0; map[17][8] = 0; map[18][8] = 0; map[19][8] = 0; map[20][8] = 0; map[21][8] = 0; map[22][8] = 0; map[23][8] = 0;
    		map[24][8] = 0; map[25][8] = 0; map[26][8] = 0; map[27][8] = 0;	map[28][8] = 0; map[29][8] = 0; map[30][8] = 0; map[31][8] = 1;	
     
    		map[0][9] = 1; map[1][9] = 0; map[2][9] = 0; map[3][9] = 0; map[4][9] = 0; map[5][9] = 0; map[6][9] = 0; map[7][9] = 0;
    		map[8][9] = 0; map[9][9] = 0; map[10][9] = 0; map[11][9] = 0; map[12][9] = 0; map[13][9] = 0; map[14][9] = 0; map[15][9] = 0; 
    		map[16][9] = 0; map[17][9] = 0; map[18][9] = 0; map[19][9] = 0; map[20][9] = 0; map[21][9] = 0; map[22][9] = 0; map[23][9] = 0;
    		map[24][9] = 0; map[25][9] = 0; map[26][9] = 0; map[27][9] = 0;	map[28][9] = 0; map[29][9] = 0; map[30][9] = 0; map[31][9] = 1;	
     
    		map[0][10] = 1; map[1][10] = 0; map[2][10] = 0; map[3][10] = 0; map[4][10] = 0; map[5][10] = 0; map[6][10] = 0; map[7][10] = 0;
    		map[8][10] = 0; map[9][10] = 0; map[10][10] = 0; map[11][10] = 0; map[12][10] = 0; map[13][10] = 0; map[14][10] = 0; map[15][10] = 0; 
    		map[16][10] = 0; map[17][10] = 0; map[18][10] = 0; map[19][10] = 0; map[20][10] = 0; map[21][10] = 0; map[22][10] = 0; map[23][10] = 0;
    		map[24][10] = 0; map[25][10] = 0; map[26][10] = 0; map[27][10] = 0;	map[28][10] = 0; map[29][10] = 0; map[30][10] = 0; map[31][10] = 1;	
     
    		map[0][11] = 1; map[1][11] = 1; map[2][11] = 1; map[3][11] = 1; map[4][11] = 1; map[5][11] = 1; map[6][11] = 1; map[7][11] = 1;
    		map[8][11] = 1; map[9][11] = 1; map[10][11] = 1; map[11][11] = 1; map[12][11] = 1; map[13][11] = 1; map[14][11] = 1; map[15][11] = 1; 
    		map[16][11] = 1; map[17][11] = 1; map[18][11] = 1; map[19][11] = 1; map[20][11] = 1; map[21][11] = 1; map[22][11] = 1; map[23][11] = 1;
    		map[24][11] = 1; map[25][11] = 1; map[26][11] = 1; map[27][11] = 1;	map[28][11] = 1; map[29][11] = 1; map[30][11] = 1; map[31][11] = 1;
     
    		return map;
    	}
    }


    Error code on the console looks something like this:

    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)

    etc….. for a vastly large quantity of lines.


    EDIT: The error only occurs after everything is already loaded, and it seems to happen when the timer attempts to redraw the screen. Ive commented out timer, and still get the error when the program tries to draw its initial screen. It has something to do with my paintComponent(Graphics g) command, but I am not sure where exactly.
    Last edited by DOLZero; April 22nd, 2012 at 06:31 AM.


  2. #2
    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: JFrame errors

    You need to post the full text of the error message that shows what the error is and where it happened.

    You can initialize an array when you define it and not have to use dozens of assignment statements. For example:
    int[][] map = {{2,3, 0}, {2,2,2}, {3,3,3}};
    Last edited by Norm; April 22nd, 2012 at 06:47 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JFrame errors

    The errors happen whenever the program tries to paint graphics to the frame. With the timer code commented out, the errors only appear the one time:

    edit: errr, it appears twice:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Mark2.gameTester$AnimatedPanel.paintComponent(gameTester.java:121)
    	at javax.swing.JComponent.paint(JComponent.java:1029)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    	at java.awt.Container.paint(Container.java:1791)
    	at java.awt.Window.paint(Window.java:3379)
    	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
    	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
    	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
    	at apple.awt.CWindow.handleEvent(CWindow.java:545)
    	at java.awt.Component.dispatchEventImpl(Component.java:4813)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Mark2.gameTester$AnimatedPanel.paintComponent(gameTester.java:121)
    	at javax.swing.JComponent.paint(JComponent.java:1029)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    	at java.awt.Container.paint(Container.java:1791)
    	at java.awt.Window.paint(Window.java:3379)
    	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
    	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
    	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
    	at apple.awt.CWindow.handleEvent(CWindow.java:545)
    	at java.awt.Component.dispatchEventImpl(Component.java:4813)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    Also, thanks for the tip on the array. I knew how to initialize with a code such as: "int point[] = {i * 50, j * 50};" but I was not sure how to do it with a multidimensional array.

  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: JFrame errors

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Mark2.gameTester$AnimatedPanel.paintComponent(game Tester.java:121)
    There is a variable on line 121 that has a null value. Look at line 121 and find the variable that has the null value and then backtrack in the code to find why it does not have a valid non-null value.
    If you can't see which variable is null, add a println statement just before line 121 and print out the values of all the variables on line 121.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JFrame errors

    line 121 appears to be this line in the code:

    for (int i = 0; i < environment.length; i++){

    I am pretty sure that environment is already initialized as an array before the program ever reaches this code. Not really sure why it doesn't accept it.

  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: JFrame errors

    Did you add the println statement? What did it print out?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JFrame errors

    I added in "system.out.prinrln(environment);" right before the line and it printed out "[LMark2.environment;@5d6d2633" in the console.

    Although I am not sure if that is exactly what you meant. Sorry about not being very knowledgeable when it comes to the code.

    Also, thanks for being so persistent and patient with me. Its much appreciated.

  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: JFrame errors

    Then the variable environment is not null. If it is not null then you will not get a NullPointerException.

    Check that you are looking at the correct line.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JFrame errors

    Okay, I think I have got it narrowed down. It seems there is something going on in this bit of code here:

    			for (int i = 0; i < environment.length; i++){
    				System.out.println(environment[i].getPoint());
    				int[] temp = environment[i].getPoint();	
    				g.drawImage(basicBlock2, temp[0], temp[1], 50, 50, this);
    			}

    When I run it with the println command, it displays:

    [I@5d6d2633
    [I@28e70e30
    [I@5954864a
    [I@3c3c9217
    [I@2c9b42e6
    [I@54520eb
    [I@7d742700
    [I@54acb158
    [I@15af33d6
    [I@217431b9
    [I@536c79d7
    [I@76b33d0a
    [I@716c9867
    [I@1089cc5e
    [I@46c837cd
    [I@2c79809
    [I@7dce784b
    [I@7e3bc473
    [I@5de9ac4
    [I@5982bcde
    [I@30384065
    [I@3d0bbf9e
    [I@4e513d61
    [I@3794d372
    [I@bc5fde0
    [I@739e8329
    [I@2bb5340c
    [I@56c163f
    [I@7b2884e0
    [I@212bcd4b
    [I@42b307f0
    [I@77ce3fc5
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Mark2.gameTester$AnimatedPanel.paintComponent(gameTester.java:122)
    	at javax.swing.JComponent.paint(JComponent.java:1029)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    	at java.awt.Container.paint(Container.java:1791)
    	at java.awt.Window.paint(Window.java:3379)
    	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
    	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
    	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
    	at apple.awt.CWindow.handleEvent(CWindow.java:545)
    	at java.awt.Component.dispatchEventImpl(Component.java:4813)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    [I@5d6d2633
    [I@28e70e30
    [I@5954864a
    [I@3c3c9217
    [I@2c9b42e6
    [I@54520eb
    [I@7d742700
    [I@54acb158
    [I@15af33d6
    [I@217431b9
    [I@536c79d7
    [I@76b33d0a
    [I@716c9867
    [I@1089cc5e
    [I@46c837cd
    [I@2c79809
    [I@7dce784b
    [I@7e3bc473
    [I@5de9ac4
    [I@5982bcde
    [I@30384065
    [I@3d0bbf9e
    [I@4e513d61
    [I@3794d372
    [I@bc5fde0
    [I@739e8329
    [I@2bb5340c
    [I@56c163f
    [I@7b2884e0
    [I@212bcd4b
    [I@42b307f0
    [I@77ce3fc5
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Mark2.gameTester$AnimatedPanel.paintComponent(gameTester.java:122)
    	at javax.swing.JComponent.paint(JComponent.java:1029)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    	at java.awt.Container.paint(Container.java:1791)
    	at java.awt.Window.paint(Window.java:3379)
    	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
    	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
    	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
    	at apple.awt.CWindow.handleEvent(CWindow.java:545)
    	at java.awt.Component.dispatchEventImpl(Component.java:4813)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    and then when I comment out the println command I get:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Mark2.gameTester$AnimatedPanel.paintComponent(gameTester.java:123)
    	at javax.swing.JComponent.paint(JComponent.java:1029)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    	at java.awt.Container.paint(Container.java:1791)
    	at java.awt.Window.paint(Window.java:3379)
    	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
    	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
    	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
    	at apple.awt.CWindow.handleEvent(CWindow.java:545)
    	at java.awt.Component.dispatchEventImpl(Component.java:4813)
    	at java.awt.Container.dispatchEventImpl(Container.java:2141)
    	at java.awt.Window.dispatchEventImpl(Window.java:2482)
    	at java.awt.Component.dispatchEvent(Component.java:4565)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:643)
    	at java.awt.EventQueue$1.run(EventQueue.java:641)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:657)
    	at java.awt.EventQueue$2.run(EventQueue.java:655)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    From what I can tell it has a value that it is displaying from the println command, but there is still some value in each of those lines that is causing an error.

  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: JFrame errors

    What is the value of i when the exception occurs?
    If environment[i] is null you would get the exception on the following line.
    System.out.println(environment[i].getPoint());
    The call to getPoint with a valid object would not cause a NPE

    It would show better if you printed this:
    System.out.println("i="+ i + ", env="+environment[i]);
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    DOLZero (April 22nd, 2012)

  12. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JFrame errors

    Ah, that's right. You helped me realize where I went wrong. When I was creating each environment object I was putting it in environment[i] in the loop, which i always increased by 1, whether it made the object or not. So basically, there was not always an object assigned to certain variables.

    I am having another issue with the display not showing, but its been a long night and this particular issue is solved. I think I will tackle the other issue at a later time. Thanks for your help!

Similar Threads

  1. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  2. [SOLVED] JFrame Menu bar errors.
    By lorider93p in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2012, 11:02 AM
  3. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 08:51 AM
  4. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  5. Can't fix my own errors
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 09:20 AM