Results 1 to 6 of 6
  1. #1
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default 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
    Java 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
    Java 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
    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)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    What variable is NULL at line 32? Check to see that your code has given it a value before you call the method.

  3. #3
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    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;

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    private Lecturer[] lecturers
    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.

  5. #5
    Sage25 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Arrays need to be defined before they are used.
    Lecturer[] lecturers = new Lecturer[5]; // create a 5 element array

    Now lecturers is an array with 5 slots where you can put Lecturer objects.
    lecturers[0] = new Lecturer(); // put a Lecturer object in slot 0

Similar Threads

  1. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 05:44 AM
  2. Replies: 12
    Last Post: 01-04-2010, 04:53 PM
  3. NullPointer.exception in main (arrays)
    By Jana in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 07:41 PM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 02:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 04:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •