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 Efficiency! Building the Model

Gábor Tóth
Gábor Tóth
11,958 Points

Errors in my code

Hi, I've followed Craig step by step and also unterstand everything, I have the same code in my workspace, but I still get 2 compiler errors(1. ArrayList<Song> cannot be converted to List<String>, 2. no suitable method found for add(Song)). Could someone help me? Thank you! Here's my code: Song.java

package com.teamtreehouse.model;

public class Song {
  private String mArtist;
  private String mTitle;
  private String mVideoUrl;

  public Song(String artist, String title, String videoUrl) {
    mArtist = artist;
    mTitle = title;
    mVideoUrl = videoUrl;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getArtist() {
    return mArtist;
  }

  public String getVideoUrl() {
    return mVideoUrl;
  }

  @Override
  public String toString() {
    return String.format("Song: %s by %s", mTitle, mArtist);
  }
}

SongBook.java

package com.teamtreehouse.model;

import java.util.ArrayList;
import java.util.List;

public class SongBook {
  private List<String> mSongs;

  public SongBook() {
    mSongs = new ArrayList<Song>();
  }

  public void addSong(Song song) {
    mSongs.add(song);
  }

  public int getSongCount() {
    return mSongs.size();
  }
}

Karaoke.java

import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;

public class Karaoke {

  public static void main(String[] args) {
    Song song = new Song(
    "Michael Jackson",
    "Beat It",
    "https://www.youtube.com/watch?v=SaEC9i9QOvk");
    SongBook songBook = new SongBook();
    System.out.printf("Adding %s %n", song);
    songBook.addSong(song);
    System.out.printf("There are %d songs. %n", songBook.getSongCount());
  }
}
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Can you post the actual error messages as they contain additional debug information. Thx.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In your SongBook class, you declare mSongs to be a List<String> then in the constructor you assign to it an ArrayList<Song>. The conversion error is due to the different <String> vs <Song> types. Fix by change the member type:

public class SongBook {
  private List<Song> mSongs;  // <-- fix type as <Song>

  public SongBook() {
    mSongs = new ArrayList<Song>();
  }
Gábor Tóth
Gábor Tóth
11,958 Points

Thank you, Chris, it helped me a lot! :)