Moin
Kann mir jemand erklären, wie hier eine Null-Pointer-Exception entstehen kann in Zeile 8 (wenn ich die auskommentiere, dann folgen natürlich die Null-Pointer-Exceptions in den folgenden Zeilen)?
Ich kann mir das ganze nicht erkären, denn ich erzeuge den Array ja mit dem new-Operator. Somit ist der doch überhaupt nicht 'null'. Oder übersehen meine übermüdeten Augen da etwas?
[Java]
private void showGridColorMenu(){
//GUI is initialized
final JFrame frame = new JFrame("Grid Color");
final JRadioButton[] radioButtons = new JRadioButton[8];
//text of the JRadioButtons is set
radioButtons[0].setText("Yellow");
radioButtons[1].setText("Orange");
radioButtons[2].setText("Red");
radioButtons[3].setText("Blue");
radioButtons[4].setText("Green");
radioButtons[5].setText("Magenta");
radioButtons[6].setText("White");
radioButtons[7].setText("Black");
//OK-JButton is initialized and the action event is set.
JButton oK = new JButton("OK");
oK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//GUI must be changed on EventDispatchThread
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(false);
mapEditor.setEnabled(true);
mapEditor.setVisible(true);
}
});
int selectedRadioButton = -1;
for(int i = 0; i < radioButtons.length; i++){
if(radioButtons.isSelected()){
selectedRadioButton = i;
break;
}
}
//Color is set, depending on the chosen JRadioButton.
switch(selectedRadioButton){
case 0: gridColor = Color.YELLOW;
break;
case 1: gridColor = Color.ORANGE;
break;
case 2: gridColor = Color.RED;
break;
case 3: gridColor = Color.BLUE;
break;
case 4: gridColor = Color.GREEN;
break;
case 5: gridColor = Color.MAGENTA;
break;
case 6: gridColor = Color.WHITE;
break;
case 7: gridColor = Color.BLACK;
break;
}
}
});
//Cancel-JButton is initialized and listener is added.
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//GUI must be changed on EventDispatchThread
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(false);
mapEditor.setEnabled(true);
mapEditor.setVisible(true);
}
});
}
});
//Window is initialized, all components are added and started on EDT
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(true);
frame.setSize(new Dimension(200, 600));
frame.setResizable(false);
frame.setLocationRelativeTo(mapEditor);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
}
[/Java]
Kann mir jemand erklären, wie hier eine Null-Pointer-Exception entstehen kann in Zeile 8 (wenn ich die auskommentiere, dann folgen natürlich die Null-Pointer-Exceptions in den folgenden Zeilen)?
Ich kann mir das ganze nicht erkären, denn ich erzeuge den Array ja mit dem new-Operator. Somit ist der doch überhaupt nicht 'null'. Oder übersehen meine übermüdeten Augen da etwas?
[Java]
private void showGridColorMenu(){
//GUI is initialized
final JFrame frame = new JFrame("Grid Color");
final JRadioButton[] radioButtons = new JRadioButton[8];
//text of the JRadioButtons is set
radioButtons[0].setText("Yellow");
radioButtons[1].setText("Orange");
radioButtons[2].setText("Red");
radioButtons[3].setText("Blue");
radioButtons[4].setText("Green");
radioButtons[5].setText("Magenta");
radioButtons[6].setText("White");
radioButtons[7].setText("Black");
//OK-JButton is initialized and the action event is set.
JButton oK = new JButton("OK");
oK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//GUI must be changed on EventDispatchThread
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(false);
mapEditor.setEnabled(true);
mapEditor.setVisible(true);
}
});
int selectedRadioButton = -1;
for(int i = 0; i < radioButtons.length; i++){
if(radioButtons.isSelected()){
selectedRadioButton = i;
break;
}
}
//Color is set, depending on the chosen JRadioButton.
switch(selectedRadioButton){
case 0: gridColor = Color.YELLOW;
break;
case 1: gridColor = Color.ORANGE;
break;
case 2: gridColor = Color.RED;
break;
case 3: gridColor = Color.BLUE;
break;
case 4: gridColor = Color.GREEN;
break;
case 5: gridColor = Color.MAGENTA;
break;
case 6: gridColor = Color.WHITE;
break;
case 7: gridColor = Color.BLACK;
break;
}
}
});
//Cancel-JButton is initialized and listener is added.
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//GUI must be changed on EventDispatchThread
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(false);
mapEditor.setEnabled(true);
mapEditor.setVisible(true);
}
});
}
});
//Window is initialized, all components are added and started on EDT
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.setVisible(true);
frame.setSize(new Dimension(200, 600));
frame.setResizable(false);
frame.setLocationRelativeTo(mapEditor);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
}
[/Java]