Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Salaam

I am working in JAVA these days, I'm trying to add an object in an ArrayList in actionPerformed method of a button.
(It works all right in another program but is throwing unbelievable exception in this one, and it compiles without error.)

Code:

Java
ActionListener add=new ActionListener()
{
    public void actionPerformed(ActionEvent actionEvent)
    {

String manu = manutext.getText();
String owner = ownertext.getText();
String reg = regtext.getText();
int nic1 = Integer.parseInt(nic1text.getText());
int nic2 = Integer.parseInt(nic2text.getText());
int nic3 = Integer.parseInt(nic3text.getText());
Car c = new Car(manu,owner,reg,nic1,nic2,nic3);
cars.add(c);
}

};

addbtn.addActionListener(add);


Exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at driver1$1.actionPerformed(driver1.java:106)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20
18)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6505)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6270)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4861)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2719)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
        at java.awt.EventQueue.access$000(EventQueue.java:102)
        at java.awt.EventQueue$3.run(EventQueue.java:662)
        at java.awt.EventQueue$3.run(EventQueue.java:660)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:676)
        at java.awt.EventQueue$4.run(EventQueue.java:674)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

What am I supposed to do to resolve the situatio??
Your help will be appreciated, thanks.
Posted
Updated 19-Jul-12 5:50am
v3
Comments
[no name] 19-Jul-12 11:39am    
"What am I supposed to do to resolve the situatio"... find out which of your items is null and make sure that it's not null and your java.lang.NullPointerException will probably go away.

java.lang.NullPointerException
This simply means that you are trying to use a property of an object which is actually a NULL

DEBUG around the lines from where this exception is thrown and then check if any object is NULL whose property you are trying to use.


Just for your knowledgebase and reference[^].

This also is a good one: Suggestions for debugging java.lang.NullPointerException[^]
 
Share this answer
 
manutext, ownertext, regtext or nic1text, nic2text or nic3text is null at that moment.

You can NEVER depend on all values to be there!
You should first gather all the Strings, check if one is not ok and only proceed when all is fine.

Turn on the line numbers in your IDE (go to preferences and search for "editor line numbers") and you will find easily what is done on line 106.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900