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

KARAOKE NullpointerException

Can please anybody help me here?

I keep getting a NullpointerException at line 30 of KaraokeMachine.java...

I am also adding my github repository in case it helps!

https://github.com/RicSala/exercises/tree/master/karaoke

package karaoke;

import karaoke.model.SongBook;
import karaoke.model.Song;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class KaraokeMachine {
  private SongBook songBook;
  private BufferedReader reader;
  private Map<String, String> menu;


  public KaraokeMachine(SongBook songBook) {
    //CONSTRUC KARAOKE

    reader = new BufferedReader(new InputStreamReader(System.in));
    menu = new HashMap<String, String>();
    menu.put("add", "Add a song to the song book");
    menu.put("quit", "Give up! Exit the Karaoke");
  }



  private String askForAction() throws IOException {
    //DRAW THE MENU AND RETURN THE STRING WRITTEN BY THE USER
    System.out.printf("There %d songs available. Your options are: ", songBook.getSongCount());

    //REVISAR!!!!!

    for (Map.Entry<String, String> option: menu.entrySet()) {
      System.out.println(option.getKey() + " - " + option.getValue());
    }

    System.out.print("What do you want to do?  ");
    String choice = reader.readLine(); //MAY THROW AN EXCEPTION!
    return choice.trim().toLowerCase();
    }


  public void run() {
    String choice = ""; //WHY THAT ""??

    do {
      try {
        choice = askForAction();
        switch(choice) {

          case"add":
            Song song = addSong();
            System.out.printf(song + " added to the song book!%n%n%n");
          break;

          case"quit":
            System.out.println("Thanks for playing!!");
          break;

          default:
            System.out.println("Unknown choice!");
        }
      }
      catch(IOException ioe) {
            System.out. println("There was a problem");
            ioe.printStackTrace();
      }
    }

    while (!choice.equals("quit")); //OJO! LAS CADENAS DE STRING COPARARLAS CON EQUALS, SON OBJETOS REFERENCIADOS!

  }

  private Song addSong() throws IOException {
    // String artist = System.out.println("Enter the artists name: "); ----> OJO!!!!!
    // String title = System.out.println("Enter the song title: ");
    // String url = System.out.println("Enter the song's URL: ");

      System.out.println("Enter the artists name: ");
      String artist = reader.readLine();
      System.out.println("Enter the song title: ");
      String title = reader.readLine();
      System.out.println("Enter the song's URL: ");
      String url = reader.readLine();

    return new Song(artist, title, url);
  } 

3 Answers

Hi Ricardo,

I think line 30 is your printf line? The null pointer will be songBook - it is never initialized so it holds nothing, or null.

I'm not sure where you would assign something to it; probably when you've called the constructor for KaraokeMachine as that takes an instance of SongBook. So, perhaps, in the constructor of KaraokeMachine you should assign the parameter, songBook to the member variable? Something like:

  public KaraokeMachine(SongBook songBook) {
    //CONSTRUC KARAOKE
    this.songBook = songBook

I hope that helps,

Steve.

arg...what happened to my code...?

I'll sort your code blocks now! :smile:

Thank Steve, sometime when you have spent hours in front of the code it is hard to see those things!

It was what you just said, I didn't initialized this.songBook (arg!).

THANKS!

No problem! :+1: