2015-01-05 85 views
3

使用StringReader時出現異常。我創建對象時解析的字符串是通過String.split生成的,它給我NullPointerException。任何建議如何解決這個問題?StringReader從String.Split輸出中給出錯誤

下面的代碼:

public static void main(String[] args) throws IOException { 
    // TODO code application logic here 
    int jmldoc = 5; 
    Hashmap hashsentences[][] = new Hashmap[5][100]; 
    Docreader reader = new Docreader(); 
    System.out.println(reader.doc[1]); 

    for (int i = 0; i < jmldoc; i++) { 
     int j = 0; 
     while (reader.sentences[i][j] != null) { 
      System.out.println(reader.sentences[i][j]); 
      j++;    
      String temp=reader.sentences[i][j]; 
      StringReader h = new StringReader(temp); 

     } 
    } 


} 

和文件讀屏類

public class Docreader { 

    public String sentences[][]=new String[5][100]; 
    public String doc[]=new String[5]; 

    public Docreader() throws IOException{ 

    this.readdoc(); 
    this.splittosentence(); 

    } 

    public void readdoc() throws IOException { 
     for (int i = 0; i < 5; i++) { 
      String temp = new String(); 
      temp = Docreader.readFile("datatrain/doc" + (i + 1) + ".txt"); 
      this.doc[i] = temp; 
     } 

    } 

    public void splittosentence() { 


     for (int i = 0; i < 5; i++) { 
      String temp[]; 
      temp = doc[i].split("\\."); 
      for(int j=0;j<temp.length;j++){ 
      sentences[i][j]=temp[j]; 

      } 

     } 

    } 

    private static String readFile(String fileName) throws IOException { 
     try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { 
      StringBuilder sb = new StringBuilder(); 
      String line = br.readLine(); 

      while (line != null) { 
       sb.append(line); 
       sb.append("\n"); 
       line = br.readLine(); 
      } 
      return sb.toString(); 
     } 
    } 
} 

例外:

Exception in thread "main" java.lang.NullPointerException 
at java.io.StringReader.<init>(StringReader.java:50) 
at stki_d_8_final.STKI_D_8_Final.main(STKI_D_8_Final.java:45) 

Java結果:1

當我檢查線50在StringReader類它包含

this.length = s.length(); 
+0

用完整的堆棧跟蹤發佈您的異常。 –

+0

@ PM77-1我不知道該怎麼做 –

+0

是的,你知道,因爲你已經做到了。 –

回答

4

在這部分代碼:

while (reader.sentences[i][j] != null) { 
    System.out.println(reader.sentences[i][j]); 
    j++;    
    String temp=reader.sentences[i][j]; 
    StringReader h = new StringReader(temp); 
} 

您使用j++所以j增加1的值,那麼你有這樣的代碼:

String temp=reader.sentences[i][j]; 

由於如果與null不同,此數組中的新條目未被評估,它可能包含null值,它是assi改爲temp,因此用null值作爲參數初始化StringReader

使用它來構建StringReader之後,解決此問題的方法將會增加j。此外,在目前的形式下,如果reader.sentences[i]的所有值不是null,則此代碼也可能丟失ArrayIndexOutOfBoundsException。這將是上述代碼的解決方案:

while (j < reader.sentences[i].length && reader.sentences[i][j] != null) { 
    System.out.println(reader.sentences[i][j]); 
    String temp=reader.sentences[i][j]; 
    StringReader h = new StringReader(temp); 
    j++; 
} 
+0

謝謝它的作品,哈哈noob時間。 –

+2

我會說,首發,而不是noob。別客氣。 –

+0

vote up up for better explanation –

3

在您的這部分代碼:

while (reader.sentences[i][j] != null) { 
     System.out.println(reader.sentences[i][j]); 
     j++;    
     String temp=reader.sentences[i][j]; 
     StringReader h = new StringReader(temp); 
} 

你適當地確保reader.sentences[i][j] != null,但是你再增加Ĵ(j++),做做一個空檢查。

+0

感謝:D,對不起noob時間。 –