我有一个Java方法(getTrucks()
),它抛出ClassCastException: string can't be cast to double
。
该方法属于FoodTruckServiceImpl
,实现FoodTruckService
。
我通过解析器检索对象,因此它们以正确的类型存储。
以下是方法:
public List<Truck> getTrucks() {
PersistenceManager pm = getPersistenceManager();
List<Truck> trucklist = new ArrayList<Truck>();
try {
Extent e = pm.getExtent(Truck.class, true);
Iterator iter = e.iterator();
int count = 0;
while(iter.hasNext()) {
count++;
System.out.println(count);
Truck t = (Truck) iter.next();
trucklist.add(t);
}
if(count>0){
System.out.println("get to here(count>0)?!!");
return trucklist;
}
} finally {
pm.close();
}
System.out.println("get to here?!!");
return this.trucklist.getTruckList();
}
Truck Class:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Truck {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String key;
@Persistent
private String vendorType;
@Persistent
private String status;
@Persistent
private String businessName;
@Persistent
private String location;
@Persistent
private String description;
@Persistent
private double lat;
@Persistent
private double lon;
@Persistent
private Date createDate;
//constructor for food truck
public Truck() {
this.createDate = new Date();
}
public Truck(String symbol, String type, String status, String businessName, String location, String description, double lat, double lon) {
this();
this.key = symbol;
this.vendorType = type;
this.status = status;
this.businessName = businessName;
this.location = location;
this.description = description;
this.lat = lat;
this.lon = lon;
}
public Long getId() {
return this.id;
}
public String getTruck() {
return this.key;
}
public void setKey(String key) {
this.key = key;
}
public String getVendorType() {
return this.vendorType;
}
public void setVendorType(String vendorType) {
this.vendorType = vendorType;
}
public String getStatus() {
return this.status;
}
public void setstatus(String status) {
this.status = status;
}
public String getBusinessName() {
return this.businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public double getLat() {
return this.lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return this.lon;
}
public void setLon(double lon) {
this.lon = lon;
}
public Date getCreateDate() {
return this.createDate;
}
}
它会抛出以下错误:
SEVERE: javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.util.List com.cpsc310.foodtrucker.client.FoodTruckService.getTrucks()' threw an unexpected exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:265)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:305)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:487)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at com.google.appengine.datanucleus.FetchFieldManager.fetchDoubleField(FetchFieldManager.java:140)
at org.datanucleus.state.AbstractStateManager.replacingDoubleField(AbstractStateManager.java:2256)
at com.cpsc310.foodtrucker.client.Truck.jdoReplaceField(Truck.java)
at com.cpsc310.foodtrucker.client.Truck.jdoReplaceFields(Truck.java)
at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:1935)
at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:1962)
at com.google.appengine.datanucleus.EntityUtils$1.fetchFields(EntityUtils.java:974)
at org.datanucleus.state.JDOStateManager.loadFieldValues(JDOStateManager.java:764)
at org.datanucleus.state.JDOStateManager.initialiseForHollow(JDOStateManager.java:205)
at org.datanucleus.state.StateManagerFactory.newForHollowPopulated(StateManagerFactory.java:89)
at org.datanucleus.state.ObjectProviderFactory.newForHollowPopulated(ObjectProviderFactory.java:75)
at org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:2882)
at com.google.appengine.datanucleus.EntityUtils.entityToPojo(EntityUtils.java:1014)
at com.google.appengine.datanucleus.query.DatastoreQuery$2.apply(DatastoreQuery.java:229)
at com.google.appengine.datanucleus.query.DatastoreQuery$2.apply(DatastoreQuery.java:226)
at com.google.appengine.datanucleus.query.LazyResult.resolveNext(LazyResult.java:96)
at com.google.appengine.datanucleus.query.LazyResult.resolveAll(LazyResult.java:121)
at com.google.appengine.datanucleus.query.StreamingQueryResult.closingConnection(StreamingQueryResult.java:90)
at org.datanucleus.store.query.AbstractQueryResult.disconnect(AbstractQueryResult.java:108)
at com.google.appengine.datanucleus.query.StreamingQueryResult.disconnect(StreamingQueryResult.java:78)
at com.google.appengine.datanucleus.query.JDOQLQuery$1.managedConnectionPreClose(JDOQLQuery.java:199)
at com.google.appengine.datanucleus.DatastoreConnectionFactoryImpl$DatastoreManagedConnection.close(DatastoreConnectionFactoryImpl.java:147)
at org.datanucleus.store.connection.ConnectionManagerImpl.closeAllConnections(ConnectionManagerImpl.java:181)
at org.datanucleus.store.AbstractStoreManager$1.preClose(AbstractStoreManager.java:260)
at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1112)
at org.datanucleus.api.jdo.JDOPersistenceManager.internalClose(JDOPersistenceManager.java:359)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.releasePersistenceManager(JDOPersistenceManagerFactory.java:1106)
at org.datanucleus.api.jdo.JDOPersistenceManager.close(JDOPersistenceManager.java:343)
at com.cpsc310.foodtrucker.server.FoodTruckServiceImpl.getTrucks(FoodTruckServiceImpl.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 40 more
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
当方法返回List<Truck>
时,只需返回trucklist
NOT this.trucklist.getTruckList()
答案 1 :(得分:0)
就我而言,问题是ToastAndroid出现的,所以我只是删除了行,错误消失了
Platform.OS === "android"
? ToastAndroid.show(
"Congratulations\n",
"Your profile has been updated",
ToastAndroid.SHORT
)
: Alert.alert("Congratulations", "Your profile has been updated");
只要小心地实施Toast,一切都会很好