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 Organizing Data Serialization

Something got jacked up and I cannot figure out how to fix it.

Going along with Craig Dennis I got about halfway through the video with everything working each time we compiled and ran Example. Once we got to this whole thing of "save", and had us compile and run after entering the Treets.save(treets); at the bottom of Example.java, I tried to compile and run like he did, but I came up with the following error:

Example.java:31: error: incompatible types: Treet[] cannot be converted to Treets[]
Treets.save(treets);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full >output
1 error

Below is the code that I have for Example.java and Treets.java, which were the only two files we edited so far in the video.

Example.java

import java.util.Arrays;
import java.util.Date;

import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;

public class Example {
  public static void main(String[] args) {
    Treet treet = new Treet(
      "craigdennis",
      "Want to be famous? Simply tweet about Java and use " +
      "the hashtag #treet. I'll use your tweet in a new " +
      "@treehouse course about data structures.",
      new Date(1421849732000L)
    ); 
    Treet secondTreet = new Treet(
      "journeytocode",
      "@treehouse makes learning Java soooo fun! #treet",
      new Date(1421878767000L)
    );
    System.out.printf("This is a new Treet:  %s %n", treet);
    System.out.println("The words are: ");
    for (String word : treet.getWords()) {
      System.out.println(word);
    }
    Treet[] treets = {secondTreet, treet};
    Arrays.sort(treets);
    for (Treet exampleTreet : treets) {
      System.out.println(exampleTreet);
    }
    Treets.save(treets);
  }
}

Treets.java

package com.teamtreehouse;

import java.io.*;

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

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

How about changing Treets[] to Treet[] in file Treets.java?

// wrong because we say Type[] types.
// we should use singular way, program cannot infer that
// for you
public static void save(Treets[] treets) {

}