split()上的空指针不应该到达

时间:2017-10-15 15:46:34

标签: javafx file-io while-loop split bufferedreader

所以我正在从.txt文件中读取一个bikeParts列表。那部分工作正常。问题是我的缓冲读卡器有一个while循环,它应该检查下一行是否为空。程序循环遍历.txt文件中的每个aprt,但随后尝试读取额外的" null"线。我很困惑为什么程序没有退出while循环。

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.*;
import java.io.*;
import java.nio.*;

public class Main extends Application {
    public static String read(String fileName) {
        String success="success";
        String nope="not found";
        String IO="could not read file";

        File file = new File(fileName);
        String line = null;
        String[] fileNamer = fileName.split(".txt");
        String houseName = fileNamer[0].toString();
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            ArrayList<BikeParts> partList = new ArrayList<>();

            while ((line = br.readLine()) != null) { //the while loop in question
                line = br.readLine();
                String[] elements = line.split(",");  //null pointer on this line
                partList.add(new BikeParts(elements[0], (elements[1]), Double.parseDouble(elements[2]),
                        Double.parseDouble(elements[3]), Boolean.parseBoolean(elements[4]), Integer.parseInt(elements[5])));
            }//BikeParts is a constructor for each part on the .txt file. makes a part object out of partName, partNumber, listPrice, salesPrice, onSale, and quantity
            Warehouse house = new Warehouse(houseName, partList);  //partList is the ArrayList of parts that is contained within the Warehouse that was just created by the user
            return success;  //debug return statement
        } catch (FileNotFoundException e) {
            return nope;  //debug return statement
        } catch (IOException e) {
            e.printStackTrace();
            return IO;  //debug return statement
        }
    }

    public void display() {

    }

    public void newPart(BikeParts p) {
        MainWarehouse.add(p);
    }

    public void sortMainByName(MainWarehouse houseName) {


    }

    public void sortByNumber() {

    }

    public void addVan(String vanName, ArrayList<BikeParts> invName) {
        //Vans.Vans(vanName,invName);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("GUI.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

.txt文件:

26inTube,1234567891,7.00,5.58,true,35
10spFrontDerailer,1234567897,41.00,31.50,true,10
seatPost,1234567892,17.00,1.23,true,5
700-25SwhalbeTire,1234567895,51.00,40.50,true,10
carbonHandleBars42cm,1234567893,47.00,5.58,true,3
10spRearDerailuer,1234567896,82.00,70.50,true,10
11spFrontDerailuer,1234567899,61.00,50.50,true,10
WTB_saddle,1234567890,33.00,25.58,false,15
mensBibsMedium,1234567900,110.00,99.00,false,4
womensHelmetSmall,1234567901,130.00,79.00,false,4
spdPedals,1234567902,62.31,79.00,true,4
700-23SwhalbeTire,1234567894,51.00,40.50,true,10
timePedals,1234567903,102.31,89.00,false,4
frogPedalsTitanium,1234567904,142.31,130.00,false,4
11spRearDerailuer,1234567898,97.00,80.50,true,10
carbonWheelSet,1234567905,542.31,480.00,false,4
mountainFork29,1234567912,223.00,195.00,false,4
lynskeyTitaniumFrame265Med,1234567906,2542.99,1880.00,false,2
grr,1234567907,4567.89,3456.78,false,2
zarminComputer,1234567908,543.21,480.00,false,2
womensBibsMedium,1234567909,110.00,99.00,false,4
womensJacketMedium,1234567911,120.00,95.00,false,4
mensJacketMedium,1234567910,120.00,95.00,false,4

这是一个.FXML项目,所以有更多的类,但这是我唯一遇到的问题。为什么它会经历那个while循环?我在调试模式和最后一次运行中运行程序,line = null,这会导致拆分调用上的空指针。但它不应该达到split()

1 个答案:

答案 0 :(得分:2)

在没有编译代码的情况下,我99.9%确定错误是在第二次readLine()调用时,你的while条件是(line = br.readLine()) != null这意味着你正在读取一行并将它分配给变量行并在之后您正在检查该行是否为空。之后由于某种原因,您正在阅读nextLine()并且您“丢弃”前一行。因此,如果您从txt文件中读取最后一行,则while条件将为true,您将从文件设置line = null再次读取,因此尝试拆分空字符串将为您提供NullPointerExpection

为了解决您的问题,请删除第二个readLine()调用

while ((line = br.readLine()) != null) { 
    String[] elements = line.split(",");  
    partList.add(new BikeParts(elements[0], (elements[1]), Double.parseDouble(elements[2]),
            Double.parseDouble(elements[3]), Boolean.parseBoolean(elements[4]), Integer.parseInt(elements[5])));
}
相关问题