I can't set text to a label

2019-08-06 10:26发布

Basically what I want to do is to display a text on a label when a button is clicked and it gives me a NullPointerException. I am using JavaFX. Here's the code itself(I deleted some of it because I don't think it's needed):

 package controller;

import java.sql.*;

import javafx.event.*;
import javafx.fxml.FXML;
import javafx.scene.control.*;

public class LoginController {
  @FXML private TextField user;
  @FXML private PasswordField password;
  @FXML private Button loginButton;
  @FXML private Button registerButton;
  @FXML private Label errorLabel1;

  public void initialize() {}


  public void login(final WindowManager Manager) {
    loginButton.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent event) {
             user.clear();
             password.clear();
//The error is in the next line 
             errorLabel1.setText("test");
        }
      });
  }

And here is the error I get when I click the button

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at controller.LoginController$1.handle(LoginController.java:28)
at controller.LoginController$1.handle(LoginController.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$175/420645852.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

FXML file:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="330.0" styleClass="background" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LoginController">
   <children>
      <ImageView fitHeight="128.0" fitWidth="128.0" layoutX="101.0" layoutY="36.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@images/safe-icon.png" />
         </image>
      </ImageView>
      <Label layoutX="24.0" layoutY="175.0" styleClass="label-bright" text="Welcome!">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label id="usernameLabel" layoutX="24.0" layoutY="215.0" text="Username:" />
      <TextField id="user" fx:id="user" layoutX="165.0" layoutY="213.0" prefHeight="25.0" prefWidth="151.0" />
      <Label layoutX="21.0" layoutY="278.0" text="Master password:" />
      <Button fx:id="loginButton" layoutX="21.0" layoutY="364.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="111.0" text="Log in." />
      <Button fx:id="registerButton" layoutX="144.0" layoutY="364.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="170.0" text="New Registration" />
      <PasswordField id="password" fx:id="password" layoutX="165.0" layoutY="276.0" prefHeight="25.0" prefWidth="151.0" />
      <Label id="errorLabel1" layoutX="27.0" layoutY="322.0" textFill="RED">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

1条回答
一夜七次
2楼-- · 2019-08-06 11:02

The Label has no fx:id attribute. You need

<Label id="errorLabel1" fx:id="errorLabel1" layoutX="27.0" layoutY="322.0" textFill="RED">

(you may not need teh id attribute, depending on whether or not you are using it for CSS).

查看更多
登录 后发表回答