0

I'm working on a program for a game that reads in formatted data from a text file and then analyzes the probability of a certain stat being rolled on a given item. The particular method at the source of the problem, takes in two pieces of user input and filters through the text file accordingly.

public ArrayList<String> analyze(String type, int ilvl){
    strImplicits = new ArrayList<>();
    // make a new item based on the user input
    userItem = new Item(type, ilvl);

    try {
        // File reading shit
        BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/src/ImplicitList.txt")));

        //File implicitList = new File("ImplicitList.txt");
        //Scanner fileReader = new Scanner(implicitList);

        String line = "";
        String name = "";
        String effect = "";
        int implicitilvl = 0;

        while ((line = reader.readLine()) != null){

            if (line.equals("$")) {
                line = reader.readLine();

                // splitting the line and grabbing data
                String[] info = line.split("\t");
                name = info[0];
                implicitilvl = Integer.parseInt(info[1]);
                effect = info[2];

            } else if (!line.equals(null) && !line.equals("$")){

                String[] tag = line.split(" ");
                int weight = Integer.parseInt(tag[1]);

                if (userItem.getTags().contains(tag[0]) && weight != 0 && ilvl >= implicitilvl) {
                    userItem.addRoll(name, effect, weight);
                }
            } // end else-if
            if (line == null)
                break;
        } // end of while
        /**
         * Iterate through each line of the text file, looking for the $ delimiter.
         * After we find the & character we need to check the mod's tags to see if it applies
         *
         * if yes, add the weighting to the int variable that represents the pool of mods
         *      probably would need an arrayList to store and print out the possible variables
         */
    } catch (FileNotFoundException e) {
        e.getMessage();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // string representation of all the possible implicits to be used in combo box
    for (Implicit imp: userItem.getPotRolls())
        strImplicits.add(imp.toString());
    return strImplicits;
}

The error message I receive is:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at Driver.alternative(Driver.java:34)
    at VaalUI.handle(VaalUI.java:158)
    at VaalUI.handle(VaalUI.java:17)

where Driver.java:34 is the line where I initialize the BufferedReader object. When debugging the program won't execute past this line so I can't see it being a problem caused by the rest of the code in the while loop (though that code may cause other issues, I just haven't gotten far enough to tell). I've tried looking at other solutions to similar error messages but none of them have worked for my case. Any help welcome, thanks.

ThomasJazz
  • 99
  • 9
  • 4
    so don't you think that `getClass().getResourceAsStream("/src/ImplicitList.txt")` is maybe returning null? – Scary Wombat Oct 30 '17 at 02:44
  • @ScaryWombat I've tried putting the file in multiple locations to no avail. Should the text file generally be in the same directory as the class accessing it? Oh and yeah sorry for the vulgarity in the comments, this is just a project I'm working on with a buddy so our commenting wasn't meant to be seen by anyone else – ThomasJazz Oct 30 '17 at 02:46
  • 3
    `/src/` should not be used in a resource path since this directory does not exist within resources. The path is relative to the path of the class files, not the user's working directory. – Hovercraft Full Of Eels Oct 30 '17 at 02:47
  • a very quick search reveals https://stackoverflow.com/a/16571046/2310289 – Scary Wombat Oct 30 '17 at 02:48
  • @HovercraftFullOfEels Thanks, this worked. Guess I need to learn more about referencing files in the project directory. – ThomasJazz Oct 30 '17 at 02:49
  • So, not a duplication with _What is NullPointerException_, but _getResourceAsStream returns null_ – Alex Oct 30 '17 at 03:51

0 Answers0