2017-06-22 98 views
-1

我知道這裏有很多類似的問題,但我仍然無法解決它。我可以得到我想要的所有結果。但最終還是顯示nullpointerexception。我不知道爲什麼。誰能幫忙?通過split()處理txt文件,說Nullpointerexception

public class PointGenterate { 

public static void main(String[] args) throws FileNotFoundException { 
    // TODO Auto-generated method stub 
    try{ 
    File file = new File("123.txt"); 
    double[] pointsid = new double[10]; 
    String[] data = null; 

    for(int i = 0; i <10; i++){ 
     double rn = (int)(Math.random()*120); 
     System.out.println(rn); 
     pointsid[i] = rn; 
    } 
    //read file 
    InputStreamReader rs = new InputStreamReader(new FileInputStream(file));//create input stream reader object 
    BufferedReader br = new BufferedReader(rs); 
    String line = ""; 
    line = br.readLine(); 
    // 
    File write = new File("output.KML"); 
    write.createNewFile(); 
    BufferedWriter out = new BufferedWriter(new FileWriter(write)); 
    while(line != null){ 
     line = br.readLine(); 
     if(line==" "){ 
      System.out.print("empty"); 
     }else{ 
     data = line.split(",|:|[|]"); 
     } 
     for(int i = 0; i < data.length; i++){ 
      data[i] = data[i].trim(); 
      System.out.println(data[i] + "num" + i); 
     } 
     if(data.length > 15){ 
      double id = Double.parseDouble(data[4]); 
      for(int i = 0; i <10; i++){ 
       if(id == pointsid[i]){ 
        data[10] = data[10].substring(0, data[10].length()-2); 
        data[15] = data[15].substring(1,data[15].length()); 
        data[16] = data[16].substring(0, data[16].length()-6); 
        out.write(data[8]+" "+ data[10]+ " " + data[13] + data[15] + data[16]+ "\r\n"); 
        out.flush(); 
       } 
      } 
     } 



     //System.out.println(line); 
    } 



    out.close(); 
    } 
    catch(Exception e){ 
     e.printStackTrace();    
    } 
} 

} 

txt文件格式是這樣

{ "type": "Feature", "properties": { "id": 126.000000, "osm_id": 4851918786.000000, "name": "Moray House Library", "type": "library" }, "geometry": { "type": "Point", "coordinates": [ -3.180841771200988, 55.950622362732418 ] } }, 

這是一條線。我有很多行,實際上這只是一個測試代碼。如果它有效。我想把它寫成一個javaseverlet類的方法。獲取字符串座標並將其返回給我的JS字體結束。

+0

這看起來非常JSON式。你有沒有嘗試過使用JSON庫來處理它? https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Chris

+0

謝謝,你的鏈接是非常有幫助的。夥計 –

回答

1

您的代碼有幾個問題。在本節:

InputStreamReader rs = new InputStreamReader(new FileInputStream(file));//create input stream reader object 
BufferedReader br = new BufferedReader(rs); 
String line = ""; 
line = br.readLine(); // here you read the first line in the file 
// 
File write = new File("output.KML"); 
write.createNewFile(); 
BufferedWriter out = new BufferedWriter(new FileWriter(write)); 
while(line != null){ // here you check that it's not null (it's not, you read the first line OK) 
    line = br.readLine(); // here you read the second line (there is no second line, now line is null) 
    if(line==" "){ // now you check if the line is a space character (this is wrong for 2 reasons, that's not how you compare strings, and a space character is not an empty string) 
     System.out.print("empty"); 
    }else{ 
    data = line.split(",|:|[|]"); // here you call split() on line but line is null 
    } 

當你檢查如果字符串是空的,你做了line == " "這是錯誤的原因有二。首先,您不能使用==來比較字符串 - 請參閱this question以瞭解有關詳細信息。其次," "是一個包含空格字符的字符串。 ""是一個空字符串。

當您想檢查一個字符串是空的,你可以做這樣的:

line.equals("") 

或像這樣:

line.isEmpty() 

這裏有一些小的改動你的代碼,以便它運行時不會拋出異常。

public class PointGenterate { 

    public static void main(String[] args) throws Exception { 
     try { 
      File file = new File("123.txt"); 
      double[] pointsid = new double[10]; 
      String[] data = null; 

      for(int i = 0; i < 10; i++){ 
       double rn = (int)(Math.random()*120); 
       System.out.println(rn); 
       pointsid[i] = rn; 
      } 

      //read file 
      InputStreamReader rs = new InputStreamReader(new FileInputStream(file));//create input stream reader object 
      BufferedReader br = new BufferedReader(rs); 
      String line = ""; 

      // 
      File write = new File("output.KML"); 
      write.createNewFile(); 
      BufferedWriter out = new BufferedWriter(new FileWriter(write)); 
      while((line = br.readLine()) != null){ // read the line and check for null 
       if(line.isEmpty()) { // is the line equal to the empty string? 
        System.out.print("empty"); 
       } else { 
        data = line.split(",|:|[|]"); 
       } 

       for(int i = 0; i < data.length; i++){ 
        data[i] = data[i].trim(); 
        System.out.println(data[i] + "num" + i); 
       } 

       if(data.length > 15){ 
        double id = Double.parseDouble(data[4]); 
        for(int i = 0; i <10; i++){ 
         if(id == pointsid[i]){ 
          data[10] = data[10].substring(0, data[10].length()-2); 
          data[15] = data[15].substring(1,data[15].length()); 
          data[16] = data[16].substring(0, data[16].length()-6); 
          out.write(data[8]+" "+ data[10]+ " " + data[13] + data[15] + data[16]+ "\r\n"); 
          out.flush(); 
         } 
        } 
       } 
       //System.out.println(line); 
      } 
      out.close(); 
     } 
     catch(Exception e){ 
      e.printStackTrace();    
     } 
    } 
} 
+0

謝謝,夥計,我明白了。還有一個問題。 while循環後,我的原始代碼有line = br.readLine(); //在這裏你讀第二行(沒有第二行,現在行是空)。你現在說它是空的,但我的代碼仍然可以運行並得到正確的答案。所以,如果我有這條線,如果它是空的,我怎麼還能運行其餘的代碼。我在這裏聽不懂。此外,當我運行它。如果我有這條線,eclipse控制檯直接顯示結果。如果我刪除這一行,我仍然可以得到結果,而結果只是逐行地繼續顯示在控制檯上。 –

+0

這取決於你的輸入文件。你的文件中是否有多餘的空白行?當我測試你的代碼時,我的輸入文件只有一行(你的JSON字符串)。如果您使用調試器,您可以逐步完成代碼並檢查變量,然後繼續。 – Matt