7

我正在研究 Google App Engine,但我被困在了一个点上。我正在尝试为我的实体创建一个密钥。这是代码:

Element child = (Element) itr.next();
                String name = "";
                if(child.getQualifiedName().equals("name")){
                    name += child.getText();
                }
                Key drugKey = KeyFactory.createKey("DrugTarget", name);

我 100% 确定 child.getText() 不会返回 null。我已经测试过了。我有另一段代码可以完美地创建 Key。两者看起来彼此相似。

String drug = req.getParameter("drugname");
Key drugKey = KeyFactory.createKey("DrugTarget", drug);

我真的不明白为什么它会出错。第一段代码属于 Java 类,我在 Java Servlet 类中调用这个类。第二个属于 Java Servlet 类。但我认为问题是不同的。能否请你帮忙?谢谢。

编辑部分:

if(child.getQualifiedName().equals("name")){
                    if(child.getText() != null){
                    name = child.getText();
                    }
                    else{
                        name = "asd";
                    }
                }

我把它放到我的代码中,仍然出现空错误。child.getText() 不返回 null 并且名称不等于“asd”。

忘记添加错误消息:

Uncaught exception from servlet
java.lang.IllegalArgumentException: name cannot be null or empty
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:73)
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:60)
at guestbook.saxParser2.getDrug(saxParser2.java:42)
at guestbook.SignGuestbookServlet.doPost(SignGuestbookServlet.java:77)
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.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:102)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
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 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.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
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.headerComplete(HttpConnection.java:923)
at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:447)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:454)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:461)
at com.google.tracing.TraceContext.runInContext(TraceContext.java:703)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:338)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:330)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:458)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
at java.lang.Thread.run(Thread.java:679)
4

2 回答 2

8

错误信息说:

Uncaught exception from servlet
java.lang.IllegalArgumentException: name cannot be null or empty

而你的实际代码:

String name = "";
if(child.getQualifiedName().equals("name")){
  name += child.getText();
}
Key drugKey = KeyFactory.createKey("DrugTarget", name);

因此,在 if 语句中,child.getQualifiedName().equals("name")表达式为假,“name”字符串变量仍然是一个空字符串 ( String name = "")。这就是错误消息所说的:不能是Key drugKey = KeyFactory.createKey("DrugTarget", name);作为参数的空字符串。

编辑:试试这个:

Element child = (Element) itr.next();
String name = "defaultName";
if(child.getQualifiedName().equals("name")){
  name = child.getText();
}
Key drugKey = KeyFactory.createKey("DrugTarget", name);

问题不在于是否child.getText()返回 null,问题在于您的 if 语句中的表达式 will be false,因此name += child.getText();无法访问并且name仍然是空字符串。

if(child.getQualifiedName().equals("name")){
      name += child.getText();
}
于 2012-11-24T13:45:32.703 回答
0

“煮”准备吃。

Element child = (Element) itr.next();
                String name;
                if(child.getQualifiedName().equals("name")){
                    name = child.getText();
                 if(name!=null){
                    Key drugKey = KeyFactory.createKey("DrugTarget", name);
                 }
                }
于 2012-11-24T14:01:33.430 回答