JavaFx TableView中的java.lang.NullPointerException

时间:2016-07-26 17:50:22

标签: java javafx nullpointerexception

对于大多数人来说,这可能是一个简单的问题,但我对此感到困惑,无法找到有用的答案。

我在:

收到java.lang.NullPointerException
package application;

import java.io.IOException;
import java.net.URL;
import java.util.Observable;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class MainController implements Initializable
{
    @FXML private TableView<Lista> tabela;
    @FXML private TableColumn<Lista, Integer> lp;
    @FXML private TableColumn<Lista, String> nazwa;
    @FXML private TableColumn<Lista, Double> cena;
    @FXML private TableColumn<Lista, String> data;

    @FXML
    private Label lblStatus;

    @FXML
    private TextField txtUserName;

    @FXML
    private TextField txtPassword;

    @FXML
    private Button buttonLogin;

    public void Login(ActionEvent event) throws Exception
    {
        if(txtUserName.getText().equals("user") && txtPassword.getText().equals("Password"))
        {
        buttonLogin.getScene().getWindow().hide();  
        //lblStatus.setText("LoginSucess");
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        }
        else 
        {
        lblStatus.setText("Błędne logowanie");
        }
    }

    public void cancelLogin() 
    {
    //c.getScene().getWindow().hide();
    }

    public ObservableList<Lista> ObserwowalnaLista = FXCollections.observableArrayList
    (
    new Lista(1, "Krem", 10.00, "30.07.2016"),
    new Lista(2, "Maść", 12.00, "31.07.2016")   
    );

    @Override
    public void initialize(URL location, ResourceBundle resources) 
    {
        lp.setCellValueFactory(new PropertyValueFactory<Lista, Integer>("lp")); // NullPointerException 
        nazwa.setCellValueFactory(new PropertyValueFactory<Lista, String>("nazwa"));
        cena.setCellValueFactory(new PropertyValueFactory<Lista, Double>("cena"));
        data.setCellValueFactory(new PropertyValueFactory<Lista, String>("data"));
        tabela.setItems(ObserwowalnaLista);
    }
}

Main.fxml:

  
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <TableView fx:id="tabela" prefHeight="400.0" prefWidth="301.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="99.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="lp" prefWidth="75.0" text="Lp." />
          <TableColumn fx:id="nazwa" prefWidth="75.0" text="Nazwa" />
            <TableColumn fx:id="cena" prefWidth="75.0" text="Cena" />
            <TableColumn fx:id="data" prefWidth="75.0" text="Data" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

班级Lista:

package application;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class Lista 
{
    private final SimpleIntegerProperty lp;
    private final SimpleStringProperty nazwa;
    private final SimpleDoubleProperty cena;
    private final SimpleStringProperty data;

    public Lista(Integer lp, String nazwa, Double cena, String data) 
    {
        super();
        this.lp = new SimpleIntegerProperty(lp);
        this.nazwa = new SimpleStringProperty(nazwa);
        this.cena = new SimpleDoubleProperty(cena);
        this.data = new SimpleStringProperty(data);
    }

    public Integer getLp() {
        return lp.get();
    }

    public String getNazwa() {
        return nazwa.get();
    }

    public Double getCena() {
        return cena.get();
    }


    public String getData() {
        return data.get();
    }
}

例外:

javafx.fxml.LoadException: 
/C:/Users/Radek/Desktop/ProjektSylwiaFx/bin/application/Login.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:17)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    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.NullPointerException
    at application.MainController.initialize(MainController.java:77)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more

在其他线程中,问题是fx:id或@FXML标记,但我认为这不是问题。有人能给我一个暗示可能是错误的吗?

0 个答案:

没有答案