The Artima Developer Community
Sponsored Link

Java Answers Forum
Handling exception

7 replies on 1 page. Most recent reply: Jun 3, 2004 10:17 AM by Varun Gosain

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Varun Gosain

Posts: 3
Nickname: vanny
Registered: Jun, 2004

Handling exception Posted: Jun 1, 2004 4:25 AM
Reply to this message Reply
Advertisement
How can we handle the exceptions which occur during event dispatching?

The code throwing the exception is


String userCampaignCode = (String) JOptionPane.showInputDialog(
myPanel, TextManager.getText("MemberInvolvement.CampaignCode"),
TextManager.getText("Mem berInvolvement.ConfirmAddTitle"),
JOptionPane.PLAIN_MESSAGE

);
// Cancel button pressed

if (userCampaignCode == null)
{
enableButtons(true);
}

<b>
When Cancel button is pressed the follwing exception occurs
</b>

Exception occurred during event dispatching:
java.lang.NullPointerException
at com.britishairways.imm.oceanwave.client.MemberInvolvementFrameController.action Performed(MemberInvolvementFrameController.java:387)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1445)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.j ava:1499)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:373)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:245)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.ja va:211)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:225)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:225)
at java.awt.Component.processMouseEvent(Component.java:3710)
at java.awt.Component.processEvent(Component.java:3539)
at java.awt.Container.processEvent(Container.java:1159)
at java.awt.Component.dispatchEventImpl(Component.java:2588)
at java.awt.Container.dispatchEventImpl(Container.java:1208)
at java.awt.Component.dispatchEvent(Component.java:2492)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2446)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2211)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2120)
at java.awt.Container.dispatchEventImpl(Container.java:1195)
at java.awt.Component.dispatchEvent(Component.java:2492)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:334)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java: 126)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:93 )
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:88)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:80)


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Handling exception Posted: Jun 1, 2004 5:40 AM
Reply to this message Reply
NullPointerException means that the String userCampaignCode is null - which means that the name doesn't point to a String object. Since this is a known behavior, it shouldn't really be treated as an exception. You can check for it as follows.
if(userCampaignCode == null)
    //whatever should happen if they press cancel


I hope this helps.

Varun Gosain

Posts: 3
Nickname: vanny
Registered: Jun, 2004

Re: Handling exception Posted: Jun 1, 2004 9:12 AM
Reply to this message Reply
Hi Twc

Thanks for the reply but still I am not clear.

The String userCampaignCode gets set from this statement.

String userCampaignCode = (String) JOptionPane.showInputDialog
(
myPanel,
TextManager.getText("MemberInvolvement.CampaignCode"),
TextManager.getText("Me mberInvolvement.ConfirmAddTitle"),
JOptionPane.PLAIN_MESSAGE
);

If the cancel button is pressed in Dialog box constructed above , userCampaignCode is assigned a null value. This is Java functionality.

Now when I am tring to use this (tring to capture the cancel button of dialog box) I am getting this exception.

if(userCampaignCode == null) is causing the exception.

Any help ? Need to provide the solution as earlier as possible.

Mason Browne

Posts: 1
Nickname: embro
Registered: Jun, 2004

Re: Handling exception Posted: Jun 1, 2004 11:04 PM
Reply to this message Reply
if:
f(userCampaignCode == null) - (although I've never heard of a comparison of this type throwing an exception, unless it was just never defined...) is causing the exception, then why not just catch the exception? Can't you just go:

try
{
if(userCampaignCode == null) ...blahblah
}
catch(NullPointerException e)
{
//noop or do whatever else
}

--Although I still can't understand why it's happening the way it is... Perhaps you could try setting the the String to null before you do the assignment...

I'm a new Java programmer myself, and tend to get by using stupid redundant things like that...

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Handling exception Posted: Jun 1, 2004 11:17 PM
Reply to this message Reply
com.britishairways.imm.oceanwave.client.MemberInvolvementFrameController.action Performed(MemberInvolvementFrameController.java:387)

Why don't you post what is coded on line # 387 of MemberInvolvementFrameController.java?

Benjamin Armintor

Posts: 2
Nickname: benjamin
Registered: Jun, 2004

Re: Handling exception Posted: Jun 2, 2004 11:22 AM
Reply to this message Reply
That stack trace should indicate that on line 387 of MemberInvolvementFrameController.java, you invoke a method on an object that is null at runtime. Without seeing the line numbers on your posted code, it's difficult to know whether userCampaignCode has anything to do with this exception at all.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Handling exception Posted: Jun 3, 2004 1:55 AM
Reply to this message Reply
Ensure that while userCampaignCode is assiged value as following:

String userCampaignCode = (String)JOptionPane.showInputDialog(

Check if the class attribute name is same as userCampaignCode.

In that case that is not getting assiged to proper value it is still null. Above assignemt operation is affecting only locally declared String userCampaignCode.

While when Cancel is pressed you try to compare class attribute that is still null.

regards,
Shashank

Varun Gosain

Posts: 3
Nickname: vanny
Registered: Jun, 2004

Re: Handling exception Posted: Jun 3, 2004 10:17 AM
Reply to this message Reply
Hey Guys

Thanks for the help you all provided. But I am really sorry to all of you as I omitted just 1 line from the code and that was the troubling factor.

Actually just before this code

if (userCampaignCode == null)
{
enableButtons(true);
}
there was 1 code line written as

userCampaignCode = userCampaignCode.toUpperCase();

This line was causing the null pointer exceptionas it is quiet possible that this string object is null.

Flat View: This topic has 7 replies on 1 page
Topic: how can i set the window title when using response.setContentType Previous Topic   Next Topic Topic: hash question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use