You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2002/08/14 04:46:50 UTC

DO NOT REPLY [Bug 11680] New: - Use of CommandLine.getOptionValue(String opt, String defaultValue) for missing argument results in null pointer exception

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11680>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11680

Use of CommandLine.getOptionValue(String opt, String defaultValue) for missing argument results in null pointer exception

           Summary: Use of CommandLine.getOptionValue(String opt, String
                    defaultValue) for missing argument results in null
                    pointer exception
           Product: Commons
           Version: 1.0 Beta 1
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: CLI
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: sdowning@fame.com


The following code results in an unexpected null pointer exception

*** begin DemoCliBug.java ***
import org.apache.commons.cli.*;

public class DemoCliBug
{
    public static void main(String args[])
    {
	Options opts = new Options();
	opts.addOption("f", true, "foobar");
	opts.addOption("m", true, "missing");

	String[] bug_args = new String[2];

	bug_args[0] = "-f";
	bug_args[1] = "foo";

	try {
	    CommandLine cmd = new GnuParser().parse(opts, bug_args);
	    System.out.println("f option: " 
			       + cmd.getOptionValue("f", "default f"));
	    //The following line results in null pointer exception
	    System.out.println("m option: " 
			       + cmd.getOptionValue("m", "default m"));
	}
	catch (ParseException exception){
	}
    }
}
*** end DemoCliBug.java ***
results in this output
*** begin output ***
f option: foo
Exception in thread "main" java.lang.NullPointerException
        at org.apache.commons.cli.CommandLine.getOptionValue(CommandLine.java:146)
        at org.apache.commons.cli.CommandLine.getOptionValue(CommandLine.java:185)
        at DemoCliBug.main(DemoCliBug.java:21)
*** end output ***

per the javadoc for the CommandLine.getOptionValue(String) method, it should
return null if the option is unset or has no argument

I changed the implementation to this to fix the bug...
    public String getOptionValue( String opt ) {
 	Option option = (Option)options.get( opt );
        return option == null ? null : option.getValue();
    }

It looks like the same problem exists for
String[] getOptionValues( String opt )

Sorry, I'm a CVS newbie so I'm not yet able to post the patch myself.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>