Reading from file - NullPointer exception
Hello
I am reading in data from a file and creating objects with that data.
I have came across a NullPointerException which is really buggin me, i have tried everything!
What i intend on doing within my code is this:
Create a loop which reads in the data line by line.
With that line i am creating objects which will be stored in a temporary array of the object type
After the objects have been stored in the temporary array i am then calling a method which assigns that object to the class attribute
Heres my code for the reading in of file
Code:
//below reads lecturers
String [] LecturerField;//array to store data after read
Lecturer []sLecturer = new Lecturer[numL];// temp array of lecturers
for (int i = 0; i < numL; i++) {
line = in.readLine();
if (line == null)
break;
LecturerField = line.split(",");
sLecturer[i] = new Lecturer(LecturerField[0], LecturerField[1], LecturerField[2],
Integer.parseInt(LecturerField[3]), Integer.parseInt(LecturerField[4]));
//line 185 head.addLecturer(sLecturer[i]);
}
Heres the method which assigns the object to the class attribute
Code:
public void addLecturer(Lecturer lecturer){
//line 32 below
if (numLecturers < lecturers.length) {
lecturers[numLecturers++] = lecturer;
}
}
Here is my error when i try reading the file
Code:
xception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at UniversityModel.HeadOfSchool.addLecturer(HeadOfSchool.java:32)
at UniversityModel.School.load(School.java:185)
at GUI.Main.jMenuLoadActionPerformed(Main.java:142)
at GUI.Main.access$000(Main.java:20)
at GUI.Main$1.actionPerformed(Main.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
BUILD SUCCESSFUL (total time: 1 minute 51 seconds)