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 trialdhruv Sharma
66 Pointsnullpointexception while changing the text of fxml label in controller
Hi Guys i am getting nullpointexception when trying to do tempReading.setText() and errorReading.setText().I have defined default values of label text in my fxml file.
package sample;
import com.google.common.base.Strings; import gnu.io.NRSerialPort; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.*; import javafx.stage.Stage; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.util.Collection;
public class SerialCommunicationController{
@FXML public ComboBox<Integer> baudRateCombo;
@FXML public ComboBox<String> serialPortCombo;
@FXML public Spinner propotional;
@FXML public Spinner integral;
@FXML public Spinner derivative;
@FXML public Label lastCmdStatus;
@FXML public Label tempReading;
@FXML public Label errorReading;
private final Logger logger = LoggerFactory.getLogger(getClass());
private ArduinoInterfaceControl arduinoInterfaceControl;
private Stage stage;
private String prop;
private String inte;
private String deri;
private boolean lastCommandSuccessful;
public static final String LOCAL_SIMULATOR = "Local Simulator";
private String[] params;
private ArduinoInterfaceControl arduinoInterface;
public SerialCommunicationController() {
}
public SerialCommunicationController(String cmd){
setValues(cmd);
}
private Stage dialogStage;
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
public void initialise() {
baudRateCombo.getItems().add(9600);
baudRateCombo.getItems().add(19200);
Collection<String> availablePorts = NRSerialPort.getAvailableSerialPorts();
serialPortCombo.getItems().addAll(availablePorts);
}
public void connectPressed(ActionEvent actionEvent) {
System.out.print("connecting");
if(!settingCombinationsAreAllowed()) {
showSerialSettingErrorDialog();
return;
}
arduinoInterface = new RS232Controller(serialPortCombo.getValue(), baudRateCombo.getValue());
}
private void showSerialSettingErrorDialog() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Invalid serial settings");
alert.setHeaderText("Invalid serial settings entered");
alert.setContentText("Please ensure you have entered a port and speed.");
alert.showAndWait();
}
private boolean settingCombinationsAreAllowed() {
return baudRateCombo.getValue() != null && serialPortCombo.getValue() != null;
}
public void startReading(ActionEvent actionEvent) {
}
public void stopReading(ActionEvent actionEvent) {
}
public void plotGraph(ActionEvent actionEvent) {
}
public void setPointTemp(ActionEvent actionEvent) {
}
public void setPIDVal(ActionEvent actionEvent) {
System.out.print(propotional.getValue());
prop = propotional.getValue().toString();
inte = integral.getValue().toString();
deri = derivative.getValue().toString();
String text = ("p"+prop+"p"+inte +"i"+ deri+"d");
if(Strings.isNullOrEmpty(text) || text.contains("\n")) {
logger.info("Pressed send message with no text.");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Message text error");
alert.setHeaderText("Invalid message entered");
alert.setContentText("Please ensure you have entered some text, and it does not contain line feeds.");
alert.showAndWait();
}
else {
arduinoInterface.sendPrintCommand(text);
enterWaitingState();
}
}
public ArduinoInterfaceControl getArdiunoInterface() {
return arduinoInterface;
}
private void enterWaitingState() {
lastCmdStatus.setText("Waiting..");
}
@FXML
public void setValues(String readings){
String[] params = readings.split("\\s+");
// something went wrong, just decode to blank.
if(params.length < 1) {
lastCommandSuccessful = false;
}
else {
tempReading.setText(params[0]);
errorReading.setText(params[1]);
// successful command received.
lastCommandSuccessful = Boolean.valueOf(params[1]);
}
}
}
1 Answer
Daniel Hartin
5,311 PointsHi Dhruv,
I use Java and FXML every day but i'll admit I haven't gone through the treehouse stuff so I may do it differently. You need to format your code to make it easier to read but from what I can see you define a link to the FXML at the top and this matches your code further down so that's okay, I can only assume that the fx:id valud in your FXML file doesn't match exactly the values tempReading and errorReading.
Any chance you can format the code above and potentially add the FXML code as well if this doesn't help?
Daniel