2
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");

            }

        }

    }
}

I got 2 problems while running this coding.First i cannot enter value for my ward duration. .Second,when i want to search my patient,it cannot display the information about the patient.Its say null pointer exception.i'm still beginner at java.someone can show me the solution?

This is my output Output

8
  • Do you how to debug a java code? Thanx Commented Mar 15, 2014 at 2:45
  • Ugh. Sooooo much work just to enter in all that information every time. The more I see Scanner, the more I dislike it. Commented Mar 15, 2014 at 2:47
  • 2
    what is an example of what you would submit for ward duration? Commented Mar 15, 2014 at 2:48
  • ArrayList PatientList = new ArrayList(); should be ArrayList<Patient> PatientList = new ArrayList<Patient>(); Commented Mar 15, 2014 at 2:48
  • @aliteralmind Scanner is just a means to read input. You could use any input method and you'd still have to enter the same amount of data. Your complaint is more in the realm of general UI design. For testing, to avoid having to re-enter the info every time, one solution here is to simply temporarily skip that block of code and just directly create a Patient with some relevant values. Commented Mar 15, 2014 at 3:03

2 Answers 2

2

Because you haven't initialized your Patient object D inside the main() method.

Patient D= null;

and invoking a method on it.

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

}

You have to create an object of Patient and initialized variable D like,

Patient D= new Patient();
Sign up to request clarification or add additional context in comments.

Comments

1

For your "ward duration" input problem; you are reading strings with Scanner.next(). Note that this only reads one word. So if you were to enter "Level 1" for, e.g., treatment, only "Level" would be read; the "1" would be left on the stream and could corrupt later input (in your case, the "1" is being read by the nextInt() call for ward duration).

I suggest using Scanner.nextLine() for those string inputs instead. That way it will read every word on the full line, and will allow you to enter data that contains spaces without issue.

As for your NullPointerException, Kugathasan Abimaran's answer covers that well.

Also, you have another smaller issue. In your Patient constructor, you are not actually initializing any of the member fields. You are declaring and initializing local variables instead:

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;
}

Should be:

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

By the way, traditionally "" is used to represent an empty string, not " ". Also, it's redundant to initialize the numeric fields to 0 because that's their default initial value anyways.

1 Comment

@user3422109 Glad you got it solved; you should accept one of the answers here if you found that it solved your problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.