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.

Custom types on main parameters don't appear to work. (1.72)

See original GitHub issue

My reduced test case is as follows:

public class Main {

    public static class CustomType {
        private String something;

        public CustomType(String something) {
            this.something = something;
        }
    }

    public static class CustomTypeConverter implements IStringConverter<CustomType> {
        @Override
        public CustomType convert(String value) {
            return new CustomType(value);
        }
    }
    
    @Parameters
    public static class ExampleCommandArgs {
        @Parameter(converter = CustomTypeConverter.class, required = true)
        public CustomType mainArg;

    }

    public static void main(String[] args) {
        JCommander.newBuilder()
                .addObject(new ExampleCommandArgs())
                .build()
                .parse(args);
    }
}

As far as I can see I’ve configured the converter properly, but the error I get is:

Exception in thread "main" com.beust.jcommander.ParameterException: Could not invoke null
    Reason: Can not set Main$CustomType field Main$ExampleCommandArgs.mainArg to java.lang.String
	at com.beust.jcommander.Parameterized.set(Parameterized.java:263)
	at com.beust.jcommander.JCommander$MainParameter.addValue(JCommander.java:101)
	at com.beust.jcommander.JCommander.parseValues(JCommander.java:773)
	at com.beust.jcommander.JCommander.parse(JCommander.java:340)
	at com.beust.jcommander.JCommander.parse(JCommander.java:319)
	at Main.main(Main.java:38)

Note I get the same error on main arguments on commands as well.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RealSkepticcommented, Jun 25, 2017

Hi. I was about to post this same bug.

To be exact, custom types cannot be used in a main parameter, unless the main parameter is a list.

Here is an example that exhibits this problem:

package testing;

import java.nio.file.Path;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.converters.PathConverter;

public class JcommanderTest {

    @Parameter( converter = PathConverter.class )
    public Path filepath;
    
    public static void main(String[] args) {
        JcommanderTest cmd = new JcommanderTest();
        String[] argv = { "/tmp/file.txt" };
        
        JCommander.newBuilder().addObject(cmd).build().parse(argv);
        
        System.out.printf("The filepath is %s class %s%n", cmd.filepath, cmd.filepath.getClass().getName());
        
    }
}

The result is:

Exception in thread "main" com.beust.jcommander.ParameterException: Could not invoke null
    Reason: Can not set java.nio.file.Path field testing.JcommanderTest.filepath to java.lang.String
	at com.beust.jcommander.Parameterized.set(Parameterized.java:273)
	at com.beust.jcommander.JCommander$MainParameter.addValue(JCommander.java:101)
	at com.beust.jcommander.JCommander.parseValues(JCommander.java:771)
	at com.beust.jcommander.JCommander.parse(JCommander.java:342)
	at com.beust.jcommander.JCommander.parse(JCommander.java:321)
	at testing.JcommanderTest.main(JcommanderTest.java:18)

If I add names, or if I declare the parameter as List<Path> instead of Path, this works properly.

0reactions
juewecommented, Aug 27, 2017

Since this is a dupe of #380 and #380 is done with #390 one should close this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JCommander
For an alternative solution to parse a list of values, see Custom types - List value. 3.1.2. By factory. If the custom types...
Read more >
Control settings in the Format Cells dialog box - Excel
To create a custom number format, click Custom in the Category list on the Number tab in the Format Cells dialog box. Then,...
Read more >
Fix common formatting issues for custom parameters
Custom parameters can only contain letters, digits, and underscores. Custom parameters shouldn't begin with digits or include spaces.
Read more >
12 User Defined Functions - A Sufficient Introduction to R
Chapter 12 User Defined Functions. It is very important to be able to define a piece of programing logic that is repeated often....
Read more >
Lookups — stacker 1.7.2 documentation
If <lookup type> isn't provided, stacker will fall back to use the output lookup . ... ${output otherStack::IAMRole} Values: Env: Custom: ${custom ${output ......
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