Results 1 to 18 of 18
Thread: null pointer exception help
- 11-29-2011, 04:30 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
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); }
- 11-29-2011, 04:54 PM #2
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!
- 11-29-2011, 07:08 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
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); } }
- 11-29-2011, 07:14 PM #4
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!
- 11-29-2011, 07:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: null pointer exception help
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-30-2011, 12:36 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
- 11-30-2011, 01:36 AM #7
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.
- 12-01-2011, 09:00 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
Re: null pointer exception help
i add this :
output is:Java Code:if (e.getKeyCode() == KeyEvent.VK_LEFT){ System.out.println(System.currentTimeMillis()); x--; label.setBounds(x, y, 100, 50); }
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?
- 12-01-2011, 09:08 PM #9
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
- 12-01-2011, 09:09 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: null pointer exception help
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-03-2011, 07:17 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
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-03-2011, 07:20 PM #12
Re: null pointer exception help
What happens when you add your Background class to a GUI?
- 12-03-2011, 10:45 PM #13
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
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); }
- 12-03-2011, 10:54 PM #14
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.
- 12-03-2011, 11:05 PM #15
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
Re: null pointer exception help
this is my main class:
it works without any background.Java Code:public class test { public static void main(String[] args) { box box = new box(); } }
- 12-03-2011, 11:09 PM #16
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.
- 12-04-2011, 12:08 AM #17
Member
- Join Date
- Oct 2011
- Posts
- 50
- Rep Power
- 0
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(); } }
- 12-04-2011, 12:27 AM #18
Re: null pointer exception help
I see that you are adding two components to the same container:
Where will the layout manager place these two components? Will there be a conflict and only one of them will get shown?Java Code:frame.add(panel); //<<<<<<<<<<<<<< ONE frame.setSize(500, 250); frame.setVisible(true); frame.addKeyListener(this); frame.add(back); //<<<<<<<<<< TWO
Similar Threads
-
Null pointer exception
By DBaskov in forum New To JavaReplies: 14Last Post: 07-10-2011, 11:16 PM -
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM -
Null pointer exception
By Stephenmak in forum New To JavaReplies: 5Last Post: 04-01-2009, 02:17 PM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks