Cris Benois Cris Benois - 7 months ago 25
Java Question

NullpointerException while querying for the Datastore statistics in App Engine Java Servlet

Within my app's Servlet, I want to control wether its Datastore is empty or not (the first time I run the servlet it will be empty, but that's not the case right now as I already have populated it and made it persistent) before continuing with the rest of the code. In this Datastore Docs I discovered a way of querying the datastore and fetch different kind of statistics.
But while executing:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
Long totalBytes = (Long) globalStat.getProperty("bytes");
Long totalEntities = (Long) globalStat.getProperty("count");


I get a
NullPointerException
when I try to store the two Long variables.
Why is the
Entity globalStat
object null after the query?

EDIT 1: Additional information

I'm trying to get the Datastore statistics just after populating it with a
List<Entity>
of Entities:

public class MyServlet extends HttpServlet {
ArrayList<Tour> m_tours = new ArrayList<Tour>();
Key tourKey;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

//.... some code
private DatastoreService populateDatastore(){
//... some other code ...
List<Entity> List = Arrays.asList(tour,tour1,tour2,tour3,
tour4,tour5,tour6,tour7,tour8);
datastore.put(List);
Entity globalStat = datastore.prepare(
new Query("__Stat_Total__")).asSingleEntity();

try{
Long totalEntities = (Long) globalStat.getProperty("count");
Long totalBytes = (Long) globalStat.getProperty("bytes");
}catch (NullPointerException e){
e.printStackTrace();
}`
}
}


and just this instructions the Entity globalstat object still shows to be null in the debugging tool:

Entity globalstat

how come that the
new Query("__Stat_Total__")).asSingleEntity();
doesn't produce any result when I just filled the datastore with several entities?

Answer

ANSWER: as reported here: Using App Engine Datastore Low Level API with Java, "Stat_Total" and "Stat_Kind" don't work in a local development server. They only work when deployed in App Engine Server.

Comments