NullPointerExeption mit JLabeln

P

Patt1502

Gast
Ich hab echt keine Ahnung wieso da diesen Fehler bekomme, ich hab ein Tutorial geschaut und alles genau so gemacht. jlKarte1 ist ein JLabel, JTKarte1 ist ein JTextfeld und Karte1 ist eine JComboBox. Wieso kann man bei dem .add() überhaupt eine NullPointerException bekommen?
 

Anhänge

  • Java.PNG
    Java.PNG
    38,3 KB · Aufrufe: 35

VfL_Freak

Top Contributor
Moin,

mal so ins Blaue geraten ist 'hBox1' == null !
Mehr kann ohne weiteren Code nicht sagen, wie thet1982 schon schrieb!

Und poste Cocde bitte IMMER in Code-Tags udn nicht als Bild!
Ist dann besser lesbar und vor allem zitierbar!!

VG Klaus
 
P

Patt1502

Gast
public class AddCard extends JPanel {

private static final long serialVersionUID = 1L;

private static Box mainBox, hBox1, hBox2, hBox3, hBox4, hBox5, hBox6, hBox7, hBox8;

private static JLabel jlKarte1, jlKarte2, jlKarte3, jlKarte4, jlKarte5, jlKarte6, jlKarte7, jlKarte8;

private JTextField jtKarte1, jtKarte2, jtKarte3, jtKarte4, jtKarte5, jtKarte6, jtKarte7, jtKarte8;
private JButton OK = new JButton("OK");
private JComboBox Karte1, Karte2, Karte3, Karte4, Karte5, Karte6, Karte7, Karte8;

public AddCard() {
super (new FlowLayout());
initWidgets();
addWidgets();
this.setBackground(new Color(150, 200, 100));
}




private void addWidgets() {
mainBox = Box.createVerticalBox();

hBox1 = Box.createHorizontalBox();
hBox2 = Box.createHorizontalBox();
hBox3 = Box.createHorizontalBox();
hBox4 = Box.createHorizontalBox();
hBox5 = Box.createHorizontalBox();
hBox6 = Box.createHorizontalBox();
hBox7 = Box.createHorizontalBox();
hBox8 = Box.createHorizontalBox();

jlKarte1 = new JLabel("Karte 1:");
jlKarte2 = new JLabel("Karte 2:");
jlKarte3 = new JLabel("Karte 3:");
jlKarte4 = new JLabel("Karte 4:");
jlKarte5 = new JLabel("Karte 5:");
jlKarte6 = new JLabel("Karte 6:");
jlKarte7 = new JLabel("Karte 7:");
jlKarte8 = new JLabel("Karte 8:");

Karte1 = new JComboBox();
Karte2 = new JComboBox();
Karte3 = new JComboBox();
Karte4 = new JComboBox();
Karte5 = new JComboBox();
Karte6 = new JComboBox();
Karte7 = new JComboBox();
Karte8 = new JComboBox();
}




private void initWidgets() {

mainBox.add(hBox1);
mainBox.add(hBox2);
mainBox.add(hBox3);
mainBox.add(hBox4);
mainBox.add(hBox5);
mainBox.add(hBox6);
mainBox.add(hBox7);
mainBox.add(hBox8);
mainBox.add(OK);

hBox1.add(jlKarte1); hBox1.add(Karte1);
hBox2.add(jlKarte2); hBox2.add(Karte2);
hBox3.add(jlKarte3); hBox3.add(Karte3);
hBox4.add(jlKarte4); hBox4.add(Karte4);
hBox5.add(jlKarte5); hBox5.add(Karte5);
hBox6.add(jlKarte6); hBox6.add(Karte6);
hBox7.add(jlKarte7); hBox7.add(Karte7);
hBox8.add(jlKarte8); hBox8.add(Karte8);

}

Das ist die ganze Klasse, mit den ganzen imports noch.
 

VfL_Freak

Top Contributor
Moin,

warum OHNE Code-Tags ??

Ist DAS jetzt der aktuelle Code ??? :rolleyes:
Hier sind nämlich die zeiklen mit "mainBox.add( ...)" und "hBox[nn].ad" vertauscht!

Auf jeden Fall sind zu Begin von initWidgets() SOWOHL mainBox als auch ALLE hBox[nn] gleich null!
EDIT: Karte[nn] natürlich auch !!

Du hast sie zwar allesamt deklariert, aber nirgendwo initialisiert !!

VG Klaus
 

Dompteur

Top Contributor
initWidgets() rufst du vor addWidgets() auf.
hBox1 wird aber erst in addWidgets() initialisiert, sodaß es in initWidgets noch null ist.
 

MiMij

Bekanntes Mitglied
Die Namen der Methoden würd ich auch umtauschen. Dort wo du die Objekte initialisierst, das sollte die initWidget Methode sein und wo du etwas hinzufügst mittels add() in der addWidget() methode.

Nochmal der Hinweis auch von mir, bitte Code-Tags verwenden. 4. Button von rechts "Einfügen ..." - > "Code" -> "java" auswählen
Ebenso Variablen bitte immer klein schreiben. Mir ist ein Rätsel warum die ersten klein geschrieben sind und dann die nächsten plötzlich mit großem Buchstaben anfangen.
 
P

Patt1502

Gast
Java:
public class AddCard extends JPanel {

    private static final long serialVersionUID = 1L;

    private static Box mainBox, hBox1, hBox2, hBox3, hBox4, hBox5, hBox6, hBox7, hBox8;

    private static JLabel jlKarte1, jlKarte2, jlKarte3, jlKarte4, jlKarte5, jlKarte6, jlKarte7, jlKarte8;

    private JButton OK = new JButton("OK");
    private JComboBox karte1, karte2, karte3, karte4, karte5, karte6, karte7, karte8;
   
   
    public AddCard() {
        super (new FlowLayout());
        initWidgets();
        addWidgets();
        setBackground(new Color(100, 100, 100));
    }
   
   
   
   
    private void initWidgets() {
        mainBox = Box.createVerticalBox();
       
        hBox1 = Box.createHorizontalBox();
        hBox2 = Box.createHorizontalBox();
        hBox3 = Box.createHorizontalBox();
        hBox4 = Box.createHorizontalBox();
        hBox5 = Box.createHorizontalBox();
        hBox6 = Box.createHorizontalBox();
        hBox7 = Box.createHorizontalBox();
        hBox8 = Box.createHorizontalBox();
       
        jlKarte1 = new JLabel("Karte 1:");
        jlKarte2 = new JLabel("Karte 2:");
        jlKarte3 = new JLabel("Karte 3:");
        jlKarte4 = new JLabel("Karte 4:");
        jlKarte5 = new JLabel("Karte 5:");
        jlKarte6 = new JLabel("Karte 6:");
        jlKarte7 = new JLabel("Karte 7:");
        jlKarte8 = new JLabel("Karte 8:");
       
        karte1 = new JComboBox();
        karte2 = new JComboBox();
        karte3 = new JComboBox();
        karte4 = new JComboBox();
        karte5 = new JComboBox();
        karte6 = new JComboBox();
        karte7 = new JComboBox();
        karte8 = new JComboBox();
    }

    private void addWidgets() {
       
        hBox1.add(jlKarte1); hBox1.add(karte1);
        hBox2.add(jlKarte2); hBox2.add(karte2);
        hBox3.add(jlKarte3); hBox3.add(karte3);
        hBox4.add(jlKarte4); hBox4.add(karte4);
        hBox5.add(jlKarte5); hBox5.add(karte5);
        hBox6.add(jlKarte6); hBox6.add(karte6);
        hBox7.add(jlKarte7); hBox7.add(karte7);
        hBox8.add(jlKarte8); hBox8.add(karte8);
       
        mainBox.add(hBox1);
        mainBox.add(hBox2);
        mainBox.add(hBox3);
        mainBox.add(hBox4);
        mainBox.add(hBox5);
        mainBox.add(hBox6);
        mainBox.add(hBox7);
        mainBox.add(hBox8);
        mainBox.add(OK);
       
       
    }

Das mit dem wie ich da jetzt den Code einfüge hab ich nicht gecheckt, aber jetzt. Ich hab das jetzt auch schöner gemacht, damit es einfacher zu verstehen ist, jetzt funktioniert das auch, aber ich sehe nur die Hintergrundfarbe und sonst nix. Keine Label und keine KomboBox.
 
P

Patt1502

Gast
Manchmal, da frag ich mich schon wieso ich studiere, bei solchen Fehlern könnte man sich vorstellen dass das nichts bringt.:D
 

MiMij

Bekanntes Mitglied
Ab und zu wenn man zulange drauf schaut, sieht man Sachen halt nicht mehr ;) Da hilft nur schritt für Schritt durchgehen was passiert.
 

Ähnliche Java Themen

Neue Themen


Oben