title: NullpointerException while querying for the Datastore statistics in App Engine Java Servlet
body: <p>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 <a href="https://cloud.google.com/appengine/docs/java/datastore/stats?csw=1" rel="nofollow noreferrer">Datastore Docs</a> I discovered a way of querying the datastore and fetch different kind of statistics. 
  But while executing:</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>I get a <code>NullPointerException</code> when I try to store the two Long variables. 
  Why is the <code>Entity globalStat</code> object null after the query?</p>
  
  <p><strong><em>EDIT 1</em></strong>: Additional information</p>
  
  <p>I'm trying to get the Datastore statistics just after populating it with a <code>List&lt;Entity&gt;</code> of Entities:</p>
  
  <pre><code>public class MyServlet extends HttpServlet {
      ArrayList&lt;Tour&gt; m_tours = new ArrayList&lt;Tour&gt;();
      Key tourKey;
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  
      //.... some code
      private DatastoreService populateDatastore(){
          //... some other code ...
          List&lt;Entity&gt; 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();
          }` 
      }
  }
  </code></pre>
  
  <p>and just this instructions the Entity globalstat object still shows to be null in the debugging tool:</p>
  
  <p><img src="https://i.stack.imgur.com/ncgEH.png" alt="Entity globalstat"></p>
  
  <p>how come that the <code>new Query("__Stat_Total__")).asSingleEntity();</code> doesn't produce any result when I just filled the datastore with several entities?</p>