0

i built a little software. The software runs perfectly ok as it should. On initiation of the software, a option pane pops up asking for name, user can insert name then choose ok or just cancel and terminate the running software. my issue with this now is that if the user clicks the cancel option it terminates then throws a bunch of errors. These errors i do not understand, I've used the appropriate exception for almost every method I've created. Does anybody have any advice or idea of whats going on.

This is the RequestAngentName Class

public class RequestAgentName{

// instance variable daclaration
private static String uName;

/*
recursive method set username to instance variable
*/

public void setUserName(String name) throws NullPointerException
{       
    name = JOptionPane.showInputDialog(null, "Insert First Name and Last Initial", "Agent Name", 
             JOptionPane.PLAIN_MESSAGE);

    // if user input is not empty
(line 24)if(!name.isEmpty())
    {
        // verify input is acceptable, if yes assign input to global variable
        if(JOptionPane.showConfirmDialog(null, "Is This Your Name?\n\n           " + name, "WARNING" , JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION)
        {
            uName = name;
        } // end if 

        // if input is not acceptable prompt user to reenter name
        else
            // if username is wrong reenter until correct
            this.setUserName(name);


    } // end if

        // if input is empty prompt user to reenter 
        else
            this.setUserName(name);


} //end method set username

// method to return user input 
public String getUserName(){
    return uName;
} // end method get username 

} // end of Request name class

This is how i invoked it in main, which i feel could be where the error is to

RequestAgentName userName = new RequestAgentName();
    try { 
        (line 52) userName.setUserName(userName.getUserName());
        new FNAFrame();
        (new Thread(new FnaComponents())).start(); 
    } 
    catch (NullPointerException npe){
         npe.printStackTrace();
    }

The errors are as follows;

java.lang.NullPointerException
at fna.comments.generator.RequestAgentName.setUserName(RequestAgentName.java:24)
at fna.comments.generator.FNAFrame$1.run(FNAFrame.java:52)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

BUILD SUCCESSFUL (total time: 6 seconds)

Juice
  • 111
  • 13
  • Well yeah what happens when cancel is clicked? Show us the code. You should know that a nullpointer exception is. – Ya Wang May 24 '15 at 05:22
  • yea i do know what a nullpointer exception is but when it gets to AccessController.doPriviledged, ProtectionDomain and pumpevents, na mhen, i'm lost lol – Juice May 24 '15 at 05:25
  • the code is to long to post – Juice May 24 '15 at 05:29
  • Just post the `RequestAgentName` class. Or at least the `setUserName()` method. Else debug on your own. Every IDE supports helpful debugging tools. At the least you put sysouts & see which object is coming out `null`. – Vineet May 24 '15 at 05:33
  • ok kool... imma post the class – Juice May 24 '15 at 06:33

1 Answers1

0

When you do

name = JOptionPane(...);

and when the user presses cancel, JOptionPane returns null. So, name is null.

Hence

if(!name.isEmpty())

becomes

if(!null.isEmpty())

You can't call a method on null. Hence the NullPointerException is thrown. A simple fix would be:

if(name!=null && !name.isEmpty())

EDIT

My bad, I didn't see the impact on the whole code. Better to fix it like so:

    if (name == null){
        // User hit cancel, so simply return/terminate
        return;
    } else if (!name.isEmpty()) {
        // User pressed OK with a value
    } else
        // User pressed OK without value
Vineet
  • 897
  • 10
  • 27
  • thanks much but now it won't terminate when i press cancel, the option pane keeps reappearing every time i press cancel. – Juice May 24 '15 at 07:03
  • thanks mucho vineet.. The return statement actually just got it into the swing application, so i replaced return with System.exit(0) and it worked like a charm – Juice May 24 '15 at 07:36
  • i was using an IDE, so i went and ran it in cmd, pressed cancel still shows those errors – Juice May 24 '15 at 07:42
  • You mean the NPE? How exactly are you running your commands? – Vineet May 24 '15 at 08:28
  • i'm using java -jar ... – Juice May 24 '15 at 20:01
  • Since the jar is giving errors, it must be outdated. Use file -> export to make a new jar in Eclipse. It works fine for me. – Vineet May 25 '15 at 03:51
  • i navigate straight to the netbeans file and run it from dre – Juice May 25 '15 at 14:06
  • Make sure you're creating the [jar](http://stackoverflow.com/a/20786265/2173960) correctly. Since it works otherwise & not in jar, that must be where the issue lies. – Vineet May 25 '15 at 17:48
  • yea i went and i do a clean and build then i ran the app from command line, clicked cancel it had no errors.... thanks mucho... now i'm tryna add a mouse listener and its such a prob.. i'm tryna do it in a anonymous class – Juice May 25 '15 at 19:29