Hey guys! I'm working on this project of mine and I can't seem to get it to work. It keeps saying that I haven't initialized the variables: name, address, email, contactNumber, numberOfVisits.
I currently have 3 classes in the package: PagamutanClinic, Clinic, and Patient.

Here's my Main:

import java.util.Scanner;
public class PagamutanClinic {

    public static void main(String[] args) {
        Clinic clinic = new Clinic();
        Scanner s = new Scanner(System.in);
        String dateOfVisit, nameOfDoctor, assessment; 
        String name, address, email, contactNumber;
        int numberOfVisits;
        int option;

        System.out.println("Enter date of visit: "); 
        dateOfVisit = s.next(); 
        System.out.println("Enter name of doctor: ");
        nameOfDoctor = s.next();
        System.out.println("Assessment Summary: ");
        assessment = s.next(); 

        clinic.addPatient(new Patient (name, address, email, contactNumber, numberOfVisits));
    }
}

Here's my Clinic class:

import java.util.ArrayList;
public class Clinic {
    private ArrayList <Patient> patients = new ArrayList<Patient>(); 

    public void addPatient (Patient patient){
        patients.add(patient);
    }


}

And lastly, here's my Patient class:

public class Patient {
    private String name;
    private String address; 
    private String email;
    private String contactNumber; 
    private int numberOfVisits;

   public Patient(String name, String address, String email, String contactNumber, int numberOfVisits){
       this.name = name;
       this.address = address; 
       this.email = email;
       this.contactNumber = contactNumber; 
       this.numberOfVisits = numberOfVisits;
   }

   public String getName(){
       return name; 
   }

   public void setName(String name){
       this.name = name; 
   }

   public String getAddress(){
       return address; 
   }

   public void setAddress(String address){
       this.address = address;
   }

   public String getEmail(){
       return email;
   }

   public void setEmail(String email){
       this.email = email; 
   }

   public String getContactNumber(){
       return contactNumber; 
   }

   public int getNumberOfVisits(){
       return numberOfVisits;
   }

   public void setNumberOfVisits(int numberOfVisits){
       this.numberOfVisits = numberOfVisits;
   }

   public void numberOfVisits(){

   }


}

I'm kinda new to programming so please bear with me if it happens to be a simple fix. Thanks guys! :D

Recommended Answers

All 6 Replies

You have created the variables, but you havent initialized them. In order to initialize them you need to give them a value to start off at. For example String a; - this is just created String a = ""; - is created and initialized to an empty string. another one is with ints. int a; - created int a = 0; is created and initialized.

sirlink: no, not really.
String a; just creates a reference, it doesn't initializes it, so, String a; => a is not "";

if a would be an instance variable, it would get the default value for an object, which is null. not something you would want to pass, but better.
but since it's not an instance, but a local variable, it doesn't get a default value, not null. it's not initialized.

name, address, email, ... are not initialized, they're just references that don't really reference to anything.

When in doubt read the Java Language Spec...

4.12.5 Initial Values of Variables
...
A local variable (§14.4, §14.14) must be explicitly given a value before it is
used, by either initialization (§14.4) or assignment (§15.26), in a way that can be
verified using the rules for definite assignment

@stultuske
I stated that String a; creates the variable (creates is used to describe the pointer in less technical terms). After that I stated that I stated that String a = ""; creates and initializes it. Though you are right that I did leave out the objects being null.

In any case the OP will want to set default values for each of the variables.

that, or better yet, read them from the command line through his scanner instance.
at this point, the references aren't even null. they would be if they were instance members, but they aren't.

public void isStringNull(){
  String test;
  if ( test == null )
    System.out.println("null");
  else
    System.out.println("not null");
}

the op might be mistaken thinking that this 'll print 'null', but it won't. it won't even compile. test is a reference variable, but it doesn't refer to anything, since it hasn't been initialized.

... it won't even compile ...

That's the inportant part. The code isn't (part of) a valid Java program, so there's nothing meaningful you can say about any variables or values. You only have variables and values when you have a compilable Java program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.