Java中引用空指针异常

时间:2017-08-17 06:05:33

标签: java

这里的家伙是我的简单代码,我不知道为什么会出现异常

我只想设定患者年龄,如果小于122岁。

请帮我解决问题。

Patient.java

public class Patient {
        int Age;
        String Gender;

        public int getAge() {
            return Age;
        }
        public void setAge(int age) {
            Age = age;
        }
        public String getGender() {
            return Gender;
        }
        public void setGender(String gender) {
            Gender = gender;
        }

        public Validation v;


        public void patient(int Age, String Gender) {

            if(v.age(Age, Gender)) {
                this.Age = Age;
                this.Gender = Gender;       
                };
        }   
    }

Validation.java

public class Validation {


public boolean age(int Age, String Gender) {
    // TODO Auto-generated method stub

    if(Age>122) {
        System.out.println("age is high");
        return false;
    }
    else
    {

        return true;
    }
}
}

Main.java

public class Main {
public static void main(String[] args) {
        // TODO Auto-generated method stub
 Patient p = new Patient();
p.patient(23, "Male");
System.out.println(+ p.Age + p.Gender);
}
}

错误

Exception in thread "main" java.lang.NullPointerException
at Patient.patient(Patient.java:24)
at Main.main(Main.java:5)

at Patient.patient(Patient.java:24):if(v.age(Age,Gender)) 在Main.main(Main.java:5):p.patient(23," Male");

0 个答案:

没有答案