Results 1 to 6 of 6
- 07-08-2010, 04:39 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
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
Heres the method which assigns the object to the class attributeJava 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]); }
Here is my error when i try reading the fileJava Code:public void addLecturer(Lecturer lecturer){ //line 32 below if (numLecturers < lecturers.length) { lecturers[numLecturers++] = lecturer; } }
Java 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)
- 07-08-2010, 05:06 PM #2
What variable is NULL at line 32? Check to see that your code has given it a value before you call the method.
- 07-08-2010, 05:20 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
I'm guessing it would be the number of lecturers variable.
I have set this to 0 for the time being, but if i have no lecturers in the lecturer object array then the length would be 0 too, would i be correct in thinking that?
Here is the declaration for the array attribute which holds the lecturer objects
Java Code:public class HeadOfSchool extends Lecturer { // Attributes private Lecturer[] lecturers;
- 07-08-2010, 05:23 PM #4
That defines a variable: lecturers but does NOT give it a value. It's value will be NULL. Your code needs to test if its null.private Lecturer[] lecturers
- 07-08-2010, 05:25 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 15
- Rep Power
- 0
Yes, i know.
Thats what i am trying to do is give the variable a value by reading in data from a file and assigning that data to an object then assign the object to the variable
- 07-08-2010, 05:34 PM #6
Similar Threads
-
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
Nullpointer exception, even tho I can print data from the file!
By Addez in forum New To JavaReplies: 12Last Post: 01-04-2010, 03:53 PM -
NullPointer.exception in main (arrays)
By Jana in forum New To JavaReplies: 5Last Post: 02-20-2009, 06:41 PM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks