Java - 获取NullPointerException

时间:2015-05-04 02:42:05

标签: java nullpointerexception

我遇到的问题是我在代码的第59行收到NullPointerException。

该程序的目的是提示用户输入文件位置(具有PI的数字)。然后,程序应通过Scanner类接受任意数量的数字(比如k位)。然后,程序必须从文件中读取PI的k个数字。然后,使用Scanner类,程序应该从用户那里获得0-9的数字并打印它出现的第一个和最后一个位置以及它出现的次数。只考虑小数点后的数字。该程序应该能够接受100,000个数字的PI。

代码的示例输出如下:

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

非常感谢任何帮助。

以下是我的代码:

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();
    }

}
}

1 个答案:

答案 0 :(得分:0)

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

以上while循环将一直运行reader.readLinenull,因此将为stringinput

现在,使用stringinput

进行while循环后
stringinput.indexOf(searchnum)
stringinput.lastIndexOf(searchnum)

从而得到NullPointerException