0

Here is my code to take a look, I have been fighting with it for an hour now and I think this is the class killing my progress, does the 30 in the error shown at the bottom represent the error being in line 30 but since that is the while loop does that mean the whole loop is flawed? I am wracking my head to figure this out with no luck.

import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;

public class ContributorManager {
   public static void main(String [] args) {
       Scanner inputFile = null;
       String name = null;
       String city = null;
       String country = null;
       String phone = null;
       double contribution = 0;
       int id = 0;
       Contributor c = null;
       TheStack stack = new TheStack();
       Node1 node = null;

       //open contributors file
       try {
           inputFile = new Scanner(new File("contributors.csv"));
           inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));
       }
       catch (Exception e) {
           System.err.println("Error opening file.");
       }

       //create contributors object for each row, and add to the stack
       while (inputFile.hasNext()) {
           name = inputFile.next();
           city = inputFile.next();
           country = inputFile.next();
           phone = inputFile.next();
           contribution = inputFile.nextDouble();
           id = inputFile.nextInt();
           inputFile.nextLine(); //advance to the next line

           c = new Contributor(name, city, country, phone, contribution, id);
           node = new Node1(c); //create a node using the contributor object
           stack.push(node); //add the node to the top of the stack
       }

       stack.pop(); //remove the last node from the top of the stack

       stack.print(); //print the contents of the entire stack
   }
} 

And here is the error exactly as I see it, I am new so I do not have the best ability to find my own errors as often as they happen, thank you.

Error opening file.
Exception in thread "main" java.lang.NullPointerException
    at ip1_second_try/Module.ContributorManager.main(ContributorManager.java:30)
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Feb 27 '20 at 10:23
  • Yes, `(ContributorManager.java:30)` means the error emerged from line 30 in `ContributorManager.java`. – byxor Feb 27 '20 at 10:23
  • I'm puzzled that you even copy and pasted your error message "Error opening file." into the quesiton, but still can't figure out while trying to do something with a file that you failed to open will lead to errors. – OH GOD SPIDERS Feb 27 '20 at 10:25
  • 1
    My *guess* is that you've missed a couple of lines out of what you've shown us, and that line 30 is `while (inputFile.hasNext())`. When you set up `inputFile`, if anything goes wrong you're catching the exception, logging something - but then continuing as if it's fine to try to read from `inputFile` anyway. I suspect that's what's going wrong. Now would be a good time to learn debugging skills though - step through the code line by line. – Jon Skeet Feb 27 '20 at 10:25
  • so it involves the whole while loop then? Should I set them equal to something? – Colton Johnston Feb 27 '20 at 10:26
  • also to oh god spiders, I kind of see it was stupid to post that considering i figured it out but I wanted to know what to set those to more or less as I think I am either confused on what to set it to or how to, I am terrible at explaining myself when tired and I apologize for this – Colton Johnston Feb 27 '20 at 10:28
  • guess you need : while ( inputFile.hasNext() != null) in your while check – Vasanth Raghavan Feb 27 '20 at 10:28
  • 1
    `inputFile` is null because the file open failed in the `new Scanner`. If you get an error opening the file you can't just continue as if all was well. – greg-449 Feb 27 '20 at 10:31

1 Answers1

0

You get the NullPointerException because there was an Exception reading the file, inputFile is still null and at inputFile.hasNext() you are trying to do null.hasNext().

Move the code after the try-catch inside the try, this way you won't execute the rest of the code if you haven't opened the file:

//open contributors file
try {
    inputFile = new Scanner(new File("contributors.csv"));
    inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));

    //create contributors object for each row, and add to the stack
    while (inputFile.hasNext()) {
        name = inputFile.next();
        city = inputFile.next();
        country = inputFile.next();
        phone = inputFile.next();
        contribution = inputFile.nextDouble();
        id = inputFile.nextInt();
        inputFile.nextLine(); //advance to the next line

        c = new Contributor(name, city, country, phone, contribution, id);
        node = new Node1(c); //create a node using the contributor object
        stack.push(node); //add the node to the top of the stack
    }

    stack.pop(); //remove the last node from the top of the stack
    stack.print(); //print the contents of the entire stack
} catch (Exception e) {
    System.err.println("Error opening file.");
}

Note: you shouldn't catch the parent exception Exception because you won't be able to distinguish between an exception from the Scanner or from other part of your code.

Eduard
  • 191
  • 8