不应到达的 split() 上的空指针
Null pointer on a split() that shouldn't be reached
所以我正在从 .txt 文件中读取 bikeParts 列表。那部分工作正常。问题是我的缓冲 reader 有一个 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 调用出现空指针。但它不应该达到那个 split()
好吧,如果不编译你的代码,我 99.9% 确定错误出在第二次 readLine() 调用上,你的 while 条件是 (line = br.readLine()) != null
,这意味着你正在读取一行并将其分配给变量 line然后检查该行是否为空。在那之后,出于某种原因,您正在阅读 nextLine() 并且您“丢弃”上一行。因此,如果您从 txt 文件中读取最后一行,则 while 条件将为真,您将从文件设置 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])));
}
所以我正在从 .txt 文件中读取 bikeParts 列表。那部分工作正常。问题是我的缓冲 reader 有一个 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 调用出现空指针。但它不应该达到那个 split()
好吧,如果不编译你的代码,我 99.9% 确定错误出在第二次 readLine() 调用上,你的 while 条件是 (line = br.readLine()) != null
,这意味着你正在读取一行并将其分配给变量 line然后检查该行是否为空。在那之后,出于某种原因,您正在阅读 nextLine() 并且您“丢弃”上一行。因此,如果您从 txt 文件中读取最后一行,则 while 条件将为真,您将从文件设置 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])));
}