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 Exploring the Java Collection Framework Using ArrayLists

Ronit Mankad
Ronit Mankad
12,166 Points

Treets program help with Twitter API

I tried to load actual tweets from twitter in this program. I am successfully getting data from twitter and it is saving in a file "tweets.ser" using twitter4j api. But on using the load() method, it seems that the ois.readObject() returns an ResponseListImpl object instead of a Status object. Can someone help me to convert this to a Status obj so I can display it?.

My code: This is the RealTreets.java file using the save and load methods.

I want to save and load my twitter feed by first saving it into tweet.ser file and then using a load() method to retrieve it. But im getting an error: "Exception in thread "main" java.lang.ClassCastException: twitter4j.ResponseListImpl cannot be cast to [Ltwitter4j.Status; at com.teamtreehouse.RealTreets.load(RealTreets.java:25) at Example.main(Example.java:12)"

Here is my code:

import java.io.*;
import java.util.List;
import twitter4j.Status;

public class RealTreets{
    public static void save(List<Status> status){
        try(
            FileOutputStream fos = new FileOutputStream("tweets.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(status);
        }catch(IOException ioe) {
            System.out.println("Problem saving Tweets");
            ioe.printStackTrace();
        }
    }
    public static Status[] load(){
        Status[] status = new Status[0];
        try(
            FileInputStream fis = new FileInputStream("tweets.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
        ){
            status = (Status[]) ois.readObject();
        } catch(IOException ioe){
            System.out.println("Error reading file!");
            ioe.printStackTrace();
        } catch(ClassNotFoundException cnfe){
            System.out.println("Error loading treets.");
            cnfe.printStackTrace();
        }
        return status;
    }
}