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 Design a Better App The Controller

Sam Gerstner
Sam Gerstner
5,258 Points

Getting a NullPointerException

I keep getting a NullPointerException on my Controller class. My code will compile and open the new window then I get the error when I click to Say Sup! button. Here is my Controller class.

package sample;

import javafx.event.ActionEvent; import javafx.fxml.FXML;

import java.awt.*;

public class Controller {

@FXML private TextField fName;

public void handleSaySup(ActionEvent actionEvent) {
    System.out.printf("Sup %s!%n", fName.getText());


2 Answers

Tamara Lavalli
Tamara Lavalli
5,707 Points

Hey, Sam Gerstner! It looks like, you get the nullpointer exception because you call ".getText()" on fname before assigning a value to it.

Tonnie Fanadez
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Sam Gerstner

Also check the id for TextField on your fmxl file, its value should be fx:id="fName" - same letter case as the Controller's TextField value.

I also note you have imported awt - Abstract Window Toolkit - on your imports.

You should have these imports instead:

import javafx.event.ActionEvent; import java.fxml.FXML; import javafx.scene.control.TextField;