Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String array parameters with usage cause NPE when not contained in CLI call #159

Open
khituras opened this issue Jul 2, 2018 · 2 comments

Comments

@khituras
Copy link

khituras commented Jul 2, 2018

Consider the following option:
@Option(name = "-i", aliases = {"--import"}, usage = "Usage") public String[] doImport;

If the respective CLI program is called without specifying the -i switch and the usage is printed, the following exception will occur:

Exception in thread "main" java.lang.NullPointerException
    at java.lang.reflect.Array.getLength(Native Method)
    at org.kohsuke.args4j.spi.ArrayFieldSetter.getValueList(ArrayFieldSetter.java:113)
    at org.kohsuke.args4j.spi.OptionHandler.printDefaultValue(OptionHandler.java:139)
    at org.kohsuke.args4j.CmdLineParser.createDefaultValuePart(CmdLineParser.java:358)
    at org.kohsuke.args4j.CmdLineParser.printOption(CmdLineParser.java:335)
    at org.kohsuke.args4j.CmdLineParser.printUsage(CmdLineParser.java:303)
    at org.kohsuke.args4j.CmdLineParser.printUsage(CmdLineParser.java:273)
    at org.kohsuke.args4j.CmdLineParser.printUsage(CmdLineParser.java:263)

Workaround:

If the String[] is changed to List<String>, the NPE won't appear.

@yuzhidi
Copy link

yuzhidi commented Dec 26, 2018

I meet it!

@Dani-Hub
Copy link

We also met the same issue here, a possible workaround is to initialize the array field with empty {}, but is really a pitty. The solution seems very clear that in ArrayFieldSetter line 113 we need to take account the possibility that the local variable array is null. I would happily provide a PULL request for this, but would anyone being able to review and accept it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants