-1

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
Zachary
  • 31
  • 8
  • What is `console`? – dan1st May 26 '21 at 09:06
  • if you have `totalDoc = 0` then `i – sanjeevRm May 26 '21 at 09:06
  • @dan1st console is the Scanner – Zachary May 26 '21 at 09:07
  • @sanjeevRm What should I replace it with, and why does it work with my pet method – Zachary May 26 '21 at 09:08
  • Where did you initialize`totalDoc` and `console`? – dan1st May 26 '21 at 09:08
  • @dan1st I initialized both of them at the start – Zachary May 26 '21 at 09:09
  • We can't help you without a [mcve]. And please also add the exact line the exception is pointing to. I'm assuming your `doctor` array contains only `null` values, which, as explained [in this answer](https://stackoverflow.com/a/23852556/479156) in the duplicate target, throws a NPE. But without a [mcve] we can only guess. – Ivar May 26 '21 at 09:18
  • @Ivar all my integers are set to 0, and the strings have nothing set to them. I have had a look at the answer (duplicate) this does not help me as I have already done this. I will add more code. – Zachary May 26 '21 at 09:20
  • @Zachary Your code seems to confirm my suspicion. You create an array of 4 `Doctor` elements, but you never assign a value to each individual element. By default they are `null`, so your `doctor` array contains `[null, null, null, null]`. You can't call `.getName()` on an a `null` value. I assume you try to get this data from `VetManagement.txt`? You never seem to be doing anything with that output stream though. – Ivar May 26 '21 at 09:30
  • @Ivar when the code runs it makes a new doctor for the doctor array and it checks for all the doctors that exist it does this by using the counter 'totalDoc' and it runs the amount of time of the value of that. When it checks for the names it only does it for the doctors that exist. – Zachary May 26 '21 at 09:32
  • @Zachary I'm afraid I can't help you without more details. I do not get that error when I run your code: https://onecompiler.com/java/3wynbkd6d – Ivar May 26 '21 at 09:41
  • @Ivar I am sorry to say that makes zero sense to me, I don't think you can help thanks for trying man and If I were to add most of the code it would be about 600lines I can't do that as it is an assessment task – Zachary May 26 '21 at 09:44
  • Your problem is all about a basic NullPointerException. Where is your stacktrace? It tells you which line throws the exception -- which line in your code is it? This is all spelled out in the duplicate, please read it and use the information that it holds to improve your question. – Hovercraft Full Of Eels May 26 '21 at 10:35

0 Answers0