Skip to content

NullPointerException for empty list #1743

@garretwilson

Description

@garretwilson

I have a CLI that takes a path as a parameter, e.g. foo do C:\temp. I just decided to allow multiple files/directories, so I changed the parameter to:

@Parameters(paramLabel = "<data>", description = "The file or base directory of the data. Multiple data sources are allowed.")
    @Nonnull List<Path> argDataPaths

I have a loop in the code that looks like this:

for(final Path dataPath : argDataPaths)

I tested it with no paths just to make sure I had things configured correctly.

foo do

But I got the following, along with a stack trace pointing the line of code above!

java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "argDataPaths" is null

OK, so I goofed and forgot to put an arity="1..*". But still, there should not have been a NullPointereException, because argDataPaths should never under any circumstances have been null. If the single parameter is a List<> and no CLI arguments are actually supplied, I expect to get an empty List<>! (I'm sure you're well aware of the huge distinction in semantics between "I gave you a list with nothing in it" and "I did not give you a list".)

After I put in my arity this won't affect me (for now), but it will affect me if I do allow arity 0..*. But just as great I cringe to think such an ugly bug could soil this awesome library. 😄

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions