Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Serialization

Sean Flanagan
Sean Flanagan
33,235 Points

Errors in code

Hi. I'm not sure what or where the errors are.

Treets.java
package com.teamtreehouse;

import java.io.*;

public class Treets {
  public static void save(Treet[] treets) {
    try (
      FileOutputStream fos = new FileOutputStream("treets.ser");
      ObjectOutput Stream oos = new ObjectOutputStream(fos);
    ) {
      oos.writeObject(treets);
    } catch(IOException ioe) {
      System.out.println("Problem saving Treets");
      ioe.printStackTrace();
    }
  }
}

All the errors are said to be in Treets.java. Here they are:

./com/teamtreehouse/Treets.java:9: error: = expected                                  
      ObjectOutput Stream oos = new ObjectOutputStream(fos);                          
                          ^                                                           
./com/teamtreehouse/Treets.java:9: error: '{' expected                                
      ObjectOutput Stream oos = new ObjectOutputStream(fos);                          
                             ^                                                        
./com/teamtreehouse/Treets.java:10: error: illegal start of expression                
    ) {                                                                               
    ^                                                                                 
./com/teamtreehouse/Treets.java:12: error: 'catch' without 'try'                      
    } catch(IOException ioe) {                                                        
      ^                                                                               
./com/teamtreehouse/Treets.java:17: error: reached end of file while parsing          
}                                                                                     
 ^                                                                                    
./com/teamtreehouse/Treets.java:11: error: cannot find symbol                         
      oos.writeObject(treets);                                                        
      ^                                                                               
  symbol:   variable oos                                                              
  location: class Treets 

As ever, thanks in advance for any help. :-)

1 Answer

You have an extra space on that line so it think you're looking to create an ObjectOutput variable named Stream. You need ObjectOutputStream oos instead:

   ObjectOutputStream oos = new ObjectOutputStream(fos);

EDIT: Also looks like you have some extra parenthesis in there around your try / catch.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Geoff. Thanks for your reply. I hadn't noticed that space on Line 9 that you mentioned. My program works now. Thank you. :-)

Sorry for the late reply. Couple more concerns with your code:

As mentioned above the try / catch should be only using curly braces "{ }" like so:

try {
  // do something
} catch (Exception e) {
  // handle error
}

Also I'm assuming your class should be Treet not Treets since you're accepting a Treet array in the save method. If that's the case the file should also be called Treet.java instead of Treets.java (always name the file after the main public class).

Think that should be it to get you compiling. Let me know if you run into any other issues.