0

OK i have a bit of a weird one probably easy to be fair but im working on a java fx app with sql database connected via JDBC. everything working good except for one thing. When you press a button the scene changes and and the data is pulled from the database this all works perfectly the debug shows that the correct values are were they need to be but its when i call a method such as label1.setText(someString); it throws a NPE.

So i run debug all the variables look good. then i do a simple print on the node itself it prints null i thought the fxml injection handled instantiation. i have checked the fxml files everything has an fx-id and every thing is mentioned in the class. its probably something really stupid ive overlooked.

this is the method that throws the Exception this class contains the mehtods start, main, handle and this method

     public void insertData(ResultSet rsIn) {
                    try {

                        while (rsIn.next()) {
                            int housenumber = rsIn.getInt("housenumber");
                            String maintext = rsIn.getString("maintext");
                            String img1 = rsIn.getString("img1");
                            String img2 = rsIn.getString("img2");
                            String img3 = rsIn.getString("img3");
                            String img4 = rsIn.getString("img4");

 // scrollLab is of Text type
      scrollLab.setText(maintext);//here is where the NPE is thrown 

                        }

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

                    }
              }

the other class contains all sql related methods and the getStuff method where Resultset rsIn comes from

public void getstuff(String buttonid) {

        switch (buttonid) {

            case "winterfell":
                try {


                    conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    Statement stark = conn.createStatement();
                    System.out.println("Fetching records");
                    String sql = "SELECT * FROM houseinfo WHERE housenumber = 1";
                    rs = stark.executeQuery(sql);
                    GOT_Map map = new GOT_Map();
                      map.insertData(rs);

                   }catch(SQLException e){}
                  break;

and finally the handle method starts the process this is inn the controller class same class as where the exception is thrown

@Override
    public void handle(ActionEvent event) {
        Sql_handler sql = new Sql_handler();
        try {

// will load the new scene for all map buttons but will pull data from the database to fill blanks
            if (event.getSource() == winterfell) {
                System.out.println("winterfell clicked");

                //getting reference to the button's stage 
                Stage stageref = (Stage) winterfell.getScene().getWindow();
                inforoot = FXMLLoader.load(getClass().getResource("infoView.fxml"));
                stageref.setScene(scene2);
                sql.getstuff("winterfell");

this is probably an easy fix that ive overlook been working on this none stop past two days any help is much appreciated

JavaScrub
  • 133
  • 9
  • if you don't initialize your label1 then you will get NPE – Youcef LAIDANI Apr 30 '17 at 20:29
  • @YCF_L i thought the fxml injection handles that. i can call methods on the first scenes nodes without any of this Label label1 = new Label(); – JavaScrub Apr 30 '17 at 20:33
  • 1
    @JavaScrub The `FXMLLoader` injects fields annotated `@FXML` in the controller. You are not calling `insertData()` on the controller: you are calling it on `map`, which is an object you instantiated yourself. So `map.scrollLab` is null. – James_D Apr 30 '17 at 20:36
  • @ James_D 2 oh i see thanks a lot man. an easy fix just as i suspected – JavaScrub Apr 30 '17 at 20:41
  • How easy a fix depends on how easy it is to get a reference to the actual controller in your `getStuff()` method. – James_D Apr 30 '17 at 20:42

0 Answers0