mercredi 26 mai 2021

Not recongising variable [duplicate]

Why am I getting a java.lang.NullPointerException:

        Doctor[] doctor = new Doctor[MAXdoc];
        String name, specialisation;
        PrintWriter outputStream = null;
        String fileName = "VetManagement.txt"; //This name can be read for the user to find the file.
        
        try
        {
            outputStream =
             new PrintWriter(
             new FileOutputStream(fileName, true)); //This is the append from a old file.
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Error: Saving data " + fileName);
            System.exit(0);
        }
        
            System.out.print("What is the doctors name? ");
                  name = console.next();
                                  String clearCode = console.nextLine(); 
        for(int i =0; i < totalDoc; i++)
        {
            if(name.equalsIgnoreCase(doctor[i].getName()))
            {
                System.out.println("That name is already taken, please enter a new name: ");
                    name = console.next();
            }
        }
    

I have declared totalDoc = 0, and it does not recognise this as it is an array it should work as it starts to count at 0. I have the same method for a pet array and it works. Why is this the case? What is a NullPointerException, and how do I fix it?

Initialize variables:

Scanner console = new Scanner(System.in);
    
    
    int end;
    int totalDoc = 0;
    int totalPet = 0;
    int MAXdoc = 4; //doctor array size. (max size) 
    int MAXpet = 5; //pet array size. (max size)
    
    Doctor [] doctor = new Doctor[MAXdoc]; //Docter array
    Pet [] pet = new Pet[MAXpet]; //pet arry

Aucun commentaire:

Enregistrer un commentaire