title: mongoDB + Java-8: java.lang.NullPointerException: no value for: users
body: <p>Am new to Java, sorry if this obvious mistake. Also I tried to search all the questions related to my issue none of them solve it.</p>
  
  <p>Am trying to fetch all documents in mongoDB collection and put them in array.
  Am fetching the documents correctly I can print them all in the console:</p>
  
  <pre><code> public List&lt;Item&gt; findAll() {
          List&lt;Item&gt; items = new ArrayList&lt;&gt;();
          try {
              collection.find().forEach(document -&gt; {
                  System.out.println(document);
              });
              return items;
          } catch (Exception e) {
              System.out.println("Error fetcing all items  " + e);
              return items; // should be logging 
          }
      }  
  </code></pre>
  
  <p>however when I add this two lines:</p>
  
  <pre><code>System.out.println(document);
  Item item = new Item((BasicDBObject) document); //line causing the error
  items.add(item);//line causing the error
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>java.lang.NullPointerException: no value for: users
  </code></pre>
  
  <p>The Item class code :</p>
  
  <pre><code>package io.app.item.model;
  
  import com.mongodb.BasicDBObject;
  import org.bson.types.ObjectId;
  
  import java.util.Date;
  
  public class Item {
  
      private final String id;
      private final String name;
      private final int users;
      private Date updated_at = new Date();
      private Date created_at = new Date();
  
      public Item(BasicDBObject dbObject) {
          this.id = ((ObjectId) dbObject.get("_id")).toString();
          this.name = dbObject.getString("name");
          this.users = dbObject.getInt("users");
          this.updated_at = dbObject.getDate("updated_at");
          this.created_at = dbObject.getDate("created_at");
      }
  
      public String getId() {
          return id;
      }
  
      public int getUsers() {
          return users;
      }
  
      public String getName() {
          return name;
      }
  }
  </code></pre>
  
  <p>I have commented out the <code>users</code> filed from the <code>Item</code> class then everything work not really work I still don't know why adding field of type <code>integer</code> causing the problem also when remove it the return was in this shape:</p>
  
  <pre><code>[io.app.item.model.Item@15457c60, io.app.item.model.Item@1238bd80]
  </code></pre>
  
  <p>Note am using mongoldb 3.4.1 driver.
  Sample of document:</p>
  
  <pre><code>{
      "_id": {
          "$oid": "586140b25fa86a1e760d92fe"
      },
      "name": "item1",
      "created_at": {
          "$date": "2016-12-26T16:09:22.949Z"
      }
  }
  </code></pre>