파일에서 정보를 읽으려고합니다. 파일의 첫 번째 것은 정수이지만 읽으려고하면 NullPointerException이 발생합니다. 또한 파일의 첫 번째 내용을 문자열로 읽으려고 시도했지만 NullPointerException이 다시 발생했습니다. 그런 다음 파일을 연결할 때 catch 문에있는 print 문을 추가했습니다. 코드를 실행하면 print 문이 평가됩니다. 내 문제의 근원은 무엇입니까? 감사. 무슨 일이 일어나고 있는지 코멘트에서스캐너에서 파일에서 int를 읽는 동안 NullPointerException이 발생했습니다.
private boolean collectSystem(String loc) {
Scanner fileIn = null;
try {
fileIn = new Scanner(new File(loc));
} catch (FileNotFoundException e) {
System.out.println("File not found, IntakeSystem");
}
// Determine number of equations
try {
n = fileIn.nextInt();
} catch (InputMismatchException e) {
return false;
}
// Collect the text in the file as a string
String info = "";
while (fileIn.hasNextLine()) {
info = info + fileIn.nextLine();
}
fileIn.close();
//Separate equations in file
String[] eqns = new String[n];
int start = 0;
int end = info.indexOf(";");
for (int i = 0; i < n; i++) {
if(end == -1) return false;
String nextLine = info.substring(start, end);
eqns[i] = nextLine;
start = end + 1;
end = info.indexOf(";", start);
}
for (int i = 0; i < n; i++) {
System.out.println(eqns[n]);
}
return true;
}
정확히 NPE를 받고있는 코드에 표시 할 수 있습니까? – sanbhat
당신의 코드가'FileNotFoundException'을 던지고있는 것 같지만, 예외를 삼킨 (그리고 그것을 출력하기 때문에), 코드는'fileIn.nextInt()'로 이동하고'fileIn'에 NPE를 던집니다. –
catch 블록에서 개발하는 동안'e.printStackTrace()'를 사용하십시오. – Braj