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 Build a JavaFX Application It's About Time Sounds About Right

Andrew Stevens
Andrew Stevens
11,083 Points

How do I point to the correct resource?

I am unable to get the sound to play as Craig demonstrates in the video, for me this keeps returning a NullPointerException meaning the call to toExternalForm() cannot be made.

I've included my code below, help would be gratefully received :)

package com.teamtreehouse.pomodoro.controllers;

import com.teamtreehouse.pomodoro.model.Attempt;
import com.teamtreehouse.pomodoro.model.AttemptKind;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.util.Duration;

public class Home {

  private final AudioClip mApplause;
  @FXML
  private VBox container;

  @FXML
  private Label title;

  @FXML
  private TextArea message;

  private Attempt mCurrentAttempt;
  private StringProperty mTimerText;
  private Timeline mTimeline;

  public Home() {
    mTimerText = new SimpleStringProperty();
    setTimerText(0);
    mApplause = new AudioClip(getClass().getResource("sounds/applause.mp3").toExternalForm());
  }

  public String getTimerText() {
    return mTimerText.get();
  }

  public StringProperty timerTextProperty() {
    return mTimerText;
  }

  public void setTimerText(String timerText) {
    mTimerText.set(timerText);
  }

  public void setTimerText(int remainingSeconds) {
    int minutes = remainingSeconds / 60;
    int seconds = remainingSeconds % 60;
    setTimerText(String.format("%02d:%02d", minutes, seconds));
  }

  private void prepareAttempt(AttemptKind kind) {
    reset();
    mCurrentAttempt = new Attempt("", kind);
    addAttemptStyle(kind);
    title.setText(kind.getDisplayName());
    setTimerText(mCurrentAttempt.getRemainingSeconds());
    mTimeline = new Timeline();
    mTimeline.setCycleCount(kind.getTotalSeconds());
    mTimeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e -> {
      mCurrentAttempt.tick();
      setTimerText(mCurrentAttempt.getRemainingSeconds());
    }));
    mTimeline.setOnFinished(e -> {
      saveCurrentAttempt();
      mApplause.play();
      prepareAttempt(mCurrentAttempt.getKind() == AttemptKind.FOCUS ?
                      AttemptKind.BREAK : AttemptKind.FOCUS);
    });
  }

  private void saveCurrentAttempt() {
    mCurrentAttempt.setMessage(message.getText());
    mCurrentAttempt.save();
  }

  private void reset() {
    clearAttemptStyles();
    if (mTimeline != null && mTimeline.getStatus() == Animation.Status.RUNNING){
      mTimeline.stop();
    }
  }

  public void playTimer() {
    mTimeline.play();
  }

  public void pauseTimer() {
    mTimeline.pause();
  }

  private void addAttemptStyle(AttemptKind kind) {
    container.getStyleClass().add(kind.toString().toLowerCase());
  }

  private void clearAttemptStyles() {
    for (AttemptKind kind : AttemptKind.values()) {
      container.getStyleClass().remove(kind.toString().toLowerCase());
    }
  }

  public void DEBUG(ActionEvent event) {
    System.out.println("Hi Mum");
  }

  public void handleRestart(ActionEvent actionEvent) {
    prepareAttempt(AttemptKind.FOCUS);
    playTimer();
  }
}