Search code examples
javasplitguava

Getting IllegalArgumentException for null value in using Splitter KeyValueGenerator


I am using the guava Splitter as:

Splitter.on(",").withKeyValueSeperator(Splitter.on("=").limit(2)).split(<My required String>);

My input String is like:

A=1,B=2,C=null....

If none of value(1,2) for the key(A,B,C) is null then the above Splitter works fine otherwise it fails with "IllegalArgumentException: Chunk[>null] is not a valid entry"


Solution

  • Works OK for input you gave:

    @Test
    public void shouldSplitValues() {
        //given
        Splitter.MapSplitter mapSplitter = Splitter.on(",").withKeyValueSeparator("=");
        String input = "A=1,B=2,C=null";
        //when
        Map<String, String> result = mapSplitter.split(input);
        //then
        assertThat(result)
                .containsExactly(entry("A", "1"),
                                 entry("B", "2"),
                                 entry("C", "null"));
        // {A=1, B=2, C=null}
    }
    

    The message suggests it's the error in your input string, i.e. if

    String input = "A=1,B=2,>null";
    

    then I indeed get java.lang.IllegalArgumentException: Chunk [>null] is not a valid entry with the code above.