Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 1 Post By JosAH
  • 1 Post By Schillers

Thread: Help! nullpointerexception?

  1. #1
    Schillers is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Angry Help! nullpointerexception?

    Hi guys,

    I am trying to create a method to read data from a saved file to load names from a file and display them in the listbox. I keep getting this error, can anyone help?


    Java Code:
    public void loadStaffMembers()
        {
            JFileChooser objFileDialogue = new JFileChooser();
            int intDialogResult = JFileChooser.CANCEL_OPTION;
            intDialogResult = objFileDialogue.showOpenDialog(this);
            
            if(intDialogResult == JFileChooser.APPROVE_OPTION)
            {
                File objFile = objFileDialogue.getSelectedFile();
                
                if(objFile.exists() && objFile.canRead())
                {
                    try(ObjectInputStream objIn = new ObjectInputStream(
                            new BufferedInputStream(
                            new FileInputStream(objFile))))
                    {
                        Object objData = objIn.readObject();
                        StaffList objNewStaffList = (StaffList)objData;
                        
                        if(objNewStaffList != null)
                        {
                            ArrayList<IObserver> objStaffListObservers = 
                                    staffMembers.getObservers();
                            
                            for(IObserver currObserver : objStaffListObservers)
                            {
                                staffMembers.removeObserver(currObserver);
                            }
                            
                            for(IObserver currObserver : objStaffListObservers)
                            {
                                objNewStaffList.registerObserver(currObserver);
                            }
                            staffMembers = objNewStaffList;
                            staffMembers.notifyObservers();
                        } else
                        {
                            JOptionPane.showMessageDialog(this, "No staff members found!", "Error reading file", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                    catch(ClassNotFoundException | IOException | ClassCastException ex)
                    {
                        JOptionPane.showMessageDialog(this, "Data file could not be read", "Operation failed", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "File not found/unreadable", "Error accessing file", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "File load cancelled", "Operation aborted", JOptionPane.INFORMATION_MESSAGE);
            }
            
        }

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at leaveDataModel.StaffList.getObservers(StaffList.ja va:25)
    at leaverecords.gui.staffMembers.loadStaffMembers(sta ffMembers.java:345)
    at leaverecords.gui.staffMembers.mnuLoadActionPerform ed(staffMembers.java:388)
    at leaverecords.gui.staffMembers.access$500(staffMemb ers.java:22)
    at leaverecords.gui.staffMembers$6.actionPerformed(st affMembers.java:161)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton. java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 693)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Help! nullpointerexception?

    Please go through the Forum Rules, particularly the second paragraph.

    I've closed the other thread you started. Moreover, as the question has nothing to do with AWT/Swing, I'm moving this one to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Help! nullpointerexception?

    Quote Originally Posted by Schillers View Post
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at leaveDataModel.StaffList.getObservers(StaffList.ja va:25)
    So, what's on line 25 of StaffList.java?

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Schillers is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help! nullpointerexception?

    Quote Originally Posted by DarrylBurke View Post
    So, what's on line 25 of StaffList.java?

    db
    My apologies. To be honest, I'm that new to Java I thought that was swing...

    Thank you for responding though!

    Java Code:
        public ArrayList<IObserver> getObservers()
        {
            ArrayList<IObserver> arlResult = new ArrayList<>();
            for (IObserver currObserver : observers)                                 //THIS IS LINE 25
            {
                arlResult.add(currObserver);
            }
            return arlResult;
        }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help! nullpointerexception?

    If the NPE exception was thrown on that line then 'observers' was null; b.t.w. if the intention of that method is to return a copy of the observers collection, you can shorten the method to:

    Java Code:
    return new ArrayList<IObserver>(observers);
    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Schillers is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help! nullpointerexception?

    Quote Originally Posted by JosAH View Post
    If the NPE exception was thrown on that line then 'observers' was null; b.t.w. if the intention of that method is to return a copy of the observers collection, you can shorten the method to:

    Java Code:
    return new ArrayList<IObserver>(observers);
    kind regards,

    Jos
    Thank you for all your help guys, was me being an idiot and not adding the line...

    Java Code:
    staffMembers.registerObserver(this);
    Thank you for all your help though!
    Fubarable likes this.

Similar Threads

  1. NullPointerException Help!
    By amazin112 in forum New To Java
    Replies: 20
    Last Post: 04-30-2012, 03:36 AM
  2. NullPointerException
    By mangesh.gho in forum New To Java
    Replies: 11
    Last Post: 01-21-2012, 06:19 AM
  3. nullpointerexception
    By natdizzle in forum New To Java
    Replies: 3
    Last Post: 01-14-2012, 09:57 PM
  4. Why am I getting a NullPointerException
    By dragstang86 in forum New To Java
    Replies: 7
    Last Post: 06-20-2011, 01:38 AM
  5. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 5
    Last Post: 01-07-2010, 01:14 PM

Tags for this Thread

Posting Permissions

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