Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a cast error in my Query using JDO on Google App Engine?

According to the Queries and Indexes doc you can go a query effectively identically (so far as I can tell) to this:

PersistenceManager pm = PMF.get().getPersistenceManager();
try {
    Query q = pm.newQuery(App.class);
    q.setOrdering("name desc");

    try {
        results = (ArrayList<App>) q.execute();
    } finally {
        q.closeAll();
    }
} finally {
    pm.close();
}

However, I'm getting the following error on the results = (ArrayList<App>) q.execute(); line:

java.lang.ClassCastException: org.datanucleus.store.appengine.query.StreamingQueryResult cannot be cast to java.util.ArrayList
    at com.killerrobots.AppSite.MainServlet.getApplications(MainServlet.java:66)
    at com.killerrobots.AppSite.MainServlet.doGet(MainServlet.java:26)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:54)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:306)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
    at org.mortbay.jetty.Server.handle(Server.java:313)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:830)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:396)
    at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)

Any ideas?

like image 968
Jeremy Logan Avatar asked Jun 22 '09 05:06

Jeremy Logan


1 Answers

You can cast the result of execute to a List<whatever>, but not specifically to an ArrayList<whatever> which is what your code is trying to do! Why not just use List, the abstraction you're allowed to cast to, rather than ArrayList, the concrete implementation that you're not allowed to cast to?

like image 78
Alex Martelli Avatar answered Nov 14 '22 22:11

Alex Martelli