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

NPE when adding programmatically created subcommands to CommandLine #381

Closed
Mikusch opened this issue May 22, 2018 · 4 comments
Closed

NPE when adding programmatically created subcommands to CommandLine #381

Mikusch opened this issue May 22, 2018 · 4 comments
Milestone

Comments

@Mikusch
Copy link

Mikusch commented May 22, 2018

Hello,

I had a confusing NPE when trying to add a programmatically created subcommand using CommandLine#addSubCommand(String, Object).

picocli.CommandLine$InitializationException: Unable to initialize @ParentCommand field: java.lang.NullPointerException
	at picocli.CommandLine$Model$CommandReflection.initParentCommand(CommandLine.java:4186)
	at picocli.CommandLine.addSubcommand(CommandLine.java:234)
Caused by: java.lang.NullPointerException
	at picocli.CommandLine$Model$CommandReflection.initParentCommand(CommandLine.java:4175)

I took a look and it's because CommandLine#addSubCommand(String, Object) calls the userObject() method on the parent and subcommand, which returms null for both. Then the CommandReflection#initParentCommand(Object, Object) method tries calling getClass() on one of the null values, creating the NPE.

I can get around this issue using CommandLine.getSubcommands() and modifying the map it returns or simply adding the commands on the CommandSpec object instead.

Can you confirm this as a bug or is this intended behavior I just don't understand?

Kind regards

@remkop
Copy link
Owner

remkop commented May 22, 2018

Hi, thanks for the bug report!
I have tried but I am unable to reproduce the problem. Can you share a program that demonstrates the issue?

@remkop
Copy link
Owner

remkop commented May 22, 2018

Never mind, I got it now:

@Test
public void test381_NPE_whenAddingSubcommand() {
    CommandSpec toplevel = CommandSpec.create();
    toplevel.addOption(OptionSpec.builder("-o").description("o option").build());
    CommandLine commandLine = new CommandLine(toplevel);

    CommandSpec sub = CommandSpec.create();
    sub.addOption(OptionSpec.builder("-x").description("x option").build());
    commandLine.addSubcommand("sub", sub); // NPE here

    commandLine.usage(System.out);
}

@remkop
Copy link
Owner

remkop commented May 23, 2018

I will try to do a bugfix release for this later today.
If you have additional tests please share.

@remkop remkop closed this as completed in 644cf2d May 23, 2018
@remkop remkop added this to the 3.0.2 milestone May 23, 2018
@remkop
Copy link
Owner

remkop commented May 23, 2018

I released picocli v3.0.2 with the fix. Should be available from Bintray and Maven soon.

Thanks again for the bug report!

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

No branches or pull requests

2 participants