java - 在 while 循环内使用 bufferedreader 时获取 NullPointerException

标签 java nullpointerexception

很难查明第 71 行出现的错误。我需要从文本文件中读取数据,将数据收集到一些树形图中,以便稍后在两个输出文件中对其进行修改。我有一个 while 循环,它通过将文本置于大写形式来修改数据,然后将数据拆分为每行的一个数组,然后将该数组数据放入 3 个树形图中。错误是 NullPointerException,当我注释掉该行时,它会将其抛出到另一行,因此我的循环显然有问题,我只是看不到它。该循环似乎只运行一次,无法遍历所有行,因此如果文本文件有 5 行,它将读取 4 行,给出正确的值,然后抛出错误。我尝试修改文本文件的内容,但没有成功。我尝试修改 while 循环,也无济于事。任何指示将不胜感激。文本文件如下所示:

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");
     }


    } 
}

提前致谢!

最佳答案

改变

 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。所以你需要在条件检查之前使用它

附注

在您的代码中,您跳过第一行。我的代码也会解决这个问题

关于java - 在 while 循环内使用 bufferedreader 时获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23349581/

相关文章:

java - 如何在java程序中设置JVM参数值

Java - 等待 Runnable 完成

java - Spring 带注释的 Controller 可以工作,但路由器/处理程序方法似乎无法从 *ServerRequest* 检索 *Mono<>*

c++ - 使用 ifstream 时出现无效空指针错误

java - 什么是NullPointerException,我该如何解决?

java - JSF 多个组合框问题

java - jep在java中解释python时出错

java - Slick2D 空指针异常

java - 调用自定义适配器时出现空点异常 : Android

android - java.lang.nullpointerException错误: app crashes