jcommander icon indicating copy to clipboard operation
jcommander copied to clipboard

NPE Using manadatory DynamicParamter without default value

Open fcav1973 opened this issue 7 years ago • 0 comments

Hi, I have a DynamicParameter defined as

	@DynamicParameter(names = { "-t",
			"--attribute" }, required = true, description = "Attribute to be set in the form 'Name'"
					+ AttributeValidator.SEPARATOR + "'Value'.")
	private Map<String, String> attributes;

When i run the command line program, I get the exception:

2017-05-24 11:15:39,208 [main] ERROR - | null
 -       sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:606)
      com.beust.jcommander.WrappedParameter.callPut(WrappedParameter.java:88)
      com.beust.jcommander.WrappedParameter.addValue(WrappedParameter.java:80)
      com.beust.jcommander.ParameterDescription.addValue(ParameterDescription.java:261)
      com.beust.jcommander.ParameterDescription.addValue(ParameterDescription.java:210)
      com.beust.jcommander.JCommander.processFixedArity(JCommander.java:867)
      com.beust.jcommander.JCommander.processFixedArity(JCommander.java:849)
      com.beust.jcommander.JCommander.parseValues(JCommander.java:721)
      com.beust.jcommander.JCommander.parse(JCommander.java:281)
      com.beust.jcommander.JCommander.parse(JCommander.java:264)
      com.beust.jcommander.JCommander.parseValues(JCommander.java:783)
      com.beust.jcommander.JCommander.parse(JCommander.java:281)
      com.beust.jcommander.JCommander.parse(JCommander.java:264)

This is due to the fact the private Map<String, String> attributes filed have not been initialized. The point is that is I define it as private Map<String, String> attributes = new HashMap<>(), help shows it with a default value (which is probably not correct since it's a mandatory parameter)

    Options:
      -t, --attribute
         Attribute to be set in the form 'Name'='Value'.
         Syntax: -tkey=value
         Default: {}

fcav1973 avatar May 24 '17 09:05 fcav1973