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 Build a Pomodoro App Properties and Bindings

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

NullPointerException

Hello you awesome Treehouse student type people :)

I've tried to follow this video as best i can but I have a Null pointer exception that's preventing the timer property from showing anything up at all.

java.lang.NullPointerException
    at com.sun.glass.ui.Application.invokeLater(Application.java:481)
    at com.sun.javafx.tk.quantum.QuantumToolkit.postPulse(QuantumToolkit.java:460)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$405(QuantumToolkit.java:322)

I've tried to follow the video as best I can. Here's my code.

Main.java
package com.teamtreehouse.pomodoro.controllers;

import com.teamtreehouse.pomodoro.model.Attempt;
import com.teamtreehouse.pomodoro.model.AttemptKind;
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.layout.VBox;
import java.lang.String;

/**
 * Created by Jonnie on 18/09/2016.
 */
public class Home {


    @FXML
    private VBox container;

    @FXML
    private Label title;
    private Attempt mCurrentAttempt;
    private StringProperty mTimerText;

    private void prepareAttempt(AttemptKind kind){

        clearAttemptStyles();
        mCurrentAttempt = new Attempt(kind, "");
        addAttemptStyle(kind);
        title.setText(kind.getDisplayName());
        setTimerText(mCurrentAttempt.getRemainingSeconds() );
    }

    public Home() {
        mTimerText = new SimpleStringProperty();
        setTimerText("Hammer");
    }


    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 actionEvent) {
        System.out.println("Hi Mom!");
    }


    public String getTimerText() {

        return mTimerText.get();
    }

    public void setTimerText(String timerText) {
        timerText = timerText;
    }

    public void setTimerText(int remainingSeconds) {
        int minutes = remainingSeconds / 60;
        int seconds = remainingSeconds % 60;
        setTimerText(String.format("02d:%02d", minutes, seconds));
    }
}
home.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.SVGPath?>
<VBox stylesheets="/css/main.css"
      xmlns="http://javafx.com/javafx/8"
      xmlns:fx="http://javafx.com/fxml"
      fx:id="container"
      fx:controller="com.teamtreehouse.pomodoro.controllers.Home">
    <children>

        <Label fx:id="title" text="Pomodoro"/>

        <Label fx:id="time" text="${controller.timerText}"/>

        <HBox styleClass="buttons">
            <children>
                <StackPane>
                    <children>
                        <StackPane styleClass="nested-action,play">
                            <children>
                                <HBox styleClass="svg-container">
                                    <SVGPath styleClass="svg"
                                             content="M1.6 17C1 17 0 16.2 0 15.6V1.3C0 .1 2.2-.4 3.1.4l8.2 6.4c.5.4.8 1 .8 1.7 0 .6-.3 1.2-.8 1.7l-8.2 6.4c-.5.2-1 .4-1.5.4z"/>
                                </HBox>
                                <Button text="Resume"/>
                            </children>
                        </StackPane>

                        <StackPane styleClass="nested-action,pause">
                            <children>
                                <HBox styleClass="svg-container">
                                    <SVGPath styleClass="svg"
                                             content="M10.2 17c-1 0-1.8-.8-1.8-1.8V1.8c0-1 .8-1.8 1.8-1.8S12 .8 12 1.8v13.4c0 1-.8 1.8-1.8 1.8z" />
                                    <SVGPath styleClass="svg"
                                             content="M10.2 17c-1 0-1.8-.8-1.8-1.8V1.8c0-1 .8-1.8 1.8-1.8S12 .8 12 1.8v13.4c0 1-.8 1.8-1.8 1.8z" />
                                </HBox>
                                <Button text="Pause"/>
                            </children>
                        </StackPane>
                    </children>
                </StackPane>

                <StackPane styleClass="nested-action,restart">
                    <children>
                        <HBox styleClass="svg-container">
                            <SVGPath styleClass="svg"
                                     content="M21 2.9C19.1 1 16.6 0 13.9 0c-1.1 0-2 .9-2 1.9s.9 1.9 2 1.9c1.7 0 3.2.6 4.4 1.8 2.4 2.4 2.4 6.3 0 8.7-1.2 1.2-2.7 1.8-4.4 1.8-1.7 0-3.2-.6-4.4-1.8-1.8-1.8-2.3-4.4-1.4-6.7L9.3 10c.2.4.5.7.9.8.4.1.8.1 1.2-.1.8-.4 1.1-1.3.8-2.1L10 4.3c0-.5-.2-1-.6-1.4l-.1-.1-.1-.2c-.2-.4-.5-.7-.9-.8-.4-.1-.8-.1-1.2.1L.9 4.8C.1 5.2-.2 6.1.2 6.9c.2.4.5.7.9.8.4.1.8.1 1.2-.1l2-.9c-1.2 3.5-.4 7.6 2.4 10.4C8.6 19 11.2 20 13.9 20s5.3-1 7.2-2.9C25 13.2 25 6.8 21 2.9z"/>
                        </HBox>
                        <Button text="Restart"/>
                    </children>
                </StackPane>
            </children>
        </HBox>

        <TextArea fx:id="message" promptText="What are you doing?"/>

        <ImageView>
            <image>
                <Image url="/images/pomodoro.png"/>
            </image>
        </ImageView>

    </children>
</VBox>

1 Answer

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

Hi, I cannot reproduce NullPointerException, however I was able to make word "Hammer" appear, on your clock.

The problem is in this code:

    public void setTimerText(String timerText) {
        timerText = timerText;
    }

First of all you probably want to set mTimerText member and not timerText, which is argument.

Second, because mTimerText is of class StringProperty you have to set its value using setValue method, like here:

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

If you feel confused. Please once again compare your code with project files that can be downloaded from any video of this course, and checkout s4v4 directory with latest version of Project.

That is what I did, and Craig in this version of projects sets mTimerText exactly like I've shown above.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Thanks again!

I did download the version of the code for the video but it was late and I had no idea if the code was completed code for for me to compare of starting for me to follow along.

It's always so much easier with a second pair of eyes :)