kohsuke/args4j

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

khituras opened this issue · 2 comments

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.

I meet it!

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?