2016-11-15 12 views
0

私はテキストフィールドにテキストを追加しようとしていますが、ボタンをクリックするとnullpointerexceptionが表示されますが、どうしてですか?JavaFx - TextField.setText()nullpointerExceptionを投げる

MainWindowController.java

@FXML 
public TextField konsumatoriPunetField = new TextField(); 

@FXML 
private void initialize() 
{ 
    FXMLLoader loader5 = new FXMLLoader(); 
    loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml")); 
    BorderPane border5 = new BorderPane(); 
    border5 = loader5.load(); 
    Scene scene5 = new Scene(border5); 
    zgjedhkonsumatorinstage.setScene(scene5); 
    zgjedhkonsumatorinstage.setTitle("Pit Stop"); 
    zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL); 
    zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage); 
} 

@FXML 
public void zgjedhKonsumatorin() 
{ 
    zgjedhkonsumatorinstage.showAndWait(); 
} 

MainWindowFXML.fxml

<TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 

ZgjedhKonsumatorinController.java

@FXML 
public void zgjedhKonsumatorin() 
{ 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowFXML.fxml")); 
    MainWindowController c = (MainWindowController) loader.getController(); 
    c.konsumatoriPunetField.textProperty().setValue("ertani"); 
    main.zgjedhkonsumatorinstage.close(); 

} 

ZgjedhKonsumatorinFXML.fxml

<Button mnemonicParsing="false" onAction="#zgjedhKonsumatorin" prefWidth="150.0" text="Zgjedh Konsumatorin" /> 

出力:

Caused by: java.lang.NullPointerException 
    at main.ZgjedhKonsumatorinController.zgjedhKonsumatorin(ZgjedhKonsumatorinController.java:193) 
    ... 102 more 

P.S.これは、あなたがFXMLファイルをロードするとき

c.konsumatoriPunetField.textProperty().setValue("ertani"); 
+0

あなたはこの部分を編集することを忘れていましたか?そのフィールドはまだ静的です... –

+0

申し訳ありませんが、私はここでそれを編集するのを忘れていました。 –

+0

デバッグnullのオブジェクトはどれですか? – MordechayS

答えて

1

コントローラは、(コントローラクラスは、FXMLファイルで指定されたFXMLLoaderによって作成されたZgjedhKonsumatorinController(例外)でライン193であるので、これはそれがことができる唯一の時間ですを作成してください)。したがって、load()を呼び出す前にloader.getController()を呼び出すと、返される値はnullになります。従って、あなたのコードではcはnullで、あなたはnullポインタの例外を受け取ります。

ここでloader.load()を呼び出すことは実際には役に立ちません。 nullポインタの例外は修正されますが、もちろんFXMLファイルで定義されたUIの新しいインスタンスとコントローラの新しいインスタンスをロードします。したがって、テキストを設定しているテキストフィールドは、表示されるテキストフィールドではなく、何も起こりません。

作成したウィンドウでshowAndWait()を使用しているため、showAndWait()コールが完了した後で、テキストを設定する最も簡単な方法は、MainWindowControllerに戻すことです。 showAndWait()は、ウィンドウが閉じられるまで実行をブロックするので、ウィンドウが閉じられるまでテキストフィールドは変更されません。

まず、テキストを取得するためZgjedhKonsumatorinController方法を定義します。

public class ZgjedhKonsumatorinController { 

    @FXML 
    public void zgjedhKonsumatorin() 
    { 
     main.zgjedhkonsumatorinstage.close(); 
    } 

    public String getText() { 
     // in real life you can get text from the controls in ZgjedhKonsumatorinFXML.fxml 
     return "ertani" ; 
    } 
} 

とすぐに戻ってMainWindowControllerであなたが行うことができます:

public class MainWindowController { 

    @FXML 
    // Note: it is ALWAYS a mistake to initialize @FXML-injected fields. 
    // Just declare them and let the FXMLLoader initialize them 
    // (that is the whole point of @FXML) 
    private TextField konsumatoriPunetField ; 

    private ZgjedhKonsumatorinController zgjedhKonsumatorinController ; 

    @FXML 
    private void initialize() 
    { 
     FXMLLoader loader5 = new FXMLLoader(); 
     loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml")); 
     BorderPane border5 = new BorderPane(); 
     border5 = loader5.load(); 
     zgjedhKonsumatorinController = loader.getController(); 
     Scene scene5 = new Scene(border5); 
     zgjedhkonsumatorinstage.setScene(scene5); 
     zgjedhkonsumatorinstage.setTitle("Pit Stop"); 
     zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL); 
     zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage); 
    } 

    @FXML 
    public void zgjedhKonsumatorin() 
    { 
     zgjedhkonsumatorinstage.showAndWait(); 
     konsumatoriPunetField.setText(zgjedhKonsumatorinController.getText()); 
    } 

} 
+0

ありがとう、私は本当にそれをappriciate :)それは今素晴らしいです! –

関連する問題