Java-获取NullPointerException
我的代码第59行有一个NullPointerException问题 该程序的目的是提示用户输入文件位置(其数字为PI)。然后,程序应该通过Scanner类接受任意数量的数字(比如k位)。然后,程序必须从文件中读取PI的k位。使用Scanner类,程序应该从用户处获取0-9的数字,并打印它出现的第一个和最后一个位置以及出现的次数。仅考虑小数点后的数字。该程序应能接受100000位PI 代码的示例输出如下所示:Java-获取NullPointerException,java,nullpointerexception,Java,Nullpointerexception,我的代码第59行有一个NullPointerException问题 该程序的目的是提示用户输入文件位置(其数字为PI)。然后,程序应该通过Scanner类接受任意数量的数字(比如k位)。然后,程序必须从文件中读取PI的k位。使用Scanner类,程序应该从用户处获取0-9的数字,并打印它出现的第一个和最后一个位置以及出现的次数。仅考虑小数点后的数字。该程序应能接受100000位PI 代码的示例输出如下所示: Give the location of the file: C:\Users\Joe\
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();
}
}
}
导入java.io.BufferedReader;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.array;
导入java.util.Scanner;
导入java.util.ArrayList;
公共类问题2{
@抑制警告(“空”)
公共静态void main(字符串[]args)引发异常{
FileInputStream inputstream=null;
BufferedReader reader=null;
@抑制警告(“资源”)
扫描仪输入=新扫描仪(System.in);
试一试{
System.out.println(“给出文件的位置(例如:C:\\Users\\Joe\\Desktop\\pi.txt):”;
字符串fileloc=input.nextLine();
inputstream=新文件inputstream(fileloc);
reader=新的BufferedReader(新的InputStreamReader(inputstream));
字符串输入;
System.out.println(“要解析的PI位数:”);
int parsenum=input.nextInt()+2;
String[]stringarray=新字符串[parsenum];
而((stringinput=reader.readLine())!=null){
stringinput=stringinput.substring(2,parsenum);
对于(int i=0;i
上述while
循环将一直运行到reader.readLine
为null
,而stringinput
也是如此
现在,在while循环之后,使用stringinput
:
stringinput.indexOf(searchnum)
stringinput.lastIndexOf(searchnum)
因此,获取
NullPointerException
和第59行将是…?一般经验法则是,如果打开它,应该关闭它。有关更多详细信息,请参阅-这将使您更好地了解如何跟踪和解决此类问题。(这通常不是好问题;每个问题都是由于对程序状态的错误假设而导致的编程错误。)@MadProgrammer似乎op关闭了其try-catch块,但我认为op在try-catch块内生成inputstream和reader,因为NPE?我认为这又回到了范围问题上again@KickButtowski是的,通常您会包装阅读器。如果(reader!=null)
关闭,或者,如果您像我一样懒惰,则关闭另一个try catch
,但既然我们现在有try with resources
,不妨使用它并使其真正干净。
stringinput.indexOf(searchnum)
stringinput.lastIndexOf(searchnum)