title: Deserialize multiple Java Objects
body: <p>hello dear colleagues,</p>
  
  <p>I have a Garden class in which I serialize and deserialize multiple Plant class objects. The serializing is working but the deserializing is not working if a want to assign it to calling variable in the mein static method.</p>
  
  <pre><code>public void searilizePlant(ArrayList&lt;Plant&gt; _plants) {
      try {
          FileOutputStream fileOut = new FileOutputStream(fileName);
          ObjectOutputStream out = new ObjectOutputStream(fileOut);
          for (int i = 0; i &lt; _plants.size(); i++) {
              out.writeObject(_plants.get(i));
          }
          out.close();
          fileOut.close();
      } catch (IOException ex) {
      }
  }
  </code></pre>
  
  <p>deserializing code:</p>
  
  <pre><code>public ArrayList&lt;Plant&gt; desearilizePlant() {
      ArrayList&lt;Plant&gt; plants = new ArrayList&lt;Plant&gt;();
      Plant _plant = null;
      try {
          ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
          Object object = in.readObject();
  
         // _plant = (Plant) object;
  
  
          // TODO: ITERATE OVER THE WHOLE STREAM
          while (object != null) {
              plants.add((Plant) object);
              object = in.readObject();
          }
  
          in.close();
      } catch (IOException i) {
          return null;
      } catch (ClassNotFoundException c) {
          System.out.println("Employee class not found");
          return null;
      }
      return plants;
  }
  </code></pre>
  
  <p>My invoking code:</p>
  
  <pre><code>ArrayList&lt;Plant&gt; plants = new ArrayList&lt;Plant&gt;();
  plants.add(plant1);
  Garden garden = new Garden();
  garden.searilizePlant(plants);
  
  // THIS IS THE PROBLEM HERE
  ArrayList&lt;Plant&gt; dp = new ArrayList&lt;Plant&gt;();
  dp = garden.desearilizePlant();
  </code></pre>
  
  <p><strong>edit</strong><br>
  I got a null Pointer exception<br>
  The solution of @NilsH is working fine, thanks!</p>