Reputation: 152
I'm doing some pretty basic coding, trying to call a method from a different class but for some reason I'm getting a null pointer exception whenever I try to call any method from any different class. I think I've created the instances of the class correctly but I'm not sure. If anybody can explain what's going wrong to me I'd appreciate it.
here is the class that makes the call:
public class Menu extends JPanel implements ActionListener{
Skeleton skeleton;
Board board;
public Menu(){
setBackground(Color.BLACK);
JButton button = new JButton("hello");
button.addActionListener(this);
this.add(button);
}
public JPanel getPanel(){
return this;
}
@Override
public void actionPerformed(ActionEvent e) {
board.boardTest();
}
}
and here is the class containing the method
public class Board extends JPanel{
public Board(){
setBackground(Color.WHITE);
}
public JPanel getPanel(){
return this;
}
public void boardTest(){
System.out.print("hello");
}
}
As you can see, whenever the user clicks the button it should print out 'hello'.
Upvotes: 1
Views: 19395
Reputation: 97
Whenever you call a method residing in some other class, you have to create an object of that other class and then call it.Here I see that you have not created the object.
Board board = new Board(); is missing from the code
Upvotes: 0
Reputation: 3266
This is not how you create an instance of class. When you declare on some variable of some class you must use the new method. If you don't the compiler won't know how to relate to this and therefore the program isn't valid and that's why you are getting null exception. You see, when you write a program and try to start it, it first go through the compiler, the compiler checks if the program is valid and if not is alerting about it. The compiler see the code as a long string and divide it into tokens. To be more simple, let's say that each declaration about some variable is a token,each keyword is also token. The variable name is identifier. So the compiler search for tokens and save there data in some symbols table and say what is type and what is value. For example int num =3; int is token, num is identifier and 3 is the value, now the compiler will know how much memory to allocate for this. Now in your case you didn't write Board board = new Board(); Because of this, the compiler doesn't know how much space to allocate and there is no reference to some instance. So the value in the symbols table isn't declare. This cause the compiler to allet about null exception. Note that another rolenof the constructor of some class is to initialize some class fields. Let's say you have the class Point and you want that each time you create a new point the initial x,y will be zero.
So
Class Point{
int x,y;
Point(){
x=y=0;
}
And when you create a new point the initial coordinate will (0,0).
Point p = new Point();
Upvotes: 0
Reputation: 285450
Your code looks as though it should throw a NullPointerException (NPE) when you try to call board.boardTest()
because you're making the call before assigning a Board object to the board variable and are thus making a method call on a null variable.
You have to create a Board instance before you can try to use the Board variable, board. i.e.,
Board board = new Board();
Note 1: that for similar questions in the future, you will want to show us the exception text and indicate by comment in your code which lines throws the exception. i.e.,
@Override
public void actionPerformed(ActionEvent e) {
board.boardTest(); // **** A NullPOinterException is thrown here ****
}
Note 2: this question is not Swing specific but rather is a basic Java issue -- you cannot use a reference variable until you first assign it a valid object.
Upvotes: 8