无法输入值和空指针异常

时间:2014-03-15 02:43:50

标签: java

import java.util.*;
import java.io.*;


public class Patient
{
    private String patientId;
    private String patientName;
    private String gender;
    private int age;
    private String patientWardNumber;
    private int patientBedNumber;
    private String dateRegistered;
    private String treatment;
    private int wardedDuration;
    private double price;
    private double payment;

    public Patient()
    {
        String patientId =" ";
        String patientName =" ";
        String patientWardNumber =" ";
        String dateRegistered =" ";
        String treatment =" ";
        int patienBedDuration = 0;
        int wardedDuration = 0;
        int age = 0;
        String gender = " ";
        double price = 0.00;
    }

    public Patient(String ID,String N,String G,int A,String WN,int BN,String DR,String    T,int WD)
    {
        patientId = ID;
        patientName = N;
        gender = G;
        age = A;
        patientWardNumber = WN;
        patientBedNumber = BN;
        dateRegistered = DR;
        treatment = T;
        wardedDuration = WD;

    }

    public void SetPatient(String ID,String N,String G,int A,String WN,int BN,String DR,String T,int WD)
    {
        patientId = ID;
        patientName = N;
        gender = G;
        age = A;
        patientWardNumber = WN;
        patientBedNumber = BN;
        dateRegistered = DR;
        treatment = T;
        wardedDuration = WD;

    }

    public String getPatientId()
    {
        return patientId;
    }
    public String getPatientName()
    {
        return patientName;
    }
    public String getGender()
    {
        return gender;
    }
    public int getAge()
    {
        return age;
    }

    public String getPatientWardNumber()
    {
        return patientWardNumber;
    }

    public int getPatientBedNumber()
    {
        return patientBedNumber;
    }

    public String getDateRegistered()
    {
        return dateRegistered;
    }
    public String getTreatment()
    {
        return treatment;
    }
    public int getWardedDuration()
    {
        return wardedDuration;
    }
    public double getPrice()
    {
        return price;
    }
    public double getPayment()
    {
        return payment;
    }

    public String toString()
    {
        return patientId+" "+patientName+" "+gender+" "+age+" "+patientWardNumber+" "+patientBedNumber+" "+dateRegistered+" "+treatment+" "+wardedDuration+" "+price+" "+payment;
    }

    public static void main(String[]args)
    {
        ArrayList PatientList = new ArrayList();
        Scanner scan = new Scanner(System.in);


        System.out.println("Enter how many patient are : ");
        int num = scan.nextInt();

        for(int i=0; i<num; i++)
        {

            System.out.println("Enter patient ID: ");
            String patientId = scan.next();

            System.out.println("Enter patient Name: ");
            String patientName = scan.next();

            System.out.println("Enter the patient's gender: ");
            String gender = scan.next();

            System.out.println("Enter the patient's age: ");
            int age = scan.nextInt();

            System.out.println("Enter patient ward number: ");
            String patientWardNumber = scan.next();

            System.out.println("Enter patient's Bed Number: ");
            int patientBedNumber = scan.nextInt();

            System.out.println("Enter patient's registeration date: ");
            String dateRegistered = scan.next();

            System.out.println("Enter the treatment: ");
            String treatment = scan.next();

            System.out.println("Enter the ward duration: ");
            int wardedDuration = scan.nextInt();



            Patient data = new   Patient(patientId,patientName,gender,age,patientWardNumber,patientBedNumber,dateRegistered,treatment,wardedDuration);
            PatientList.add(data);

        }


        System.out.println("Total patients are: "+PatientList.size());

        System.out.println("Enter patient name : ");


        Patient D= null;
        Object data ;



        String user=scan.next();
        boolean found=false;

        while(!PatientList.isEmpty())
        {
            if(D.getPatientName().equalsIgnoreCase(user))
            {

                found=true;
                System.out.println(D.toString());

            }

            else if (found==false)
            {
                System.out.println("Patient is not found ! TRY AGAIN");

            }

        }

    }
}

我在运行这个编码时遇到了两个问题。首先,我不能为我的病房持续时间输入值。 。第二,当我想搜索我的病人时,它无法显示有关病人的信息。它说空指针异常。我还是初学者在java.someone可以给我看解决方案吗?

This is my output Output

2 个答案:

答案 0 :(得分:2)

因为您尚未在Patient方法中初始化D对象main()

Patient D= null;

并在其上调用方法。

if(D.getPatientName().equalsIgnoreCase(user)){ // Here D is null
      found=true;
     System.out.println(D.toString()); 

}

你必须创建一个Patient对象并初始化变量D,比如

Patient D= new Patient();

答案 1 :(得分:1)

您的病房期限&#34;输入问题;你正在阅读Scanner.next()的字符串。请注意,只能读取一个单词。所以如果你要进入&#34;等级1&#34;例如,治疗,仅限于#34; Level&#34;会被阅读; &#34; 1&#34;将留在流上并可能损坏以后的输入(在您的情况下,&#34; 1&#34;正在nextInt()呼叫读取病房持续时间。)

我建议使用Scanner.nextLine()代替字符串输入。这样它就会读取整行上的每个单词,并允许您输入包含空格而无问题的数据。

至于您的NullPointerExceptionKugathasan Abimaran's answer涵盖的很好。

此外,您还有另一个较小的问题。在Patient构造函数中,您实际上并未初始化任何成员字段。您正在声明并初始化局部变量:

public Patient() {
    String patientId = " ";
    String patientName = " ";
    String patientWardNumber = " ";
    String dateRegistered = " ";
    String treatment = " ";
    int patienBedDuration = 0;
    int wardedDuration = 0;
    int age = 0;
    String gender = " ";
    double price = 0.00;
}

应该是:

public Patient() {
    patientId = " ";
    patientName = " ";
    patientWardNumber = " ";
    dateRegistered = " ";
    treatment = " ";
    patientBedNumber = 0; // <- note: "patienBedDuration" was probably a typo
    wardedDuration = 0;
    age = 0;
    gender = " ";
    price = 0.00;
}

顺便说一句,传统上""用于表示空字符串,而不是" "。此外,将数字字段初始化为0也是多余的,因为无论如何它都是默认的初始值。