title: Storing a values with ArrayList<Model> to MongoDB in Java
body: <p>In my Java Play framework application, I want to store the ArrayList values in mongoDB.</p>
  
  <p>I am having 3 values in the JSON which is loginid, phone, students; In that students is the ArrayList. I am storing the data in mongoDB like this:</p>
  
  <pre><code>{  "loginid" : "user@mail.com", "phone" : "0123456789", "students" : [{"firstName" : "Jesse", "lastName" : "Varnell", "age" : "15", "gender" : "M" }, { "firstName" : "John", "lastName" : "Doe", "age" : "13", "gender" : "F"}] }
  </code></pre>
  
  <p>I am using mongo query to store the values like:</p>
  
  <pre><code>BasicDBObject searchQuery = new BasicDBObject();
  BasicDBObject theUserObj = new BasicDBObject();
  
  ArrayList&lt;Student&gt; student = new ArrayList&lt;Student&gt;();
  if(studentArray != null &amp;&amp; studentArray.size()&gt;=0) {
      Student stud = new Student();
      for(int i = 0; i &lt; studentArray.size(); i++){
          stud = studentArray.get(i);
          student.add(stud);
      }   
  }
  theUserObj.put("loginid", profile.loginid);
  theUserObj.put("phone", profile.phone);
  searchQuery.append("loginid", username);
  theUserObj.put("students", student);
  
  theUserCollection.update(searchQuery, theUserObj); //Got error on this line.
  </code></pre>
  
  <p>I am getting the following error:</p>
  
  <pre><code>Execution exception (In /app/controllers/StudentInfo.java around line 176)
  IllegalArgumentException occured : can't serialize class models.Student
  
  play.exceptions.JavaExecutionException: can't serialize class models.Student
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
      at Invocation.HTTP Request(Play!)
  Caused by: java.lang.IllegalArgumentException: can't serialize class models.Student
      at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
      at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259)
      at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198)
      at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
      at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
      at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
      at com.mongodb.OutMessage.putObject(OutMessage.java:142)
      at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:346)
      at com.mongodb.DBCollection.update(DBCollection.java:165)
      at com.mongodb.DBCollection.update(DBCollection.java:197)
      at com.mongodb.DBCollection.update(DBCollection.java:209)
      at controllers.StudentInfo.doStoreProfile(StudentInfo.java:176)
      at controllers.StudentInfo.storeUserProfile(StudentInfo.java:212)
      at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
      at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
      at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
  </code></pre>
  
  <p>How to store a value with ArrayList in MongoDB using Java?</p>