PersonBuilder和空指针[java]

时间:2016-10-09 11:52:32

标签: java nullpointerexception stringbuilder

我是java的新手,不知道如何修复我的简单代码。我需要创建一个人,使用另一个类,在本例中为“PersonBuilder”。 问题是,我有NullPointerException:X

  

线程“main”java.lang.NullPointerException中的异常       在RevStr $ PersonBuilder.withAge(RevStr.java:64)
      在RevStr.main(RevStr.java:75)

有班级人员

public class Person{
private String name;
private String surname;
private int age;
private String pesel;

// ** AKCESOR I MUTATOR ** //
public void setName(String name){
    this.name = name;
}
public void setSurname(String surname){
    this.surname = surname;
}
public void setAge(int age){
    this.age = age;
}
public void setPesel(String pesel){
    this.pesel = pesel;
}

public String getName(){
    return this.name;
}
public String getSurname(){
    return this.surname;
}
public int getAge(){
    return this.age;
}
public String getPesel(){
    return this.pesel;
}

}

和PersonBuilder

    public static class PersonBuilder{
    private Person p;

    public Person build(){
    return p;
    }

public PersonBuilder withName(String name){
    p.setName(name);
    return this;
}
public PersonBuilder withSurname(String surname){
    p.setSurname(surname);
    return this;
 }
public PersonBuilder withAge(int age){
    p.setAge(age);
    return this;
}
public PersonBuilder withPesel(String pesel){
    p.setPesel(pesel);
    return this;
}
}

主要方法

public static void main(String[] args) {
Person p = new PersonBuilder().withAge(25).withName("A").withSurname("B").withPesel("11224").build();
System.out.println(p);
}

我认为问题可以快速而简单地修复,但我现在看不到解决方案:X

0 个答案:

没有答案