Hallo
Ich bekomme folgende Fehlermeldung beim Ausführen:
Exception in thread "main" java.lang.NullPointerException
at bk.Contact.setBirthDate(Contact.java:77)
at bk.TestDriver.main(TestDriver.java:9)
Hier der Code (Ich lass mal alles Überflüssige weg):
Danke und Grüsse
Ich bekomme folgende Fehlermeldung beim Ausführen:
Exception in thread "main" java.lang.NullPointerException
at bk.Contact.setBirthDate(Contact.java:77)
at bk.TestDriver.main(TestDriver.java:9)
Hier der Code (Ich lass mal alles Überflüssige weg):
Java:
package bk;
import java.util.Calendar;
import java.lang.StringBuilder;
import java.text.SimpleDateFormat;
public class Contact {
private String firstName;
private String lastName;
private Calendar birthDate;
private String phoneNumber;
private String eMail;
private int idNumber;
//address initialize all together
private String street;
private int streetNumber;
private int postCode;
private String residence;
//
private static int counter = 10000;
private StringBuilder builder = new StringBuilder();
//Constructor
public Contact(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
idNumber = counter;
counter++;
}
public String toString() {
//appends idNumber
builder.append("ID Number: " + "\t" + idNumber + "\n");
//appends name
builder.append("First Name: " + "\t" + firstName + "\n");
builder.append("Last Name: " + "\t" + lastName + "\n");
//appends birthDate
if (birthDate == null) {}
else builder.append("Birthday: " + "\t" + birthDateToString() + "\n");
//appends phoneNumber
if (phoneNumber == null) {}
else builder.append("Phone Number: " + "\t" + phoneNumber + "\n");
//appends eMail
if (eMail == null) {}
else builder.append("E-Mail: " + "\t" + eMail + "\n");
//appends address
if (street == null) {}
else {
builder.append("Street : " + "\t" + street + " " + streetNumber + "\n");
builder.append("Residence: " + "\t" + postCode + " " + residence + "\n");
}
return builder.toString();
}
//helper class for toString, converts Date birthDate to String stringDate
private String birthDateToString() {
String stringDate;
SimpleDateFormat sdf = new SimpleDateFormat();
stringDate = sdf.format(birthDate);
return stringDate;
}
/* Accessor / Mutator
* address can only be initialized all together
* idNumber can not be changed
*/
public void setBirthDate(int initialDate, int initialMonth, int initialYear) {
birthDate.set(initialYear, initialMonth, initialDate); //<-Fehler
}
public String getBirthDate() {
return birthDateToString();
}
}
Java:
package bk;
public class TestDriver {
public static void main(String[] args) {
Contact me = new Contact("Hans", "Muster");
me.setBirthDate(1, 1, 1988); <- Fehler
System.out.println(me.toString());
}
}
Danke und Grüsse