我有四节课。项目类、学生类、ProjectFile 类和 ProjectFrame JFrame。
ProjectFrame 仅适用于 GUI,我还没有触及它。
Student 和 Project Class 是构造函数,我已经对它们进行了编码。
现在我试图通过从文本文件中读取然后存储要读取的数据来实现 ProjectFile 类。我遇到了麻烦,因为我不确定为什么项目类的实例不存储数据。我查看了我的循环,并且确实打印了变量以确保循环确实发生了。它第一次工作,但是当我尝试调用第二个数组时,它给了我一个 NullPointerException。所以我假设它将值存储为 null 但事实并非如此。
这是我的 ProjectFile 类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DecelAssignment;
import java.io.*;
/**
*
* @author Zane
*/
public class ProjectFile {
private static Project[] pJect;
private static Student[] sDent;
private static Project ja;
private static BufferedReader br;
public static void readData() {
File inputFile = new File("projects.txt");
try {
br = new BufferedReader(new FileReader(inputFile));
String s = br.readLine();
pJect = null;
pJect = new Project[Integer.parseInt(s)];
//System.out.println(s);
for (int i = 0; i < pJect.length; i++) {
s = br.readLine();
if (s == null) {
break;
} else {
String sLine[] = s.split(",");
int count = 3;
// for (int i2 = 0; i2 < Integer.parseInt(sLine[3]); i2++) {
// sDent[i2] = new Student(sLine[count+1], sLine[count+2], sLine[count+3], sLine[count+4]);
// count += 4;
// }
pJect[i] = new Project(sLine[0], sLine[1], sLine[2], sDent);
System.out.println(pJect[1].getTitle());
System.out.println(sLine[0]);
System.out.println(i);
}
}
} catch (IOException e) {
System.out.println("I caught an IO Exception1");
}
// } catch (NullPointerException e) {
// e.printStackTrace();
// System.out.println("I caught a Null Pointer Exception!");
//
// }
}
// public Project[] getProjectInfo() {
//
//
// return;
// }
public static void main(String[] args) {
readData();
}
}
这是我正在读取的文本文件
3
iPhone App,EEE,John Tan,1,P109520,Kelvin Tay,DBIT,M
iPad App,DMIT,Mark Goh,3,P106286,Felicia Wong,DIT,F,P101803,Rachel Chang,DIT,F,P100036,Lewis Poh,DBIT,M
Green Living,DMIT,Audrey Lim,2,P101234,Peter Chua,DIT,M,P103287,Ng Ming Shu,DISM,F
有人可以向我解释我在哪里编码错误吗?我想不通。
编辑:
这是项目类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DecelAssignment;
/**
*
* @author Zane
*/
public class Project {
private String title, school, supervisor;
private Student[] stDent;
public Project() {
title = "";
school = "";
supervisor = "";
stDent = new Student[0];
}
public Student[] getStDent() {
return stDent;
}
public Project(String title, String school, String supervisor, Student[] stDent) {
this.title = title;
this.school = school;
this.supervisor = supervisor;
this.stDent = stDent;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getSupervisor() {
return supervisor;
}
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}