Re: [Simple-support] NPE using Map
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2008-04-28 21:29:05
|
Hi,
Yes, I can understand why this might seem strange.
However, Java generics are not reified. Which means
that template types are not kept after compilaion. So
the template type "Value" cannot be extracted through
reflection. Other generified languages such as C# can
do much better with this type of construct. However,
in the interest of backward compatibility Java decided
that erasure of template types was the way to go.
There is no way around this. Template types do not
work. As for the Object declaration, all objects are
considered composite types. That is it will not be
considered a primitive. Is you want your map to
contain a varying array of primitive types consider
storing them as strings.
You could use the
org.simpleframework.xml.transform.Transformer to help
you with this. The
org.simpleframework.xml.load.Factory provides some
documentation on why primitives can not be stored as
primitives.
Hope this helps,
Niall
--- Jas...@sc... wrote:
> Hi,
>
> Really like the looks of the library, seems very
> neat - but I'm having a
> problem setting up.
>
> When I run the following:
>
> @Root
> public class TestXML<Value>
> {
> @ElementMap
> Map<String, Value> map;
>
> TestXML()
> {
> map = new HashMap<String, Value>();
> }
>
> public static void main(String[] args)
> throws Exception
> {
> TestXML<Double> object = new
> TestXML<Double>();
> object.map.put("One", 1.0);
> object.map.put("Two", 2.0);
>
> Serializer serializer = new
> Persister();
> serializer.write(object, new
> File("test.xml"));
> }
> }
>
> I get:
>
> Exception in thread "main"
> java.lang.NullPointerException
> at
> org.simpleframework.xml.load.Factory.isPrimitive(
> Factory.java:213)
> at
>
org.simpleframework.xml.load.Entry.getValue(Entry.java:191)
> at
> org.simpleframework.xml.load.CompositeMap.<init>(
> CompositeMap.java:89)
> at
>
org.simpleframework.xml.load.ElementMapLabel.getConverter(
> ElementMapLabel.java:101)
> at
>
org.simpleframework.xml.load.CacheLabel.getConverter(
> CacheLabel.java:135)
> at
> org.simpleframework.xml.load.Composite.writeElement(
> Composite.java:829)
> at
>
org.simpleframework.xml.load.Composite.writeElements(
> Composite.java:728)
> at
>
org.simpleframework.xml.load.Composite.write(Composite.java:666
> )
> at
>
org.simpleframework.xml.load.Composite.write(Composite.java:644
> )
> at
>
org.simpleframework.xml.load.Traverser.write(Traverser.java:206
> )
> at
>
org.simpleframework.xml.load.Traverser.write(Traverser.java:183
> )
> at
>
org.simpleframework.xml.load.Traverser.write(Traverser.java:161
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:769
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:751
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:732
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:848
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:830
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:811
> )
> at
>
org.simpleframework.xml.load.Persister.write(Persister.java:790
> )
> at TestXML.main(TestXML.java:30)
>
> OK, so I guess it may be generics-related. It works
> fine if I change the
> generic Double to an explicit one. But this is a bit
> weird as the
> implementation of Map is itself generic?!
>
> To confuse me even more, changing this to an
> explicit Map<String, Object>
> gives the following unusable file:
>
> <testXML>
> <map class="java.util.HashMap">
> <entry>
> <string>Two</string>
> <object class="java.lang.Double"/>
> </entry>
> <entry>
> <string>One</string>
> <object class="java.lang.Double"/>
> </entry>
> </map>
> </testXML>
>
> i.e. It has lost the values of the Doubles....
>
>
>
> Jason Chown
> Sony Computer Entertainment Europe
> http://www.eu.playstation.com
>
>
**********************************************************************
> This email and any files transmitted with it are
> confidential and intended solely for the use of the
> individual or entity to whom they are addressed. If
> you have received this email in error please notify
> pos...@sc...
> This footnote also confirms that this email message
> has been checked for all known viruses.
> Sony Computer Entertainment Europe Limited
> Registered Office: 10 Great Marlborough Street,
> London W1F 7LP, United Kingdom
> Registered in England: 3277793
>
**********************************************************************
> >
-------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008
> JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still
> time to save $100.
> Use priority code J8TL2D2.
>
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone>
_______________________________________________
> Simple-support mailing list
> Sim...@li...
>
https://lists.sourceforge.net/lists/listinfo/simple-support
>
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
|