NullPointerException instead of ConversionException

21 views
Skip to first unread message

Ed Erwin

unread,
Aug 2, 2024, 7:06:14 PM8/2/24
to XStream User

With XStream 1.4.20, the method `DefaultConverterLookup.lookupConverterForType(Class type)` can throw a NullPointerException instead of the intended and more helpful ConversionException.  For  example, the code below throws an unintelligble exception when trying to lookup a converter for EnumMap.

It should probably throw a ConversionException.  Instead, it throws a NullPointerException while trying to create the ConversionException.  A simple null-check in the DefaultLookupConverter class would fix this.

```
    XStream xstream =new XStream(new DomDriver());

    Converter forString = xstream.getConverterLookup().lookupConverterForType(String.class);
    System.out.println("converter for String = " + forString);

    Converter forEnumMap = xstream.getConverterLookup().lookupConverterForType(EnumMap.class);
    /*
  A NullPointerException is thrown, so never get here.
  Should have been a ConversionException with a useful message.
*/

    System.out.println("converter for EnumMap = " + forEnumMap);

```

Jörg Schaible

unread,
Aug 15, 2024, 7:32:11 PM8/15/24
to XStream User
On Saturday, 3. August 2024, 01:06:14 CEST Ed Erwin wrote:
> With XStream 1.4.20, the method
> `DefaultConverterLookup.lookupConverterForType(Class type)` can throw a
> NullPointerException instead of the intended and more helpful
> ConversionException. For example, the code below throws an unintelligble
> exception when trying to lookup a converter for EnumMap.

This can only happen, if XStream cannot instantiate a converter because of a
module restriction. You shoud open java.base for it.

> It should probably throw a ConversionException. Instead, it throws a
> NullPointerException while trying to create the ConversionException.

Please just open an issue at GitHub.

> A
> simple null-check in the DefaultLookupConverter class would fix this.
>
> ```
> XStream xstream =new XStream(new DomDriver());
>
> Converter forString =
> xstream.getConverterLookup().lookupConverterForType(String.class);
> System.out.println("converter for String = " + forString);
>
> Converter forEnumMap =
> xstream.getConverterLookup().lookupConverterForType(EnumMap.class);
> /*
> A NullPointerException is thrown, so never get here.
> Should have been a ConversionException with a useful message.
> */
>
> System.out.println("converter for EnumMap = " + forEnumMap);
>
> ```

Regards,
Jörg




Reply all
Reply to author
Forward
0 new messages