samujjwal / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Get NullPointer Exeption when parsing a Map<String,Map<String,String> object. #408

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
According to documentation the following code should work:
<code>
Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();
HashMap<String, String> vals = new HashMap<String,String>();
vals.put("memo", "tired");
vals.put("mem", "lome");
map.put( "hello",  vals);

String str = gson.toJson(map);
System.out.println(str);
Type type2 = new TypeToken<Map<String,Map<String,String>>>(){}.getType();
String str2 = gson.toJson(map, type2);
System.out.println(str2);

Map<String,Map<String,String>> map2 =
            gson.fromJson(str2, type2);
</code>
The Type object is being created to tell json the nested types that it should 
look for.
However, I get this error:
<error>
{"hello":{"memo":"tired","mem":"lome"}}
Exception in thread "main" java.lang.NullPointerException
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
    at com.google.gson.Gson.fromJson(Gson.java:755)
    at com.google.gson.Gson.fromJson(Gson.java:721)
    at com.google.gson.Gson.fromJson(Gson.java:670)
    at RawCollectionsExample.main(RawCollectionsExample.java:80)
</error>
Notice that the json string is created correctly.  

What is the expected output? What do you see instead?
The output should be the map<string,map<string,string> back to its form.  

What version of the product are you using? On what operating system?
Gson 2.1 release 
http://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.1-relea
se.zip&can=2&q=
This is happening on Kubuntu 11.10(x86_64).  Java version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)
kernel = 3.0.0-15-generic

Please provide any additional information below.

Original issue reported on code.google.com by jeremyvillalobos on 10 Feb 2012 at 6:52

GoogleCodeExporter commented 9 years ago
I'm guessing you have both gson.jar AND gson-stream.jar on your class path; 
that won't work.

The problem is that in Gson 2.1 we have a custom version of JsonReader that 
adds some secret hooks to make MapTypeAdatperFactory faster; these hooks won't 
work with gson-stream.jar's version of JsonReader.

Original comment by limpbizkit on 11 Feb 2012 at 6:25

GoogleCodeExporter commented 9 years ago
Yes, that was it.

Original comment by jeremyvillalobos on 17 Apr 2012 at 11:18

GoogleCodeExporter commented 9 years ago
Thanks a lot. Helped me too

Original comment by abiram...@gmail.com on 19 Dec 2012 at 9:16

GoogleCodeExporter commented 9 years ago
Thanks a lot. Helped me too

Original comment by abiram...@gmail.com on 19 Dec 2012 at 9:16