question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JCommander#parseValues throws NullPointerException (since 1.71)

See original GitHub issue

During the changes in commit 4435722a0b45be660b1b58834d4f5326fc78c9ad new defect has crept in which raises a NullPointerException.

Exception in thread "main" java.lang.NullPointerException
	at com.beust.jcommander.JCommander.parseValues(JCommander.java:718)
	at com.beust.jcommander.JCommander.parse(JCommander.java:340)
	at com.beust.jcommander.JCommander.parseWithoutValidation(JCommander.java:330)

The Boolean in line 717 Boolean value = (Boolean) pd.getParameterized().get(pd.getObject()); can be null if no default value is present for a Boolean parameter like:

@Parameter(names = {"--flag"}, description = "Just a simple flag")
private Boolean simpleFlag;

This behaviour is not obvious because strings can be null. And a nullable parameter is important if one just wants to override a configured value with those of the command line if they are present.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
glhezcommented, Aug 14, 2017

@cbeust The problem is still there using version from maven central. I don’t know how to publish a new version to central and perhaps it is a task that fall on you being main maintainer 😃 (beside if I use ./kobaltw clean compîle publishToMavenLocal on WIndows, it fails with java.lang.IllegalArgumentException: Illegal character in opaque part at index 11: jar:file:E:\git\github\jcommander\.\kobaltBuild\ => you might need to use Path.toURI() or File.toURI() …) Nonetheless, my fix fix your problem @bisoldi .

1reaction
bisoldicommented, Jul 17, 2017

Unfortunately, it does not work for me. Using v1.72, I still get a NullPointerException, however as soon as I switched the dependency to v1.69, it worked fine. Below is the test class I used.

package com.myapp.models;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import org.junit.Test;

public class CmdLineArgsTest {
    
    class CmdLineArgs {

        @Parameter(names = {"--help", "-h"}, description = "Display help for this application", help = true)
        private Boolean help;

        @Parameter(names = {"--topic", "-t"}, description = "Sets a single topic (can be multiple words) for the processing of the articles")
        private String topic;

        public Boolean getHelp() {
            return help;
        }

        public void setHelp(Boolean help) {
            this.help = help;
        }

        public String getTopic() {
            return topic;
        }

        public void setTopic(String topic) {
            this.topic = topic;
        }
    }

    @Test
    public void testCmdLineArgs() {

        CmdLineArgs cmdLine = new CmdLineArgs();

        JCommander jcmd = JCommander.newBuilder()
                .addObject(cmdLine)
                .build();

        jcmd.parse(new String[]{"-h"});

        if (cmdLine.getHelp()) {
            System.out.println("help");
            System.exit(0);
        }
    }

}

Here is the full stack trace:

java.lang.NullPointerException
	at com.beust.jcommander.JCommander.parseValues(JCommander.java:718)
	at com.beust.jcommander.JCommander.parse(JCommander.java:340)
	at com.beust.jcommander.JCommander.parse(JCommander.java:319)
	at com.myapp.models.CmdLineArgsTest.testCmdLineArgs(CmdLineArgsTest.java:43)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix and Avoid NullPointerException in Java - Rollbar
NullPointerException in Java occurs when a variable is accessed which is not pointing to any object and refers to nothing or null.
Read more >
java - What is a NullPointerException, and how do I fix it?
Obviously, it can't do this, so it throws a null pointer exception . In general, it's because something hasn't been initialized properly. Share....
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
When we run the above program, it throws the following NullPointerException error message. Exception in thread "main" java.lang.
Read more >
NullPointerException (Java Platform SE 7 ) - Oracle Help Center
Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of...
Read more >
How to resolve the java.lang.NullPointerException - Educative.io
In Java, the java.lang.NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found