2017-03-05 18 views
1

的数据。这里有一个新的项目与相关的代码。正如您在textfield中看到的那样,即使initData应该初始化testVar变量,它也不会发生,并且在initialize()变量中为null。我需要在那里获取变量,所以我可以从网络连接中将数据从表中取出。下面的代码:ObjectOutputStream在PlayerList.java中不可用 - 无法再获取表

FXMLController.java:

package de.freakyonline.testproject; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextArea; 

public class FXMLController implements Initializable { 
    String testVar; 

    @FXML 
    private ResourceBundle resources; 

    @FXML 
    private URL location; 

    @FXML 
    private TextArea testTextArea; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
    // Here it's null  
     testTextArea.appendText("TestVar: " + testVar); 

    } 

    void initData(Remote remoteObj) { 
     this.testVar = remoteObj.getVar(); 
    }  
} 

MainApp.java:

package de.freakyonline.testproject; 

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 


public class MainApp extends Application { 
    Thread remote; 
    Remote remoteObj; 

    String testVar = new String("success"); 

    @Override 
    public void start(Stage stage) throws Exception { 

     FXMLLoader root = new FXMLLoader(
      getClass().getResource("/fxml/Scene.fxml") 
     ); 

     try { 
      remoteObj = new Remote(testVar); 
      remote = new Thread(remoteObj); 
     } catch (Exception ex) { ex.printStackTrace(); } 
     remote.start(); 

     Scene scene = new Scene((Parent) root.load()); 
     FXMLController controller = root.<FXMLController>getController(); 
     controller.initData(remoteObj); 

     scene.getStylesheets().add("/styles/Styles.css"); 

     stage.setTitle("TestProjects ..."); 
     stage.setScene(scene); 
     stage.show(); 


    } 

    /** 
    * The main() method is ignored in correctly deployed JavaFX application. 
    * main() serves only as fallback in case the application can not be 
    * launched through deployment artifacts, e.g., in IDEs with limited FX 
    * support. NetBeans ignores main(). 
    * 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 

Scene.fxml:

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

<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.layout.BorderPane?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.freakyonline.testproject.FXMLController"> 
    <center> 
     <TextArea fx:id="testTextArea" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /> 
    </center> 
</BorderPane> 

Remote.java:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package de.freakyonline.testproject; 

import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.net.Socket; 

/** 
* 
* @author uwe 
*/ 
public class Remote implements Runnable { 
    private String testVar; 

    public Remote (String testVar) { 
     this.testVar = testVar; 
    } 

    public String getVar() { 
     return testVar; 
    } 

    public void run() { 
    } 
} 
+0

请将代码减少到相关部分,因为没有人愿意阅读代码 –

+0

的页面完成:)谢谢。 –

+0

尝试向后追踪从它的空点 –

回答

0

FXML加载时,控制器的initialize()方法由FXMLLoader调用,即在调用load()期间调用该方法。在你的示例代码中,你调用initData()你叫load(),所以当执行initialize()方法时,不能初始化testVar。由于示例代码中的控制器由FXMLLoader创建,所以在调用initialize()之前,不能调用initData(...)

(旁白:使用一些简单的调试技术,如采用步进式调试器,甚至只是打印邮件时调用的每个方法,将使它非常明确的问题是在这里什么)

最简单的解决方法是只需移动取决于testVarinitData()方法的代码(或者是testVar初始化后调用一些其他的方法):

public class FXMLController implements Initializable { 
    String testVar; 

    @FXML 
    private ResourceBundle resources; 

    @FXML 
    private URL location; 

    @FXML 
    private TextArea testTextArea; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

    } 

    void initData(Remote remoteObj) { 
     this.testVar = remoteObj.getVar(); 
     testTextArea.appendText("TestVar: " + testVar); 
    }  
} 

这里的轻微缺点是的初始化对照oller分布在多个方法上,在控制器生命周期的不同点执行。其他选项包括在代码中设置控制器,而不是在FXML文件中指定控制器类,或使用控制器工厂。在代码中设置控制器的缺点是,如果您使用工具(例如Scene Builder)来设计FXML,它将不会意识到正在使用的控制器类。使用控制器工厂更复杂,但避免了这个问题。