2014-09-22 35 views
0

我在写一个音乐播放器,并且无法将我的可观察阵列与控制器中定义的TableView链接起来。当项目运行时,我得到NullPointerException在线评论。我不知道是什么让它发生,以及如何解决这个问题。JavaFX - 无法将控制器与数据阵列连接

Main.java

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.control.SplitPane; 
import javafx.stage.Stage; 


public class Main extends Application { 
    ObservableList<CModel> modelData = FXCollections.observableArrayList(); 


    @Override 
    public void start(Stage primaryStage) { 
     try { 
      FXMLLoader loader = new FXMLLoader(); 

      SplitPane root = FXMLLoader.load(getClass().getResource("Player.fxml")); 

      Scene scene = new Scene(root); 
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 

      primaryStage.setScene(scene); 

      PlayerController controller = loader.getController(); 

      //this line gives exception 
      controller.setData(this); 

      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public ObservableList<CModel> getPersonData() { 
     return modelData; 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

PlayerController.java

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Menu; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 

public class PlayerController { 

    @FXML 
    private TableView<CModel> cTable; 

    @FXML 
    private TableColumn<CModel, String> artCol; 

    @FXML 
    private TableColumn<CModel, String> albCol; 

    @FXML 
    private TableColumn<CModel, String> trNameCol; 

    @FXML 
    private TableColumn<CModel, String> trNoCol; 

    @FXML 
    private TableColumn<CModel, String> durCol; 

    @FXML 
    private Menu addFile; 

    @FXML 
    private Menu addFolder; 

    @FXML 
    private Menu savePlayL; 

    @FXML 
    private Menu loadPlayL; 


    private Files files; 



    private Main main; 

    public PlayerController() { 

    } 

    @FXML 
    private void initialize() { 
     artCol.setCellValueFactory(
       new PropertyValueFactory<CModel,String>("artistName") 
      ); 

     albCol.setCellValueFactory(
       new PropertyValueFactory<CModel,String>("albumName") 
      ); 

     trNameCol.setCellValueFactory(
       new PropertyValueFactory<CModel,String>("trackName") 
      ); 

     trNoCol.setCellValueFactory(
       new PropertyValueFactory<CModel,String>("trackDurationName") 
      ); 

     durCol.setCellValueFactory(
       new PropertyValueFactory<CModel,String>("trackNumberName") 
      ); 



    } 


    @FXML 
    protected void handleAddFile(ActionEvent event) { 
     files = new Files(); 
     files.openFile(); 
    } 

    public void setData(Main mainApp) { 
     this.main = mainApp; 

     cTable.setItems(main.getPersonData()); 
    } 

} 

回答

2

你需要调用实例化对象的loader.load()方法,而是采用静态方法,让控制器之前,采取详情请看here

0

这里有几件事情需要考虑。为了得到控制,你需要记住以下几点:

  • 使用FXMLoader参考加载FXML
  • 调用非静态load方法。为了做到这一点,通过InputStream作为参数。

Main.Java里面,你应该有:

FXMLLoader loader = new FXMLLoader(); 
SplitPane root = (SplitPane)loader.load(getClass().getResource("Player.fxml").openStream()); 

如果你不想使用InputStream,你也可以通过资源在构造函数中,并调用非staic load()方法

FXMLLoader loader = new FXMLLoader(getClass().getResource("Player.fxml")); 
SplitPane root = (SplitPane)loader.load(); 
+0

我做了你写的,它没有帮助。仍然我得到空例外 – regularmike 2014-09-22 12:27:14

+0

我错过了openStream,请再次检查。如果我们不使用'InputStream'作为参数,它仍然会引用静态方法 – ItachiUchiha 2014-09-22 12:42:49

+0

现在,它的工作原理!谢谢,惊讶的是我在Oracle教程中找不到它,这是抓住javafx的一个非常重要的方面。 – regularmike 2014-09-22 17:08:34