Hallo,
kann mir vielleicht jemand helfen und erklären, wieso ich bei dem ersten Code eine NullPointerExeption in der Zeile mit
bekomme und in der zweiten Version nicht?
Fehlermeldung:
Version mit Fehler:
[HIGHLIGHT="Java"]public class GUI_Layout extends JFrame implements ActionListener
{
private clsJPanel myJPanel;
private JButton Auswahl_JBtn[];
// Konstruktor --> kein VOID --> sonst Methode und nicht Konstruktor
public GUI_Layout (String Titelzeile) throws IOException
{
super(Titelzeile);
setSize(800,600);
this.setBackground(Color.LIGHT_GRAY);
this.setLocation(150, 100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
// Dadurch kann ich jedes Element frei platzieren
setLayout(null);
// etwas Code gelöscht
myJPanel = new clsJPanel(600,400, Color.WHITE);
add (myJPanel);
myJPanel.setLocation(100,100);
JButton b;
for (int i = 0; i <10 ;i++)
{
if (i==0) Auswahl_JBtn = new JButton("LEER");
else
{
Auswahl_JBtn = new JButton(Integer.toString(i));
}
myRightMenu.add(Auswahl_JBtn);
Auswahl_JBtn.addActionListener(this); }
//add (BorderLayout.NORTH, new Title(this.WIDTH, 100, Color.BLUE));
//add (BorderLayout.WEST, new Title(100, this.HEIGHT-200, Color.GREEN));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//Auslesen des gedrueckten JButtons
String myBtn = e.getActionCommand();
System.out.println(myBtn);
if (myBtn.equals("LEER")) myJPanel.Set_EinzusetzendeZahl(0);
else
myJPanel.Set_EinzusetzendeZahl(Integer.parseInt(myBtn));
Auswahl_JBtn[1].setBackground(Color.BLUE);
}
}[/HIGHLIGHT]
Ohne Fehler:
[HIGHLIGHT="Java"]public class GUI_Layout extends JFrame implements ActionListener
{
private clsJPanel myJPanel;
private JButton Auswahl_JBtn[];
// Konstruktor --> kein VOID --> sonst Methode und nicht Konstruktor
public GUI_Layout (String Titelzeile) throws IOException
{
super(Titelzeile);
setSize(800,600);
this.setBackground(Color.LIGHT_GRAY);
this.setLocation(150, 100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
// Dadurch kann ich jedes Element frei platzieren
setLayout(null);
// (...)
myJPanel = new clsJPanel(600,400, Color.WHITE);
add (myJPanel);
myJPanel.setLocation(100,100);
JButton b;
for (int i = 0; i <10 ;i++)
{
if (i==0) b = new JButton("LEER");
else
{
b = new JButton(Integer.toString(i));
}
myRightMenu.add(b);
b.addActionListener(this);
}
//add (BorderLayout.NORTH, new Title(this.WIDTH, 100, Color.BLUE));
//add (BorderLayout.WEST, new Title(100, this.HEIGHT-200, Color.GREEN));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//Auslesen des gedrueckten JButtons
String myBtn = e.getActionCommand();
System.out.println(myBtn);
if (myBtn.equals("LEER")) myJPanel.Set_EinzusetzendeZahl(0);
else
myJPanel.Set_EinzusetzendeZahl(Integer.parseInt(myBtn));
Auswahl_JBtn[1].setBackground(Color.BLUE);
}
}[/HIGHLIGHT]
kann mir vielleicht jemand helfen und erklären, wieso ich bei dem ersten Code eine NullPointerExeption in der Zeile mit
Code:
if (i==0) Auswahl_JBtn[i] = new JButton("LEER");
Fehlermeldung:
Vielen Dankrun:
Exception in thread "main" java.lang.NullPointerException
at sudoku.GUI_Layout.<init>(GUI_Layout.java:69)
at sudoku.Main.main(Main.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Version mit Fehler:
[HIGHLIGHT="Java"]public class GUI_Layout extends JFrame implements ActionListener
{
private clsJPanel myJPanel;
private JButton Auswahl_JBtn[];
// Konstruktor --> kein VOID --> sonst Methode und nicht Konstruktor
public GUI_Layout (String Titelzeile) throws IOException
{
super(Titelzeile);
setSize(800,600);
this.setBackground(Color.LIGHT_GRAY);
this.setLocation(150, 100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
// Dadurch kann ich jedes Element frei platzieren
setLayout(null);
// etwas Code gelöscht
myJPanel = new clsJPanel(600,400, Color.WHITE);
add (myJPanel);
myJPanel.setLocation(100,100);
JButton b;
for (int i = 0; i <10 ;i++)
{
if (i==0) Auswahl_JBtn = new JButton("LEER");
else
{
Auswahl_JBtn = new JButton(Integer.toString(i));
}
myRightMenu.add(Auswahl_JBtn);
Auswahl_JBtn.addActionListener(this); }
//add (BorderLayout.NORTH, new Title(this.WIDTH, 100, Color.BLUE));
//add (BorderLayout.WEST, new Title(100, this.HEIGHT-200, Color.GREEN));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//Auslesen des gedrueckten JButtons
String myBtn = e.getActionCommand();
System.out.println(myBtn);
if (myBtn.equals("LEER")) myJPanel.Set_EinzusetzendeZahl(0);
else
myJPanel.Set_EinzusetzendeZahl(Integer.parseInt(myBtn));
Auswahl_JBtn[1].setBackground(Color.BLUE);
}
}[/HIGHLIGHT]
Ohne Fehler:
[HIGHLIGHT="Java"]public class GUI_Layout extends JFrame implements ActionListener
{
private clsJPanel myJPanel;
private JButton Auswahl_JBtn[];
// Konstruktor --> kein VOID --> sonst Methode und nicht Konstruktor
public GUI_Layout (String Titelzeile) throws IOException
{
super(Titelzeile);
setSize(800,600);
this.setBackground(Color.LIGHT_GRAY);
this.setLocation(150, 100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
// Dadurch kann ich jedes Element frei platzieren
setLayout(null);
// (...)
myJPanel = new clsJPanel(600,400, Color.WHITE);
add (myJPanel);
myJPanel.setLocation(100,100);
JButton b;
for (int i = 0; i <10 ;i++)
{
if (i==0) b = new JButton("LEER");
else
{
b = new JButton(Integer.toString(i));
}
myRightMenu.add(b);
b.addActionListener(this);
}
//add (BorderLayout.NORTH, new Title(this.WIDTH, 100, Color.BLUE));
//add (BorderLayout.WEST, new Title(100, this.HEIGHT-200, Color.GREEN));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//Auslesen des gedrueckten JButtons
String myBtn = e.getActionCommand();
System.out.println(myBtn);
if (myBtn.equals("LEER")) myJPanel.Set_EinzusetzendeZahl(0);
else
myJPanel.Set_EinzusetzendeZahl(Integer.parseInt(myBtn));
Auswahl_JBtn[1].setBackground(Color.BLUE);
}
}[/HIGHLIGHT]