Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting IllegalArgumentException when trying to create key for entity

I'm working on Google App Engine and I'm stuck at a point. I'm trying to create a key for my entity. Here is the code:

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

I'm 100% sure that child.getText() doesn't return null. I tested it already. I have another piece of code that creating Key works perfectly. Both seem similar to each other.

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

I really did'nt understand why it gives error. First piece of code is belonged to a Java Class and I call this class in a Java Servlet Class. The second one is belonged to a Java Servlet Class. But I think the problem is different. Could you please help? Thanks.

Edited Part:

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

I put this into my code, still getting null error. child.getText() does not return null and name is not equal to "asd".

Forgot to add Error Message:

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)
like image 812
Ahmet Tanakol Avatar asked Nov 24 '12 13:11

Ahmet Tanakol


1 Answers

The error message said:

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

And your actual code:

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

So in the if statement the child.getQualifiedName().equals("name") expression is false, and the 'name' String variable is still an empty String (String name = ""). And that is what the error message said: cannot be an empty String at Key drugKey = KeyFactory.createKey("DrugTarget", name); as parameter.

EDIT: Try out this:

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

The problem isn't that the child.getText() return with null or not, the problem is the expression at your if statement will be false, so the name += child.getText(); is unreachable and the name still an empty String.

if(child.getQualifiedName().equals("name")){
      name += child.getText();
}
like image 199
Endox Avatar answered Oct 15 '22 14:10

Endox