0

I have a problem with where I am getting a NullPointerException on line 59 of my code.

The program's purpose is to prompt the user for the file location (which has the digits of PI). The program should then accept any number of digits (say k digits) via the Scanner class. Then, the program must read k digits of PI from the file. Using the Scanner class, the program should then obtain a number for 0-9 from a user and print the first and last position in which it appears and also the number of times it appears. Only digits after the decimal point are to be considered. The program should be able to accept 100,000 digits of PI.

The sample output of the code is below:

Give the location of the file:
C:\Users\Joe\Desktop\pi.txt

Number of digits of PI to parse:
10

Give any number between 0-9:
1

1 appears 2 times
First position in which appears: 1
Last position in which appears: 3

Any help would be much appreciated.

Below is my code:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.ArrayList;


public class Problem2 {
    @SuppressWarnings("null")
    public static void main(String[] args) throws Exception {
    FileInputStream inputstream = null;
    BufferedReader reader = null;

    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);

    try {
        System.out.println("Give the location of the file (example: C:\\Users\\Joe\\Desktop\\pi.txt):");
        String fileloc = input.nextLine();

        inputstream = new FileInputStream(fileloc);
        reader = new BufferedReader(new InputStreamReader(inputstream));
        String stringinput;

        System.out.println("Number of digits of PI to parse: ");
        int parsenum = input.nextInt() + 2;
        String[] stringarray = new String[parsenum];

        while((stringinput = reader.readLine()) != null) {
            stringinput = stringinput.substring(2, parsenum);

            for(int i = 0; i < stringinput.length(); i++) {
                stringarray = stringinput.split("");
            }
        }

        System.out.println("Give any number between 0-9: ");
        String searchnum = input.next();

        int count = 0;

        for(int i = 1; i < parsenum - 1; i++) {
            if(searchnum == stringarray[i]) {
                count++;
            }
            else count++;
        }

        System.out.println(searchnum + " appears " + count + " time(s)");

        for(int i = 1; i < parsenum - 1; i++) {
            System.out.print(stringarray[i]);
        }

        System.out.println();
        System.out.println("First position in which " + searchnum + " appears: " + stringinput.indexOf(searchnum));
        System.out.println("Second position in which " + searchnum + " appears: " + stringinput.lastIndexOf(searchnum));

    } 
    catch (FileNotFoundException exception) {
        System.err.println("File not found, please try again");
        main(null);
    }
    catch (Exception e) {
        System.err.println("Invalid input entered");
        e.printStackTrace();
        System.exit(0);
    }
    finally {
        reader.close();
    }

}
}
Onur Olmez
  • 37
  • 3
  • 3
    And line 59 would be...? – MadProgrammer May 04 '15 at 02:44
  • 1
    As general rule of thumb, if you open it, you should close it. See [The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) for more details – MadProgrammer May 04 '15 at 02:44
  • Please read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it - this will give you a better idea of how to track down and solve these sorts of issues. (Which are generally *not* good questions; each one is a programming error caused by an incorrect assumption about the program state.) – user2864740 May 04 '15 at 02:46
  • @MadProgrammer it seems the op close its try catch block but I think the op make inputstream and reader inside the try catch block and cuz the NPE? **I think it goes back to scoping issue again** – Kick Buttowski May 04 '15 at 02:57
  • 1
    @KickButtowski Yes, typically you would wrap the `reader.close` either in a `if (reader != null)` or, if you're lazy like me, another `try-catch`, but since we now have `try-with-resources`, might as well use that and make it truly clean. – MadProgrammer May 04 '15 at 02:59

1 Answers1

0
while((stringinput = reader.readLine()) != null) 

The above while loop will run untill reader.readLine is null and so will be stringinput.

Now after the while loop your using stringinput:

stringinput.indexOf(searchnum)
stringinput.lastIndexOf(searchnum)

and thus getting the NullPointerException.

Samurai
  • 3,724
  • 5
  • 27
  • 39