null pointer exception help
i got null pointer exception. how can i solve it? i want create box and move it.
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);
}
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?
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
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);
}
}
Re: null pointer exception help
Have you tried calling repaint() or revalidate() after you move the label?
Re: null pointer exception help
Quote:
Originally Posted by
captain_turkiye
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
Re: null pointer exception help
Quote:
Originally Posted by
JosAH
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?
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.
Re: null pointer exception help
i add this :
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?
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
Re: null pointer exception help
Quote:
Originally Posted by
captain_turkiye
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
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?
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);
}
}
Re: null pointer exception help
What happens when you add your Background class to a GUI?
Re: null pointer exception help
i try to add like this, but it doesn't work.
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);
}
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.
Re: null pointer exception help
this is my main class:
Code:
public class test {
public static void main(String[] args) {
box box = new box();
}
}
it works without any background.
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.
Re: null pointer exception help
my whole code(in 3 class):
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();
}
}
Re: null pointer exception help
I see that you are adding two components to the same container:
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?