NullPointerException Headache
I've got a button that when pressed, will call a method in another java class, (a set method), to set a variable in an array. However, as the subject of this thread indicates, I keep getting the NullPointerException error.
I've attached the code that is appropriate for the things being used, and would really love some help.
Java Class 1 - ButtonPanel
Code:
class ButtonPanel extends JPanel{
private Elevator elevator;
public JButton b[] = new JButton[8]; // 8 Buttons
public boolean bp[] = new boolean[8]; // the state of each button, pressed or not
//constructor
public ButtonPanel(Elevator elevator) {
this.elevator = elevator;
this.setLayout(new GridLayout(8,1));
for(int i = 7 ; i >= 0 ; i--){
b[i] = new JButton("F" + Integer.toString(i+1));
b[i].setBackground(Color.LIGHT_GRAY);
this.add(b[i]);
b[i].addActionListener(new ButtonListener());
}
}
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
for(int i = 7 ; i >= 0 ; i--){
if(e.getSource() == b[i]){
int j = i + 1;
System.out.println("Pressed Floor "+ j );
b[i].setBackground(Color.YELLOW);
elevator.setFloor(j);
}
}
}
}
}
Java Class 2 - Elevator
Code:
public void setFloor(int i){
bp[i] = true;
System.out.println("Marker");
}
It doesn't seem to matter what's actually inside the setFloor method, as the NullPointerException is thrown inside the actionPerformed method of the button action, when setFloor() is called..
Anyone have any ideas? If you want more code then let me know...
Thanks
Denno