0
我正在創建一個函數來加載文本文件,問題是,而不是你期望從文本文件格式(名稱| ID |地址|等),它是這樣呈現;從文本文件創建對象
OBE兩個Canobi
專家
假
JK001
F010
的任務
編程和計算機支持
下一個文本文件,我的程序必須能夠加載這組8.這裏的任何數額是我想出了,但不工作無論出於何種原因,它會重新加載來自第一次輸入的相同行。
//Custom function to load files from given inputs
public static void loadFile(String location,String file,String type) {
//Using try/catch to retrieve errors
WorkUnit[] wu = null;
try {
//Using the buffered reader to load the file.
BufferedReader in = null;
try {
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("C:\\Users\\kenji\\IdeaProjects\\Assingment01C_LaTrobe\\src\\workunit.txt")));
lnr.skip(Long.MAX_VALUE);
int length = lnr.getLineNumber() + 1;
print(length + "");
lnr.close();
in = new BufferedReader(new FileReader("C:\\Users\\kenji\\IdeaProjects\\Assingment01C_LaTrobe\\src\\workunit.txt"));
wu = new WorkUnit[length/8];
print(wu.length + "");
String currentLine;
for(int i=0;i<length/8;i+=8) {
Crew cr = new Crew();
Ship ship = new Ship();
cr.setName(in.readLine());
cr.setClassification(getCrewClassification(in.readLine()));
cr.setStatus(getTrueFalse(in.readLine()));
cr.setId(in.readLine());
cr.setPoints(Integer.parseInt(in.readLine()));
ship.setId(in.readLine());
ship.setShipStatus(getShipStatus(in.readLine()));
ship.setPurpose(in.readLine());
wu[i] = new WorkUnit(ship,cr);
print(wu[i].toString());
}
} finally {
in.close();
}
} catch (Exception ex) { ex.printStackTrace(); }
for(int i = 0; i < wu.length;i++) {
print(wu[i].toString());
}
}
這裏是輸出
16
2
WorkUnit{ship=Ship{id='F010', purpose='Programming and Computer Support', shipStatus=on_task}, crew=Crew{id='JK001', name='Obe Two Canobi', experiencePoints=125, status=false, classification=Specialised}, available=true}
WorkUnit{ship=Ship{id='F010', purpose='Programming and Computer Support', shipStatus=on_task}, crew=Crew{id='JK001', name='Obe Two Canobi', experiencePoints=125, status=false, classification=Specialised}, available=true}
Exception in thread "main" java.lang.NullPointerException
at SpaceCity.loadFile(SpaceCity.java:131)
at SpaceCity.addMenuChoice(SpaceCity.java:81)
at SpaceCity.MainMenu(SpaceCity.java:32)
at SpaceCity.main(SpaceCity.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code 1