• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException is killing me  RSS feed

 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to run this program and when I click on a button the console spits out this jargon at me:

java.lang.NullPointerException
at addressbook.AddressBookGUI.addEntry(AddressBookGUI.java:71)
at addressbook.AddressBookGUI$AddButtonHandler.actionPerformed(AddressBookGUI.java:87)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


I figure there is something wrong with this code:



Where is the NullPointerException coming from?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

These first few lines


java.lang.NullPointerException
at addressbook.AddressBookGUI.addEntry(AddressBookGUI.java:71)
at addressbook.AddressBookGUI$AddButtonHandler.actionPerformed(AddressBookGUI.java:87)


Tell you that line 87 of your code, in the actionPerformed() method, called the method addEntry(), which ran into trouble at line 71. You don't tell us which is line 71; it could actually be either line in the addEntry() method.

A NullPointerException always happens when you say x.y, where x is a variable that contains null. If the keyInput, valueInput, or addressFile member variables are null, then this method will throw a NullPointerException to report it. If you check your line numbers carefully, you can narrow it down; look at the code to determine where, and if, the suspect variable should have been set to non-null, and decide why it didn't happen.
 
Divya Mehrotra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NUllPointerException is coming at

addressFile.add(entry);

line as addressFile is Null and you are trying to access method of this object.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Divya Mehrotra:
The NUllPointerException is coming at

addressFile.add(entry);


And you know this... how? It could also be keyInput or valueInput that is null, right? So there are three possibilities, as I described.
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I got the solution in another thread. It's because I had declared the variable in the constructor and it was local to the constructor; the class variable was null. I took away the type in the constructor and now it works fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!