所以我從.txt文件讀取bikeParts的列表。這部分工作正常。麻煩的是我有一個while循環用於緩衝讀取器,它應該檢查下一行是否爲空。該程序循環遍歷.txt文件中的每個aprt,但隨後嘗試讀取額外的「空」行。我很困惑,爲什麼程序沒有退出while循環。不應該到達的split()上的空指針
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,這會導致分割調用上的空指針。但它不應該達到那個分割()
伴侶你是一個救生員,非常感謝你。它做到了 –
很高興能夠提供幫助,我們傾向於相信我們的代碼能夠像我們期望的那樣工作,所以對於像這樣的未來問題,只需在每行上插入一條打印語句以查看真正發生的事情 – JKostikiadis
多數民衆贊成我試圖用調試模式,我只是沒有做出我丟棄第一行的連接。我想我實際上並不知道while語句中的任務卡住了,我認爲這只是一個條件測試 –