Results 1 to 6 of 6
Thread: Help! nullpointerexception?
- 12-07-2012, 10:15 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 5
- Rep Power
- 0
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)
- 12-08-2012, 03:22 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-08-2012, 03:25 AM #3
- 12-08-2012, 09:58 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 5
- Rep Power
- 0
Re: Help! nullpointerexception?
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; }
- 12-08-2012, 10:45 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:return new ArrayList<IObserver>(observers);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-08-2012, 09:35 PM #6
Member
- Join Date
- Dec 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
NullPointerException Help!
By amazin112 in forum New To JavaReplies: 20Last Post: 04-30-2012, 03:36 AM -
NullPointerException
By mangesh.gho in forum New To JavaReplies: 11Last Post: 01-21-2012, 06:19 AM -
nullpointerexception
By natdizzle in forum New To JavaReplies: 3Last Post: 01-14-2012, 09:57 PM -
Why am I getting a NullPointerException
By dragstang86 in forum New To JavaReplies: 7Last Post: 06-20-2011, 01:38 AM -
Why do I get a NullPointerException?
By nessa203 in forum New To JavaReplies: 5Last Post: 01-07-2010, 01:14 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks