javafxjavafx-9

Is it possible to add multiple table view using a single Controller class and multiple entity Class in Java FX?


My question is about JavaFX-9. I am trying to handle two table view in one FXML Controller class. But it is giving me a Null pointer exception. How to solve this?

The first TableView (studentTable) is working and second table(rTable) is not working.

home.java:

    package Home;

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

    public class home extends Application{
        public void start(Stage stage)throws Exception{
            Parent root=(Parent) FXMLLoader.load(getClass().getResource("Home.fxml"));
            Scene scene=new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Result Analysis System");
            stage.show();
        }
        public static void main(String[] args){
            launch(args);
        }
    }

homeController.java

    package Home;

    /*_____________________
    Error in this file
    _____________________*/
    import dbUtils.dbConnection;
    //import Home.rControl;
    //import Home.resultData;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableArray;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.*;
    //import javafx.scene.control.DatePicker;
    import javafx.scene.control.cell.PropertyValueFactory;
    //import Home.resultData;
    import java.net.URL;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import java.util.ResourceBundle;


    public class homeController /*extends resultController*/ implements Initializable{


        //Student tab
        @FXML
        private TextField usn;
        @FXML
        private TextField name;
        @FXML
        protected TableView<studentData> studentTable;
        @FXML
        protected TableColumn<studentData,String> USNcolumn;
        @FXML
        protected TableColumn<studentData,String> Namecolumn;

        //Result tab
        @FXML
        private TextField rusn;
        @FXML
        private ComboBox<option> rSelectSem;
        @FXML
        private TextField rSub1;
        @FXML
        private TextField rSub2;
        @FXML
        private TextField rSub3;
        @FXML
        private TextField rSub4;
        @FXML
        private TextField rSub5;
        @FXML
        private TextField rSub6;
        @FXML
        private TextField rSub7;
        @FXML
        private TextField rSub8;
        @FXML
        private Button rAdd;
        @FXML
        private Button rLoad;
        @FXML
        private Button rClear;
        @FXML
        private ComboBox<option> rSelectSem1;
        @FXML
        private Button rLoad1;
        @FXML
        private TableView<resultData> rTable;
        @FXML
        private TableColumn<resultData,String> rColusn;
        @FXML
        private TableColumn<resultData,String> rColname;
        @FXML
        private TableColumn<resultData,Integer> rColsub1;
        @FXML
        private TableColumn<resultData,Integer> rColsub2;
        @FXML
        private TableColumn<resultData,Integer> rColsub3;
        @FXML
        private TableColumn<resultData,Integer> rColsub4;
        @FXML
        private TableColumn<resultData,Integer> rColsub5;
        @FXML
        private TableColumn<resultData,Integer> rColsub6;
        @FXML
        private TableColumn<resultData,Integer> rColsub7;
        @FXML
        private TableColumn<resultData,Integer> rColsub8;
        @FXML
        private TableColumn<resultData,Integer> rColtotal;




        //Analyze tab
        @FXML
        private ComboBox<option> aSelectSem;
        @FXML
        private Button aHighmarks;
        @FXML
        private Button aPassedstudent;
        @FXML
        private Button aFailedstudent;
        @FXML
        private Button aListallstudent;
        @FXML
        private Button adistiction;
        @FXML
        private Button aFirstclass;
        @FXML
        private Button aSecondclass;
        @FXML
        private TableView<analysisData> aTable;
        @FXML
        private TableColumn<analysisData,String> aColusn;
        @FXML
        private TableColumn<analysisData,String> aColname;
        @FXML
        private TableColumn<analysisData,Integer> aColsub1;
        @FXML
        private TableColumn<analysisData,Integer> aColsub2;
        @FXML
        private TableColumn<analysisData,Integer> aColsub3;
        @FXML
        private TableColumn<analysisData,Integer> aColsub4;
        @FXML
        private TableColumn<analysisData,Integer> aColsub5;
        @FXML
        private TableColumn<analysisData,Integer> aColsub6;
        @FXML
        private TableColumn<analysisData,Integer> aColsub7;
        @FXML
        private TableColumn<analysisData,Integer> aColsub8;
        @FXML
        private TableColumn<analysisData,Integer> aColtotal;


        protected dbConnection dc;
        protected ObservableList<studentData> data;
        //private ObservableList<resultData> list;
        protected String sql = "SELECT * FROM studentDet";
        @Override
        public void initialize(URL url, ResourceBundle rb){
            this.dc = new dbConnection();
            this.rSelectSem.setItems(FXCollections.observableArrayList(option.values()));
            this.rSelectSem1.setItems(FXCollections.observableArrayList(option.values()));
            this.aSelectSem.setItems(FXCollections.observableArrayList(option.values()));
            //  rTable.setItems(list);



        }



        //Load student data in student tab
        //This is working
        @FXML
        private void loadStudentData(ActionEvent event) throws SQLException{

            try {
                Connection conn = dbConnection.getConnection();
                this.data = FXCollections.observableArrayList();
                ResultSet rs = conn.createStatement().executeQuery(sql);
                while (rs.next()){
                    this.data.add(new studentData(rs.getString(1),rs.getString(2)));
                }
            }
            catch (SQLException e){
                System.err.println("error" + e);
            }

            this.USNcolumn.setCellValueFactory(new PropertyValueFactory<studentData,String>("USN"));
            this.Namecolumn.setCellValueFactory(new PropertyValueFactory<studentData,String>( "Name"));

            this.studentTable.setItems(null);
            this.studentTable.setItems(this.data);



        }

        //This is working
        //Add student in student tab
        @FXML
        private void addStudent(ActionEvent actionEvent) {

            String sqlInsert="INSERT INTO studentDet(USN,Name) VALUES(?,?)";
            try {
                //here the name is same but it doesn't matter because this is a local variable
                Connection conn=dbConnection.getConnection();
                PreparedStatement statement=conn.prepareStatement(sqlInsert);
                statement.setString(1,this.usn.getText());
                statement.setString(2,this.name.getText());
                statement.execute();
                conn.close();




            } catch (SQLException e) {
                e.printStackTrace();
            }

        }


        //This is working
        //Clear feilds in Student tab
        @FXML
        private void clearFields(ActionEvent actionEvent)
        {
            this.usn.setText("");
            this.name.setText("");
        }

        @FXML
        private void rclearFields(ActionEvent actionEvent)
        {
            this.rusn.setText("");
            this.rSub1.setText("");
            this.rSub2.setText("");
            this.rSub3.setText("");
            this.rSub4.setText("");
            this.rSub5.setText("");
            this.rSub6.setText("");
            this.rSub7.setText("");
            this.rSub8.setText("");
        }



        //This is working
        //Add marks to sem in Result tab
        @FXML
        private void addSemMarks(ActionEvent actionEvent)
        {
            String sqlInsert;
            try {
                switch (((option) this.rSelectSem.getValue()).toString()) {
                    case "SEM1":
                        sqlInsert="INSERT INTO SEM1(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM2":
                        sqlInsert="INSERT INTO SEM2(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM3":
                        sqlInsert="INSERT INTO SEM3(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM4":
                        sqlInsert="INSERT INTO SEM4(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM5":
                        sqlInsert="INSERT INTO SEM5(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM6":
                        sqlInsert="INSERT INTO SEM6(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM7":
                        sqlInsert="INSERT INTO SEM7(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                    case "SEM8":
                        sqlInsert="INSERT INTO SEM8(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
                        semMarksAdd(sqlInsert);
                        break;
                }
            }
            catch (Exception ex){
                ex.printStackTrace();
            }

        }
        private void semMarksAdd(String sqlinsert){
            //String sqlInsert="INSERT INTO SEM1(USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E) VALUES(?,?,?,?,?,?,?,?,?)";
            try {
                //here the name is same but it doesn't matter because this is a local variable
                Connection conn=dbConnection.getConnection();
                PreparedStatement statement=conn.prepareStatement(sqlinsert);
                statement.setString(1,this.rusn.getText());
                statement.setString(2,this.rSub1.getText());
                statement.setString(3,this.rSub2.getText());
                statement.setString(4,this.rSub3.getText());
                statement.setString(5,this.rSub4.getText());
                statement.setString(6,this.rSub5.getText());
                statement.setString(7,this.rSub6.getText());
                statement.setString(8,this.rSub7.getText());
                statement.setString(9,this.rSub8.getText());

                statement.execute();
                conn.close();

            } 
            catch (SQLException e) {
                e.printStackTrace();
            }

        }

        //#########################################################Stable upto here
        //From here it is giving error
        //TO LOAD STUDENT MARKS DATA IN RESULT TABLE
        /*@FXML
        private void loadResultData(ActionEvent actionEvent){
            String sqlLoad;
            try {
                switch (((option) this.rSelectSem.getValue()).toString()) {
                    case "SEM1":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM1";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM2":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM2";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM3":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM3";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM4":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM4";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM5":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM5";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM6":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM6";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM7":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM7";
                        loadRdata(sqlLoad);
                        break;
                    case "SEM8":
                        sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM8";
                        loadRdata(sqlLoad);
                        break;
                }
            }
            catch (Exception ex){
                ex.printStackTrace();
            }

        }*/


    @FXML
        public void loadResultData(ActionEvent actionEvent) {
            String sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM1";
            loadRdata(sqlLoad); //Error here
        }

    /*private void loadResultData(ActionEvent actionEvent) {
        String sqlLoad = "SELECT USN,SUB1E,SUB2E,SUB3E,SUB4E,SUB5E,SUB6E,SUB7E,SUB8E,TOTAL FROM SEM1";
        rc.loadRdata(sqlLoad);
    }*/
        private ObservableList<resultData> list = FXCollections.observableArrayList();
        private void loadRdata(String sqlLoad) {
            try {
                Connection conn = dbConnection.getConnection();
                this.list = FXCollections.observableArrayList();
                //System.out.println("Hello"); //working
                ResultSet rs = conn.createStatement().executeQuery(sqlLoad);
                while (rs.next()) {
                    //System.out.println(rs.getString(1));  //Working
                    this.list.add(new resultData(rs.getString(1), rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6), rs.getInt(7), rs.getInt(8), rs.getInt(9), rs.getInt(10)));
                }
                // this.rColusn.setCellValueFactory("Helloworld"); //working
                this.rColusn.setCellValueFactory(new PropertyValueFactory<resultData, String>("rColUsn"));
                this.rColsub1.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub1"));
                this.rColsub2.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub2"));
                this.rColsub3.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub3"));
                this.rColsub4.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub4"));
                this.rColsub5.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub5"));
                this.rColsub6.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub6"));
                this.rColsub7.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub7"));
                this.rColsub8.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColSub8"));
                this.rColtotal.setCellValueFactory(new PropertyValueFactory<resultData, Integer>("rColTotal"));
                System.out.println("after block1");
                list.clear();
                System.out.println("after block2");
                rTable.setItems(list);// CAUSING NULL POINTER EXCEPTION
                System.out.println("after block3");//NOT PRINTING
            }
            catch (Exception e) {//System.out.println(" "+e);
                System.err.println("error" + e);
            }
        }


    }

resultData.java

    package Home;

    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;


    public class resultData {
        private final StringProperty rColUsn;
        //private final StringProperty rColName;
        private final IntegerProperty rColSub1;
        private final IntegerProperty rColSub2;
        private final IntegerProperty rColSub3;
        private final IntegerProperty rColSub4;
        private final IntegerProperty rColSub5;
        private final IntegerProperty rColSub6;
        private final IntegerProperty rColSub7;
        private final IntegerProperty rColSub8;
        private final IntegerProperty rColTotal;

        public resultData(String usn,Integer sub1,Integer sub2,Integer sub3,Integer sub4,Integer sub5,Integer sub6,Integer sub7,Integer sub8,Integer total){
        System.out.println("result data");// TWO TIMES IT WORKS,BUT THE THERE ARE 10 COLUMNS AND IT SHOULD PRINT 10 TIMES.
            rColUsn = new SimpleStringProperty(usn);
            //this.rColName = new SimpleStringProperty(name);
            rColSub1 = new SimpleIntegerProperty(sub1);
            rColSub2 = new SimpleIntegerProperty(sub2);
            rColSub3 = new SimpleIntegerProperty(sub3);
            rColSub4 = new SimpleIntegerProperty(sub4);
            rColSub5 = new SimpleIntegerProperty(sub5);
            rColSub6 = new SimpleIntegerProperty(sub6);
            rColSub7 = new SimpleIntegerProperty(sub7);
            rColSub8 = new SimpleIntegerProperty(sub8);
            rColTotal = new SimpleIntegerProperty(total);
        }

        public String getrColUsn() {
            return rColUsn.get();
        }

        public StringProperty rColUsnProperty() {
            return rColUsn;
        }

        public void setrColUsn(String rColUsn) {
            this.rColUsn.set(rColUsn);
        }

        public int getrColSub1() {
            return rColSub1.get();
        }

        public IntegerProperty rColSub1Property() {
            return rColSub1;
        }

        public void setrColSub1(int rColSub1) {
            this.rColSub1.set(rColSub1);
        }

        public int getrColSub2() {
            return rColSub2.get();
        }

        public IntegerProperty rColSub2Property() {
            return rColSub2;
        }

        public void setrColSub2(int rColSub2) {
            this.rColSub2.set(rColSub2);
        }

        public int getrColSub3() {
            return rColSub3.get();
        }

        public IntegerProperty rColSub3Property() {
            return rColSub3;
        }

        public void setrColSub3(int rColSub3) {
            this.rColSub3.set(rColSub3);
        }

        public int getrColSub4() {
            return rColSub4.get();
        }

        public IntegerProperty rColSub4Property() {
            return rColSub4;
        }

        public void setrColSub4(int rColSub4) {
            this.rColSub4.set(rColSub4);
        }

        public int getrColSub5() {
            return rColSub5.get();
        }

        public IntegerProperty rColSub5Property() {
            return rColSub5;
        }

        public void setrColSub5(int rColSub5) {
            this.rColSub5.set(rColSub5);
        }

        public int getrColSub6() {
            return rColSub6.get();
        }

        public IntegerProperty rColSub6Property() {
            return rColSub6;
        }

        public void setrColSub6(int rColSub6) {
            this.rColSub6.set(rColSub6);
        }

        public int getrColSub7() {
            return rColSub7.get();
        }

        public IntegerProperty rColSub7Property() {
            return rColSub7;
        }

        public void setrColSub7(int rColSub7) {
            this.rColSub7.set(rColSub7);
        }

        public int getrColSub8() {
            return rColSub8.get();
        }

        public IntegerProperty rColSub8Property() {
            return rColSub8;
        }

        public void setrColSub8(int rColSub8) {
            this.rColSub8.set(rColSub8);
        }

        public int getrColTotal() {
            return rColTotal.get();
        }

        public IntegerProperty rColTotalProperty() {
            return rColTotal;
        }

        public void setrColTotal(int rColTotal) {
            this.rColTotal.set(rColTotal);
        }
    }

studentData.java

    package Home;

    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;

    public class studentData{
        private final StringProperty USN;
        private final StringProperty Name;

        public studentData(String usn,String name){
            this.USN=new SimpleStringProperty(usn);
            this.Name=new SimpleStringProperty(name);
        }

        public String getUSN() {
            return USN.get();
        }

        public StringProperty USNProperty() {
            return USN;
        }

        public void setUSN(String USN) {
            this.USN.set(USN);
        }

        public String getName() {
            return Name.get();
        }

        public StringProperty nameProperty() {
            return Name;
        }

        public void setName(String name) {
            this.Name.set(name);
        }
    }

Commnd Line Output:

"C:\Program Files\Java\jdk-9.0.1\bin\java" "-javaagent:C:\Program 
Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=55700:C:\Program 
Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -
   /*  WHEN CLICKING TABLE DISPLAY BUTTON FIRST TIME */
        result data
        result data
        after block1
        after block2
        errorjava.lang.NullPointerException                                               
  /*  WHEN CLICKING TABLE DISPLAY BUTTON SECOND TIME*/
        errorjava.lang.NullPointerException
        result data
        result data
        after block1
        after block2

Solution

  • You need to set a value into the table.

    This is not going to work (remove from your loadRdata method):

    this.rTable.setItems(null); // giving error here:Null pointer Exception
    this.rTable.setItems(this.list);
    

    Initialize your ObservableList first:

    private ObservableList<resultData> list = FXCollections.observableArrayList();
    

    Then in your method loadRdata clear the values rather than initializing like you are now.

    list.clear();
    

    At the end of you initialize() method you can add the values

    rTable.setItems(list);