Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with .setText() for labels in JavaFX

raulzinho17Nov 13 2016 — edited Nov 22 2016

I am trying to change the text for a label from JavaFX, and I keep getting an error. Could anyone point out what the mistake is that I'm making?

Main Class: (Error in line 215)

package application;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Hyperlink;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.stage.Stage;

public class MainController {

   

    @FXML

    private ImageView background;

   

    @FXML

    private ImageView makeItemImage;

   

    @FXML

    private Label lblFood;

    @FXML

    private Label lblStatus;

   

    @FXML

    private Label buttonLabel;

   

    @FXML

    private TextField txtUsername;

   

    @FXML

    private TextField txtPassword;

   

    @FXML

    private TextField txtConfirmPassword;

   

    @FXML

    private Button login;

   

    @FXML

    private Button signup;

   

    @FXML

    private Hyperlink hypSignup;

   

    @FXML

    private Label lblStatus1;

   

    @FXML

    private Label lblStatus2;

   

    @FXML

    private Button makeItem;

   

    @FXML

    private Button makeFood;

   

    private String getRank;

    private int rank;

    private String getMoney;

    private int money;

    private String getFood;

    private int food;

    private String getBread;

    private int bread;

    private String getFarmers;

    private int farmers;

    private String getBakers;

    private int bakers;

   

   

    public void Login(ActionEvent event) throws Exception {

        BufferedReader br = null;

        try {

            br = new BufferedReader(new FileReader("src/stats/userInformation.txt"));

            String line;

           

            while ((line = br.readLine()) != null) {

                if (txtUsername.getText().equals(line) && txtPassword.getText().equals(br.readLine())) {

                    br.close();

                    br = new BufferedReader(new FileReader("src/stats/"+txtUsername.getText()+".txt"));

                    getRank = br.readLine();

                    rank = Integer.parseInt(getRank);

                    lblStatus.setText("Login Successful!");

                    Stage primaryStage = new Stage();

                   

                    if (rank == 0) {

                        Parent root = FXMLLoader.load(getClass().getResource("/application/Tutorial.fxml"));

                        Scene scene = new Scene(root);

                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

                        primaryStage.setScene(scene);

                        primaryStage.setTitle("Company Simulator");

                        primaryStage.show();

                        Stage stage = (Stage) login.getScene().getWindow();

                        stage.close();

                       

                        tutorial();

                       

                    } else {

                        Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));

                        Scene scene = new Scene(root);

                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

                        primaryStage.setScene(scene);

                        primaryStage.setTitle("Company Simulator");

                        primaryStage.show();

                        Stage stage = (Stage) login.getScene().getWindow();

                        stage.close();

                    }

                } else {

                    lblStatus.setText("Invalid Credentials...");

                }

            }

           

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                br.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

       

    }

   

    public void SignupFromLogin(ActionEvent event) throws Exception {

        Stage primaryStage = new Stage();

        Parent root = FXMLLoader.load(getClass().getResource("/application/Signup.fxml"));

        Scene scene = new Scene(root);

        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

        primaryStage.setScene(scene);

        primaryStage.setTitle("Company Simulator");

        primaryStage.show();

        Stage stage = (Stage) hypSignup.getScene().getWindow();

        stage.close();

    }

   

    public void Signup(ActionEvent event) throws Exception {

        try {

           

        File file = new File("src/stats/userInformation.txt");

       

        if (!file.exists()) {

            file.createNewFile();

        }

        if (txtPassword.getText().equals(txtConfirmPassword.getText())) {

            File f = new File("src/stats/"+txtUsername.getText()+".txt");

            if (!f.exists()) {

                file.createNewFile();

            }

            PrintWriter pw = new PrintWriter(new FileWriter(file, true));

            pw.println(txtUsername.getText());

            pw.println(txtPassword.getText());

            pw.close();

            PrintWriter pw1 = new PrintWriter("src/stats/"+txtUsername.getText()+".txt");

            pw1.println("0");

            pw1.println("0");

            pw1.println("0");

            pw1.println("0");

            pw1.println("0");

            pw1.println("0");

            pw1.close();

   

            Stage primaryStage = new Stage();

            Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));

            Scene scene = new Scene(root);

            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            primaryStage.setScene(scene);

            primaryStage.setTitle("Company Simulator");

            primaryStage.show();

            Stage stage = (Stage) signup.getScene().getWindow();

            stage.close();

        } else {

            lblStatus.setText("Passwords Don't Match");

        }

       

        } catch (IOException e) {

            e.printStackTrace();

        }

       

    }

   

    public void makeFood(ActionEvent event) {

        food++;

    }

   

    public void makeBread(ActionEvent event) {

        bread++;

    }

   

    public void Startup(ActionEvent event) {

        System.out.println("Starting Up...");

    }

   

    public void tutorial() {

        int section = 0;

        food = 0;

        while (true) {

            if (section == 0) {

                lblFood.setText("Food"); //This is the setText I am having trouble with

                if (food == 10) {

                    section = 1;

                }

            } else if (section == 1) {

                Image img = new Image("/img/bread.png");

                makeItemImage.setImage(img);

                lblStatus1.setText("Press the 'Make Bread' Button!");

                lblStatus2.setText("");

                buttonLabel.setText("Make Bread");

            }

        }

    }

   

}

FXML File:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>

<?import javafx.scene.control.Label?>

<?import javafx.scene.image.Image?>

<?import javafx.scene.image.ImageView?>

<?import javafx.scene.layout.AnchorPane?>

<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">

   <children>

      <ImageView fx:id="background" fitHeight="600.0" fitWidth="800.0" pickOnBounds="true">

         <image>

            <Image url="img/CompanyBackground.png" />

         </image>

      </ImageView>

      <Label fx:id="lblStatus1" alignment="CENTER" layoutY="41.0" prefHeight="23.0" prefWidth="800.0" text="Welcome to Company Simulator! To start off, press the 'Make Food'">

         <font>

            <Font name="System Bold" size="24.0" />

         </font>

      </Label>

      <Label fx:id="lblStatus2" alignment="CENTER" layoutY="76.0" prefHeight="23.0" prefWidth="800.0" text="button!">

         <font>

            <Font name="System Bold" size="24.0" />

         </font>

      </Label>

      <Button fx:id="makeItem" layoutX="352.0" layoutY="220.0" mnemonicParsing="false" onAction="#makeFood" prefHeight="32.0" prefWidth="32.0">

         <font>

            <Font size="24.0" />

         </font>

         <graphic>

            <ImageView fx:id="makeItemImage" fitHeight="64.0" fitWidth="64.0" pickOnBounds="true">

               <image>

                  <Image url="img/food.png" />

               </image>

            </ImageView>

         </graphic>

      </Button>

      <Label fx:id="buttonLabel" layoutX="353.0" layoutY="305.0" text="Make Food">

         <font>

            <Font name="System Bold" size="18.0" />

         </font>

      </Label>

      <Label fx:id="lblFood" alignment="CENTER_RIGHT" layoutX="450.0" layoutY="120.0" prefHeight="27.0" prefWidth="350.0" text="Label" textFill="#433900">

         <font>

            <Font name="System Bold" size="18.0" />

         </font>

      </Label>

   </children>

</AnchorPane>

Error:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)

    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)

    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)

    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)

    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)

    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)

    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)

    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)

    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)

    at javafx.event.Event.fireEvent(Event.java:198)

    at javafx.scene.Node.fireEvent(Node.java:8413)

    at javafx.scene.control.Button.fire(Button.java:185)

    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)

    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)

    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)

    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)

    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)

    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)

    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)

    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)

    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)

    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)

    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)

    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)

    at javafx.event.Event.fireEvent(Event.java:198)

    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)

    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)

    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)

    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)

    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)

    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)

    at java.security.AccessController.doPrivileged(Native Method)

    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)

    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)

    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)

    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)

    at com.sun.glass.ui.View.notifyMouse(View.java:937)

    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)

    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)

    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.reflect.InvocationTargetException

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:498)

    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)

    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:498)

    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)

    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)

    ... 48 more

Caused by: java.lang.NullPointerException

    at application.MainController.tutorial(MainController.java:215)

    at application.MainController.Login(MainController.java:110)

    ... 58 more

Any help would be appreciated!

This post has been answered by bouye-JavaNet on Nov 22 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 20 2016
Added on Nov 13 2016
4 comments
7,122 views