Hello, I need to read in objects from a binary file and place those objects into an ArrayList. Not sure on how to go about adding the objects - this is what I have so far:

ArrayList<MailingLabel> arrayOfMailingLabels = new ArrayList<MailingLabel>();

      try
      { 
                 
         ObjectInputStream in = new ObjectInputStream(new FileInputStream("labels.dat"));
         
            while (in.readObject() != null)          	
            {                               
               arrayOfMailingLabels.add(in.readObject());               
            }    
         
         in.close();
            	    
      } // end try
         
      catch(IOException ioe)
      {
      	 ioe.printStackTrace();
       }

Previous to this, I read in mailing labels from a txt file, placed the content into an ArrayList as an object, and read those objects into this binary file - fyi. Any help is greatly appreciated, thanks.

My apologies, I neglected to mention in the original post that i'm getting the error "cannot find symbol method add(java.lang.Object)" when I compile the above code.

which should tell you all you need to know.
You're supplying the add() method with an argument of an incorrect type.
Fix that.

If the objects are written as MailingLabel objects, read them in and cast them before adding to the arraylist (which you specified takes MailingLabel classes, so add(Object) is now add(MailingLabel) more or less)

arrayOfMailingLabels.add((MailingLabel)in.readObject());

works?

that should indeed work, if the stream contains objects of that type and none other.
More correct would be to read in an object, check if it's the correct type, insert it into the container if it is and somehow handle the error if it's not (log it, notify users, gracefully terminate with an error message) without crashing the program with an exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.