I have four classes. Project Class, Student Class, ProjectFile Class and ProjectFrame JFrame.
The ProjectFrame is only for GUI and i haven’t touched that.
The Student and Project Class are constructors and i’ve coded those.
Now i’m trying to implement the ProjectFile class by reading from a text file and then storing the data to be read. I am having trouble as i’m not sure why the Instance of the Project Class isn’t storing the data. I’ve looked at my loops and i did print the variables to be sure that the loop is actually happening. It works for the first time but when i try to call the second array, it gives me a NullPointerException. So i’m assuming it’s storing the value as null but that shouldn’t be the case.
This is my ProjectFile Class
/*
* 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();
}
}
This is the text file i’m reading from
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
Can someone please explain to me where i’m coding this wrongly? I can’t figure it out.
EDIT:
This is the Project Class
/*
* 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;
}
}
I guess your code crashes here
In the first loop pJect[1] will contain null which causes a crash
You probably intend