Re: [Simple-support] Strange Nullpointer for ElementMap with generics?
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2009-03-29 13:16:44
|
Hi,
Generics are not reified in Java, this means that the type is erased after compilation. So when you reflectively attempt to acquire the type there is a null pointer exception.
The is no workaround for this, its a limitation of the language unfortunately.
Niall
--- On Sun, 3/29/09, Fabian Kürten <fab...@we...> wrote:
> From: Fabian Kürten <fab...@we...>
> Subject: [Simple-support] Strange Nullpointer for ElementMap with generics?
> To: sim...@li...
> Date: Sunday, March 29, 2009, 5:28 AM
> Hi Group
>
> I just tried this:
>
> @Root
> public class MapTest<K, V> {
>
> @ElementMap
> public HashMap<K, V> data;
>
> public MapTest() {
> this.data = new
> HashMap<K, V>();
> }
>
> public static void main(String[] args)
> throws Exception {
> MapTest<String,
> Integer> mt = new MapTest<String, Integer>();
> mt.data.put("Eins",
> 1);
> mt.data.put("Vier",
> 4);
> mt.data.put("Drei",
> 3);
> Serializer serializer
> = new Persister();
> serializer.write(mt,
> System.out);
> }
> }
>
> Which failed like:
> Exception in thread "main" java.lang.NullPointerException
> at
> org.simpleframework.xml.core.Signature.getEntry(Signature.java:102)
> at
> org.simpleframework.xml.core.ElementMapLabel.getEntry(ElementMapLabel.java:196)
> at
> org.simpleframework.xml.core.Signature.getName(Signature.java:196)
> at
> org.simpleframework.xml.core.ElementMapLabel.getName(ElementMapLabel.java:214)
> at
> org.simpleframework.xml.core.CacheLabel.<init>(CacheLabel.java:119)
> at
> org.simpleframework.xml.core.LabelFactory.getInstance(LabelFactory.java:67)
> at
> org.simpleframework.xml.core.Scanner.process(Scanner.java:606)
> at
> org.simpleframework.xml.core.Scanner.scan(Scanner.java:539)
> at
> org.simpleframework.xml.core.Scanner.field(Scanner.java:498)
> at
> org.simpleframework.xml.core.Scanner.scan(Scanner.java:348)
> at
> org.simpleframework.xml.core.Scanner.<init>(Scanner.java:98)
> at
> org.simpleframework.xml.core.ScannerFactory.getInstance(ScannerFactory.java:67)
> at
> org.simpleframework.xml.core.Support.getScanner(Support.java:161)
> at
> org.simpleframework.xml.core.Support.getName(Support.java:223)
> at
> org.simpleframework.xml.core.Source.getName(Source.java:196)
> at
> org.simpleframework.xml.core.Traverser.getName(Traverser.java:262)
> at
> org.simpleframework.xml.core.Traverser.write(Traverser.java:185)
> at
> org.simpleframework.xml.core.Traverser.write(Traverser.java:168)
> at
> org.simpleframework.xml.core.Persister.write(Persister.java:921)
> at
> org.simpleframework.xml.core.Persister.write(Persister.java:903)
> at
> org.simpleframework.xml.core.Persister.write(Persister.java:884)
> at
> org.simpleframework.xml.core.Persister.write(Persister.java:1000)
> at
> org.simpleframework.xml.core.Persister.write(Persister.java:982)
> at
> org.simpleframework.xml.core.Persister.write(Persister.java:963)
> at MapTest.main(MapTest.java:25)
>
> I am quite sure the reason are the generics... anyone knows
> more about this?
>
> Fabian
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
|