NullPointerException
Verfasst: Mi Mai 04, 2011 3:42 pm
Hmhm, ich dachte in Java würde mir das nie passieren, aber ich muss für eine Hausaufgabe ein Malprogramm schreiben und die Knöpfe zum auswählen von den verschiedenen Malmethoden sollen wir mit Vierrecken und Text darin selbst zeichnen. Also habe ich mir in Java eine Klasse geschrieben namens Knopf:
Soweit so gut. Ich habe in meiner Hauptklasse dann ein Array erstellt für die Knöpfe und im Konstruktor folgendes geschrieben:
Bei Zeile 60, dass ist "this.knopf[0].rect.x = 20;" gibt er mir die Exception. Hat jemand eine Ahnung, woran es liegen kann?
Danke im Vorraus...
Code: Alles auswählen
// Knopf Klasse
public class Knopf
{
Rectangle rect = new Rectangle(); // Umrandung als Rechteck
String text = new String(); // Inhalt als String
int textX, textY; // Position des Strings
}
Code: Alles auswählen
// Knoepfe festlegen
this.knopf = new Knopf[5];
this.knopf[0].rect.x = 20;
this.knopf[0].text = "R"; // Rechteck
this.knopf[0].textX = 25;
this.knopf[1].rect.x = 45;
this.knopf[1].text = "K"; // Kreis
this.knopf[1].textX = 50;
this.knopf[2].rect.x = 70;
this.knopf[2].text = "D"; // Dreieck
this.knopf[2].textX = 75;
this.knopf[3].rect.x = 95;
this.knopf[3].text = "T"; // Text
this.knopf[3].textX = 100;
this.knopf[4].rect.x = 120;
this.knopf[4].text = "P"; // Polylinie
this.knopf[4].textX = 125;
for( int i = 0; i < knopf.length; i++ )
{
knopf[i].textY = 40;
knopf[i].rect.y = 40;
knopf[i].rect.height = 20;
knopf[i].rect.width = 20;
}
Danke im Vorraus...