準確指出錯誤的難點我在第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");
}
}
}
提前感謝!
哪條線是71? –