java.util.Vector cannot be cast to java.util.Hashtable

535 views
Skip to first unread message

Mostapha ZINE EL ABIDINE

unread,
Feb 26, 2014, 3:27:21 PM2/26/14
to codenameone...@googlegroups.com
hi, since I did the last update of codenameone, I get this error java.lang.ClassCastException: java.util.Vector java.util.Hashtable cannot be cast
My code: 
  JSONParser parser = new JSONParser (); 
                         Hashtable h = parser.parse (new InputStreamReader (input)); 
                         Vector contents = (Vector) h.get ("result"); 
                        
                         model.removeAll (); 
                         for (int i = 0; i <contents.size (); i + +) {
                             Hashtable elmts = (Hashtable) contents.elementAt (i); 
                             / / System.out.println ((String) elmts.get ("signature")); 
                             model.addItem ((String) elmts.get ("signature")); 
                         }
Help me pleaze !

Shai Almog

unread,
Feb 27, 2014, 1:54:26 AM2/27/14
to codenameone...@googlegroups.com
Hi,
I'm guessing the structure of your JSON changed and the contents is now a vector.

Mostapha ZINE EL ABIDINE

unread,
Feb 27, 2014, 6:54:40 AM2/27/14
to codenameone...@googlegroups.com
this is my json file
file.json

babtu

unread,
Feb 27, 2014, 8:51:22 AM2/27/14
to codenameone...@googlegroups.com
Shai is right. In file you have vector of hashtables.Try this:

JSONParser parser = new JSONParser (); 
Vector v = (Vector)parser.parse (new InputStreamReader (input));
for(int i = 0; i < v.size(); ++i)
{
    Hashtable h = v.get(i);
   //do whatever you want
}

Mostapha ZINE EL ABIDINE

unread,
Feb 27, 2014, 10:07:39 AM2/27/14
to codenameone...@googlegroups.com
batu i tried your code and i got this error,
I'm sorry but i upload the json file , this is th good one here

java.util.Vector cannot be cast to java.util.Hashtable
at com.mycompany.myapp.MyApplication$1.readResponse(MyApplication.java:71)
at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:398)
at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:261)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

Le mercredi 26 février 2014 20:27:21 UTC, Mostapha ZINE EL ABIDINE a écrit :
file.json

Shai Almog

unread,
Feb 27, 2014, 2:50:20 PM2/27/14
to codenameone...@googlegroups.com
In line 71 of your app you are casting a vector to a Hashtable.
Place a breakpoint on that line.
Run the debugger and inspect the variable, expand it and look at the types of elements within to understand how they break down.

babtu

unread,
Feb 28, 2014, 3:51:39 AM2/28/14
to codenameone...@googlegroups.com
Its not a suprise for me since you are executing it on JSON file with different structure. Now you have Hashtable with one key - "result", which value is vector of vectors of hashtables. You need to become more familiar with JSON format and JSON parsing methods in CN1, because I have the impression that you have no idea what you are doing. Helpful links: 4 Ways to Consume JSON Web Services in Codename One, expression language for json and xml, JSON

Mostapha ZINE EL ABIDINE

unread,
Feb 28, 2014, 8:29:45 PM2/28/14
to codenameone...@googlegroups.com
thanks for your answer, now i changed my code, and i got juste the first element, how can i got all element ?
my new code: 

                    try {
                    JSONParser parser = new JSONParser();
                    Result result = Result.fromContent(parser.parse(new InputStreamReader(input)));
                    Hashtable res = (Hashtable)result.get("result");
                    Hashtable post = (Hashtable)res.get("posts");
                    System.out.println("" + post.get("signature"));
                     
                    } catch (IOException ex) {
                        System.out.println(ex.getMessage());
                    }

Le mercredi 26 février 2014 20:27:21 UTC, Mostapha ZINE EL ABIDINE a écrit :

Shai Almog

unread,
Mar 1, 2014, 3:09:29 AM3/1/14
to codenameone...@googlegroups.com
That's a very different type of parsing. See http://gadgets.coolman.ca/new-feature-introduction-expression-language-for-json-and-xml/ for explanation on that approach.
Reply all
Reply to author
Forward
0 new messages