Results 1 to 18 of 18
  1. #1
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default null pointer exception help

    i got null pointer exception. how can i solve it? i want create box and move it.

    Java Code:
    	import javax.swing.*;
    
    	import java.awt.*;
    	import java.awt.event.KeyEvent;
    	import java.awt.event.KeyListener;
    
    	public class box implements KeyListener{
    
    		JFrame frame;
    		JPanel panel;
    		Icon icon;
    		JLabel label;
    		int x=20,y=20;
    		
    		public box(){		
    			
    			label=new JLabel(icon);
    			label.setBounds(100,50,100,50);
    			panel.add(label);
    			frame =new JFrame("labwork 8");
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			panel.setLayout(null);
    			icon = new ImageIcon(getClass().getResource("icon.jpg"));	
    			frame.add(panel);
    			frame.setSize(100, 75);
    			frame.setVisible(true);	
    			frame.addKeyListener(this);
    		}

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

    Default Re: null pointer exception help

    What line is your NPE on? Take a look at that line, and figure out why it's null.

    Hint: When do you initialize each of your variables?
    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
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    thanks, i forgot initialize panel, i think JPanel panel is enough. now it run, box is moving. but now when i push a key it jump somewhere,then move what i want. why?

    my key part

    Java Code:
    public void keyPressed(KeyEvent e) {
    			
    			if (e.getKeyCode() == KeyEvent.VK_LEFT){
    				x--;
    				label.setBounds(x, y, 100, 50);
    			}
    			
    			if (e.getKeyCode() == KeyEvent.VK_RIGHT){
    				x++;
    				label.setBounds(x, y, 100, 50);
    			}
    			if (e.getKeyCode() == KeyEvent.VK_UP){	
    				y--;
    				label.setBounds(x, y, 100, 50);
    			}
    			if (e.getKeyCode() == KeyEvent.VK_DOWN){	
    				y++;
    				label.setBounds(x, y, 100, 50);
    			}
    			
    			
    		}

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

    Default Re: null pointer exception help

    Have you tried calling repaint() or revalidate() after you move the label?
    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
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: null pointer exception help

    Quote Originally Posted by captain_turkiye View Post
    thanks, i forgot initialize panel, i think JPanel panel is enough. now it run, box is moving. but now when i push a key it jump somewhere,then move what i want. why?
    You initialize x and y to 20, 20 but you place your label at position at (100, 50) to start with. Initialize x and y to the same values.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    Quote Originally Posted by JosAH View Post
    You initialize x and y to 20, 20 but you place your label at position at (100, 50) to start with. Initialize x and y to the same values.

    kind regards,

    Jos
    thanks, now last problem, i think, when box start to move, it move 1 pixel, wait a moment and continue. why it can't move smooth?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: null pointer exception help

    Add some printlns to your code to print out the values of the variables your code is using as they are changed and as they are used. Print with the variables' values, the value of System.currentTimeMillis() to show what time the value was printed.
    The print outs should show you where the wait a moment happens.

  8. #8
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    i add this :

    Java Code:
    if (e.getKeyCode() == KeyEvent.VK_LEFT){
    				System.out.println(System.currentTimeMillis());
    				x--;
    				label.setBounds(x, y, 100, 50);
    			}
    output is:

    1322769425335
    1322769425880
    1322769425917
    1322769425955
    1322769425993
    1322769426031


    first and second move difference is larger than others. i think that causes problem. but how can i solve it?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: null pointer exception help

    Look at your OS's settings for repeated(??) key events for a held down key. In Windows it is in the Control Panel

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: null pointer exception help

    Quote Originally Posted by captain_turkiye View Post
    thanks, now last problem, i think, when box start to move, it move 1 pixel, wait a moment and continue. why it can't move smooth?
    Don't forget that if you press a key and keep it pressed, first an initial delay between the first and second key events happen (the 'initial delay') and next between the following key events the 'repeat delays' hit. Those two delay times are usually different.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    then that problem is not important. so i can ignore it. i want add background image. i wrote code but how can i add to panel?

    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    public class background extends JPanel {
    	
    	Image myimage;
    	
    	public background(){
    		
    		ImageIcon image;
    		image = new ImageIcon(getClass().getResource("background.jpg"));
    		myimage=image.getImage();
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponents(g);
    		g.drawImage(myimage,0,0,null);
    	}
    	
    }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: null pointer exception help

    What happens when you add your Background class to a GUI?

  13. #13
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    i try to add like this, but it doesn't work.

    Java Code:
    public box(){	
    			
    			background back = new background();
    			icon = new ImageIcon(getClass().getResource("icon.jpg"));	
    			panel = new JPanel();
    			panel.setLayout(null);
    			label=new JLabel(icon);
    			label.setBounds(75,120,100,50);
    			panel.add(label);
    			frame =new JFrame("test");
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    			frame.add(panel);
    			frame.setSize(500, 250);
    			frame.setVisible(true);	
    			frame.addKeyListener(this);
    			frame.add(back);
    	
    		}

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: null pointer exception help

    How do you compile and execute these pieces of code?
    There is no main() method here. Most variables do not have a definition.

  15. #15
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    this is my main class:

    Java Code:
    public class test {
    
    
    	public static void main(String[] args) {
    
    		box box = new box();
    
    	}
    
    }
    it works without any background.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: null pointer exception help

    Your code pieces do not compile. What about this: frame.addKeyListener(this);
    There has been too much copying and pasting of bits and pieces to make any sense of what you are trying to do.

    Can you make a small, complete program that compiles and executes and shows your problem.

  17. #17
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: null pointer exception help

    my whole code(in 3 class):

    Java Code:
    	import javax.swing.*;
    
    	import java.awt.*;
    	import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    	public class box implements KeyListener{
    
    		JFrame frame;
    		JPanel panel;
    		Icon icon;
    		JLabel label;
    		int x=75, y=120;
    		
    		public box(){	
    			
    			background back = new background();
    			icon = new ImageIcon(getClass().getResource("icon.jpg"));	
    			panel = new JPanel();
    			panel.setLayout(null);
    			label=new JLabel(icon);
    			label.setBounds(75,120,100,50);
    			panel.add(label);
    			panel.add(back);
    			frame =new JFrame("test");
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    			frame.add(panel);
    			frame.setSize(500, 250);
    			frame.setVisible(true);	
    			frame.addKeyListener(this);
    			frame.add(back);
    	
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    			
    			
    			
    			if (e.getKeyCode() == KeyEvent.VK_LEFT){
    				System.out.println(System.currentTimeMillis());
    				x--;
    				label.setBounds(x, y, 100, 50);
    			}
    			
    			if (e.getKeyCode() == KeyEvent.VK_RIGHT){
    				x++;
    				label.setBounds(x, y, 100, 50);
    			}
    			if (e.getKeyCode() == KeyEvent.VK_UP){	
    				y--;
    				label.setBounds(x, y, 100, 50);
    			}
    			if (e.getKeyCode() == KeyEvent.VK_DOWN){	
    				y++;
    				label.setBounds(x, y, 100, 50);
    			}
    			
    			
    		}
    
    		@Override
    		public void keyReleased(KeyEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyTyped(KeyEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    	}
    
    
    import java.awt.*;
    
    import javax.swing.*;
    
    public class background extends JPanel {
    	
    	Image myimage;
    	
    	public background(){
    		
    		ImageIcon image;
    		image = new ImageIcon(getClass().getResource("background.jpg"));
    		myimage=image.getImage();
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponents(g);
    		g.drawImage(myimage,0,0,null);
    	}
    	
    }
    
    public class test {
    
    
    	public static void main(String[] args) {
    
    		box box = new box();
    
    	}
    
    }

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: null pointer exception help

    I see that you are adding two components to the same container:
    Java Code:
                frame.add(panel);     //<<<<<<<<<<<<<< ONE
                frame.setSize(500, 250);
                frame.setVisible(true); 
                frame.addKeyListener(this);
                frame.add(back);    //<<<<<<<<<< TWO
    Where will the layout manager place these two components? Will there be a conflict and only one of them will get shown?

Similar Threads

  1. Null pointer exception
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-10-2011, 11:16 PM
  2. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  3. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  4. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

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