2015-05-04 37 views
0

我在我的代碼的第59行出現NullPointerException時出現問題。Java - 獲取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(); 
    } 

} 
} 
+3

而59行將是...? – MadProgrammer

+1

作爲一般經驗法則,如果你打開它,你應該關閉它。有關更多詳細信息,請參見[try-with-resources語句](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) – MadProgrammer

+0

請閱讀http://stackoverflow.com/questions/218384/what-is-null-pointer-exception-and-how-do-i-fix-it - 這會讓你更好地瞭解如何追蹤和解決這些問題。 (這通常是*不是很好的問題;每一個都是由於對程序狀態的錯誤假設而導致的編程錯誤。) – user2864740

回答

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

以上while循環運行,直到reader.readLinenull等都將成爲stringinput

現在用stringinput while循環的後:

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

,從而得到NullPointerException

相關問題