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 Build the State Change Events

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Problem instantiating a new attempt object.

Hi all.

I'm tryingto follow the video along but I've got an error trying pass in the AttemptKind object.

Here's my code.

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

import com.teamtreehouse.pomodoro.model.Attempt;
import com.teamtreehouse.pomodoro.model.AttemptKind;
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 void prepareAttempt(AttemptKind kind){

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


    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!");
    }
}

Here's the code that#s erroring

mCurrentAttempt = new Attempt(kind, "");
Error:(26, 27) java: constructor Attempt in class com.teamtreehouse.pomodoro.model.Attempt cannot be applied to given types;
  required: java.lang.String,int,com.teamtreehouse.pomodoro.model.AttemptKind
  found: com.teamtreehouse.pomodoro.model.AttemptKind,java.lang.String
  reason: actual and formal argument lists differ in length

Any ideas? I'm not sure what I've done that's different from the video.

2 Answers

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

So the compiler is just saying that there is no constructor with 2 args in Attempt class.

That is strange: I just downloaded project files, and it looks like in all Attempt.java files there is only one constructor:

    public Attempt(AttemptKind kind, String message) {
        mKind = kind;
        mMessage = message;
        mRemainingSeconds = kind.getTotalSeconds();
    }   

Compiler infers that in your Attempt class should be only constructor like public Attempt(String message, int seconds, AttemptKind kind) {

So please post your class Attempt, or I think better just download it from project files to any video.

I'll post Attempt class that I found in project files in s4v4 directory, so that you can compare as well:

package com.teamtreehouse.pomodoro.model;

public class Attempt {
    private String mMessage;
    private int mRemainingSeconds;
    private AttemptKind mKind;

    public Attempt(AttemptKind kind, String message) {
        mKind = kind;
        mMessage = message;
        mRemainingSeconds = kind.getTotalSeconds();
    }

    public AttemptKind getKind() {
        return mKind;
    }

    public String getMessage() {
        return mMessage;
    }

    public int getRemainingSeconds() {
        return mRemainingSeconds;
    }

    public void setMessage(String message) {
        mMessage = message;
    }

    public void tick() {
        mRemainingSeconds--;
    }

    @Override
    public String toString() {
        return "Attempt{" +
                "mKind=" + mKind +
                ", mMessage='" + mMessage + '\'' +
                ", mRemainingSeconds=" + mRemainingSeconds +
                '}';
    }

    public void save() {
        System.out.printf("Saving: %s %n", this);
    }
}
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thanks for pointing me in the right direction. It was a problem with my Attempt constructor! :-)

I couldn't see that last night!

I fixed this by changing from

public Attempt (String message, AttemptKind kind)

to

public Attempt (AttemptKind kind, String message)

I didn't know the constructor is so sensible to this, why is it that?