2014-04-28 34 views
0

準確指出錯誤的難點我在第71行。我需要從文本文件中讀取數據,將數據收集到一些treemaps中,以稍後修改它輸出文件。我有一個while循環,通過將文本置於大寫形式來修改數據,然後將數據分割爲每行一個數組,然後將數組數據放入3個treemaps中。該錯誤是一個NullPointerException異常,當我註釋掉該行時,它會將其引發到不同的行上,所以一般而言,我的循環顯然有些問題,但我無法看到它。循環看起來運行時間短於所有行,所以如果文本文件有5行,它將通過4讀取,給我正確的值,然後拋出錯誤。我試過修改文本文件的內容,但沒有用。我試過修改while循環,也無濟於事。任何指針將不勝感激。該文本文件是這樣的:在while循環中使用bufferedreader時得到NullPointerException

110001 commercial 500000.00 101 

110223 residential 100000.00 104 

110020 commercial 1000000.00 107 

550020 land 400000.00 105 

這裏的錯誤代碼:

java.lang.NullPointerException 
    at realestate.RealEstate.main(RealEstate.java:71) 

而且這裏是我到目前爲止的代碼:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package realestate; 

import java.io.BufferedReader; 
import java.io.*; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import static java.lang.System.out; 
import java.nio.file.DirectoryNotEmptyException; 
import java.nio.file.Files; 
import java.nio.file.NoSuchFileException; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.Scanner; 
import java.util.Set; 
import java.util.TreeMap; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* 
*/ 
public class RealEstate { 
    /** 
    * @param args the command line arguments 
    * @throws java.io.IOException 
    */ 
    public static void main(String[] args) throws IOException { 

     Scanner input = new Scanner(System.in);//Prompt user for listings.txt location. 
     String listInput = null; 
     System.out.print("Please enter the real estate listings.txt file's full path: "); 
     listInput = input.next(); 
     if(listInput.contains("listings.txt") == false) {//Verify input with if else statement. 
      System.out.print("\nSorry, the path entered will not work. Please enter the full path to listings.txt: "); 
      listInput = input.next(); 
      if(listInput.contains("listings.txt") == true){//Nested in order to display same message. 
       System.out.print("\nThank you, the agentreport.txt file is now available."); 
      } 
     } else { 
      System.out.print("\nThank you, the agentreport.txt file is now available.\n"); 
     } 
    String[][] listArray; //Initialize array for later data manipulation. 
    listArray = new String[10][5]; 
    listArray = null; 
    Path ar = Paths.get("D:\\JAVA\\RealEstate\\agentreport.txt");//Deletes agentreport.txt if it already exists. 
    try { 
     Files.deleteIfExists(ar); 
    } catch (IOException x) { 
     System.out.print("\nIO Exception, please try again."); 
    } 

    //Reads listings.txt, parses information, places data in array, then inside treemaps. 
    BufferedReader br = null; 
    String[] lineArray = new String[4]; 
    TreeMap tm1 = new TreeMap(); 
    TreeMap tm2 = new TreeMap(); 
    TreeMap tm3 = new TreeMap(); 
    try { 
     br = new BufferedReader(new FileReader(listInput)); 
      String line = br.readLine(); 
     while(line != null){//WHERE I"M GETTING ERROR 
      line = br.readLine();//Read line. 
      line = line.toUpperCase();//Make everything uppercase. 
      lineArray = line.split("\\s+");//Place line into new array based on where spaces are. 
      tm1.put(lineArray[0], lineArray[1]);//Place array items into treemaps. 
      tm2.put(lineArray[0], lineArray[2]); 
      tm3.put(lineArray[0], lineArray[3]); 
      System.out.print(tm1 + "\n" + tm2 + "\n" + tm3 + "\n");//Test if data is received correctly. 
      lineArray = null;//Clear array for next line. 
     } 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.out.print("\nIO Exception, please try again."); 
    } 

    try {//File writer, creates file agentreport.txt and starts writing. 
     File arFile = new File("D:\\JAVA\\RealEstate\\agentreport.txt"); 
     FileOutputStream is = new FileOutputStream(arFile); 
     OutputStreamWriter osw = new OutputStreamWriter(is); 
     Writer w = new BufferedWriter(osw); 
     //w.write();//what I need the writer to do 
     //w.close(); 
    } catch (FileNotFoundException e) { 
     System.err.println("Problem writing to the file, agentreport.txt"); 
    } 


    } 
} 

提前感謝!

+1

哪條線是71? –

回答

0

我幾乎肯定你在誤讀觸發錯誤的行。

String[] lineArray = new String[4]; // this is not needed, it gets overwritten 
... 
try { 
    br = new BufferedReader(new FileReader(listInput)); 
    String line = br.readLine(); 
    while(line != null){ 
    line = br.readLine(); 
    line = line.toUpperCase(); // this will NPE 
    ... 
    lineArray = null; // you don't need this 
    } 
} 

的問題是,你調用readLine()第二次你的while循環中,這意味着line現在可能是null。相反,請執行此操作(注意try-with-resources語法):

try(BufferedReader br = new BufferedReader(new FileReader(listInput))) { 
    String line = null; 
    while((line=br.readLine()) != null){ 
    String[] lineArray = line.toUpperCase().split("\\s+"); 
    tm1.put(lineArray[0], lineArray[1]);//Place array items into treemaps. 
    tm2.put(lineArray[0], lineArray[2]); 
    tm3.put(lineArray[0], lineArray[3]); 
    System.out.print(tm1 + "\n" + tm2 + "\n" + tm3 + "\n");//Test if data is received 
    } 
} 
+0

是的,line.toUpperCase拋出了異常,但是當我評論它時,它會把它扔到下一行,所以我不認爲這是真正的問題。感謝所有的幫助!學習一點一滴。 – user3576714

0

您在循環內調用line = br.readLine();,然後不檢查它是否爲空。

例如,如果你有1行,你會得到NullPointerException

要麼遵循迪馬Goltsman建議或使用掃描儀類是這樣的:

Scanner scanner = new Scanner(new File(listInput)); 
    while (scanner.hasNext()) { 
     String line = scanner.next(); 
     //do the rest of your stuff 
    } 
    scanner.close(); 
+0

我會,但我需要使用BufferedReader。我非常感謝幫助! – user3576714

+0

@ user3576714好的,沒問題 – geoand

2

變化

br = new BufferedReader(new FileReader(listInput)); 
     String line = br.readLine(); 
    while(line != null){//WHERE I"M GETTING ERROR 

br = new BufferedReader(new FileReader(listInput)); 
     String line = null; 
    while((line=br.readLine()) != null){//WHERE I"M GETTING ERROR 

當你在循環內使用br.readLine();時,它仍然可以得到null。所以你需要在條件檢查前使用它

p.s.

在你的代碼中,你跳過第一行。我的代碼也會修復這個問題

+0

工作過,真的很感激! – user3576714