1 2 Previous Next 22 Replies Latest reply: Mar 19, 2013 1:26 PM by biochemistry43 RSS

    I can't populate a TableView. NullPointerException in TableView

    biochemistry43
      Hi all.

      I'm having a lot of problems to populate a table with data.

      The following is my code:
      public class Panel_MarcadoresController implements Initializable {
      
          @FXML static Tab lineas;
          @FXML TabPane marcadores;
          @FXML Button finalizarPolilinea;
          @FXML ComboBox comboGrosorLinea;
          @FXML TableView<CoordenadasLinea> tablaCoordenadasDeLinea;
          @FXML TableColumn itemCol;
          @FXML TableColumn latitudCol;
          @FXML TableColumn longitudCol;
      
         @Override
          public void initialize(URL url, ResourceBundle rb) 
          {
      
          }
      
          public void leerCoordenadasLineaDesdeMapa(String datos)
          {
              Pattern p = Pattern.compile("[,]+");
              String[] result = p.split(datos);
              ObservableList<CoordenadasLinea> dataCoordLinea = FXCollections.observableArrayList();
              
              int item = 0;
              String latitud = "";
              String longitud  = "";
              for (int i = 0; i < result.length; i++)
              {
                  item =  i;
                  latitud = result;
      longitud = result [i];
      CoordenadasLinea coordenadasLinea = new CoordenadasLinea(latitud,longitud);
      dataCoordLinea.add(coordenadasLinea);
      }
      tablaCoordenadasDeLinea.setItems(dataCoordLinea);

      }


      ///// The following is a inner class
      public class CoordenadasLinea
      {
      //private final SimpleIntegerProperty item = new SimpleIntegerProperty();
      private final SimpleStringProperty latitud = new SimpleStringProperty("");
      private final SimpleStringProperty longitud = new SimpleStringProperty("");

      public CoordenadasLinea()
      {
      this("","");
      }

      public CoordenadasLinea(String lat, String Long)
      {
      setLatitud(lat);
      setLongitud(Long);
      }

      // public void setItem(int it)
      // {
      // item.set(it);
      // }

      public void setLatitud(String lat)
      {
      latitud.set(lat);
      }

      public void setLongitud(String lon)
      {
      longitud.set(lon);
      }

      // public int getItem()
      // {
      // return item.get();
      // }
      public String getLatitud()
      {
      return latitud.get();
      }
      public String getlongitud()
      {
      return longitud.get();
      }
      }
      My FXML file:
      <?xml version="1.0" encoding="UTF-8"?>

      <?import java.lang.*?>
      <?import java.net.*?>
      <?import java.util.*?>
      <?import javafx.collections.*?>
      <?import javafx.scene.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.layout.*?>
      <?import javafx.collections.*?>
      <?import javafx.geometry.Insets?>
      <?import java.lang.*?>
      <?import javafx.scene.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.control.cell.*?>
      <?import javafx.scene.layout.*?>
      <?import fxmltableview.*?>

      <AnchorPane id="AnchorPane" prefHeight="798.0" prefWidth="426.0" styleClass="mainFxmlClass, frame" xmlns:fx="http://javafx.com/fxml" fx:controller="signa.Panel_MarcadoresController">
      <children>

      <TableView fx:id="coordenadasDeLinea" layoutX="47.0" layoutY="436.0" prefHeight="138.0" prefWidth="317.0">
      <columns>
      <TableColumn prefWidth="75.0" text="Item" fx:id="itemCol"/>

      <TableColumn prefWidth="120.0" text="Latitud" fx:id="latitudCol" >
      <cellValueFactory>
      <PropertyValueFactory property="latitud" />
      </cellValueFactory>
      </TableColumn>
      <TableColumn prefWidth="120.0" text="Longitud" fx:id="longitudCol" >
      <cellValueFactory>
      <PropertyValueFactory property="longitud" />
      </cellValueFactory>
      </TableColumn>
      </columns>
      </TableView>
      </children>
      <layoutY>
      <Long fx:value="0" />
      </layoutY>
      <stylesheets>
      <URL value="@SIGNA.css" />
      </stylesheets>
      </AnchorPane>
      The idea is that the table is filled with the data that is passed as a parameter to the method.
      
      I've tried everything and I can't ... with this last code I present here, I get this exception:  
      
      Exception in thread "Thread-5" java.lang.NullPointerException
      at signa.Panel_MarcadoresController.leerCoordenadasLineaDesdeMapa(Panel_MarcadoresController.java:122)   this points here:
      tablaCoordenadasDeLinea.setItems(dataCoordLinea);
      I think I'm making a basic error but I'm locked ... help                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: I can't populate a TableView. NullPointerException in TableView
          James_D
          Can you show the code where you get a reference to the controller (Panel_MarcadoresController) and where you call the leerCoordenadasLineaDesdeMapa method on it?
          • 2. Re: I can't populate a TableView. NullPointerException in TableView
            shakir.gusaroff
            The following method
            public String getlongitud()
                    {
                        return longitud.get();
                    }
            should be like this:
            public String getLongitud()   //uppercase “L”
                     {
                        return longitud.get();
                    }
            • 3. Re: I can't populate a TableView. NullPointerException in TableView
              biochemistry43
              Sorry shakir ... I do not understand what is the importance of this. (Either way I already did and no change)
              • 4. Re: I can't populate a TableView. NullPointerException in TableView
                biochemistry43
                public  class EvaluateInput extends Evaluate 
                {
                    private String datos;
                
                   public void changeMap(String instruccion, String datos)
                    {
                        Pattern p = Pattern.compile("[,]+");
                        String[] result = p.split(datos);
                        
                       
                        switch(instruccion)
                        {
                            case "@SELECT_OPEATIONAL_ AREA#" :
                                
                                ......
                            break;
                                
                            case "@CLOSE_POLYLINE_VALUES#" :   //Here I call the method
                
                                Panel_MarcadoresController panelMarcadores = new Panel_MarcadoresController();
                                panelMarcadores.leerCoordenadasLineaDesdeMapa(datos);
                            break;
                            case "@LEFT#" : 
                                System.out.println("@LEFT#");
                            break;
                                
                            case "@RIGHT#" : 
                                System.out.println("@RIGHT#");
                            break;
                                
                            case "@3D#" : 
                                
                            break;
                           . 
                           .
                           .
                           }
                      }
                }
                • 5. Re: I can't populate a TableView. NullPointerException in TableView
                  James_D
                  Shakir is right: you need the get...() method to be named correctly in order for the table column to find the correct property. That's not causing your NullPointerException, though...
                  • 6. Re: I can't populate a TableView. NullPointerException in TableView
                    James_D
                    Here is your problem.

                    The Panel_MarcadoresController is your controller class for the view defined in your FXML file. This means when the FXML file is parsed, due to you calling something like FXMLLoader.load(getClass().getResource(...)), it will create an object from the controller class, and then initialize the @FXML-annotated fields for that object.

                    In your code, you have
                    Panel_MarcadoresController panelMarcadores = new Panel_MarcadoresController();
                    panelMarcadores.leerCoordenadasLineaDesdeMapa(datos);
                    This creates a new object from the Panel_MarcadoresController class. This is not the same object the FXMLLoader created, so it's @FXML-annotated attributes are not initialized. Hence the TableView is still null and when you call setItems(...) on it you get a null pointer exception.

                    You need to call the leerCoordenadasLineaDesdeMapa(...) method on the same object that was constructed by the FXMLLoader. Where you call FXMLLoader.load(...) you can do the following:
                    // AnchorPane root = FXMLLoader.load(getClass().getResource("....fxml"));
                    FXMLoader loader = new FXMLLoader(getClass().getResource("....fxml"));
                    AnchorPane root = (AnchorPane) loader.load();
                    Panel_MarcadoresController controller = (Panel_MarcadoresController) loader.getController();
                    Then you need to pass the controller reference to your EvaluateInput class and call leerCoodenadasLineaDesdeMapa(datos) on that reference.
                    • 7. Re: I can't populate a TableView. NullPointerException in TableView
                      biochemistry43
                      Sorry I can not fully understand what you're saying ...

                      This is my start method in main class
                       @Override
                          public void start(Stage primaryStage) {
                              try {
                                  AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("Estructura_General.fxml"));
                                  Scene scene = new Scene(page);
                                  primaryStage.setScene(scene);
                                  primaryStage.setTitle("SIGNA");
                                  primaryStage.initStyle(StageStyle.UNDECORATED);
                                  //primaryStage.setFullScreen(true);
                                  primaryStage.show();
                                  disableDividers(Estructura_GeneralController.splitPane1);
                                  disableDividers(Estructura_GeneralController.splitPane2);
                                  disableDividers(Estructura_GeneralController.splitPane3);
                                  disableDividers(Estructura_GeneralController.splitPane4);
                                  Panel_MarcadoresController controller = (Panel_MarcadoresController) FXMLLoader.getController();
                                  ClientSocket soc = new ClientSocket("@RESIZE#,11,137,1363,795");
                                 (new Thread(new SocketCatcherClients())).start();
                                  
                                  //ApplicationTemplate.start(null, SignaPrincipal.AppTemplate.AppFrame.class);
                                  
                       //           ClientSocket clientSocket = new ClientSocket();
                                  //conectaGuiVisor = new ConectaGuiVisor();
                                 // conectaGuiVisor.ejecutarConectaGuiVisor();
                                  
                                  
                              } catch (Exception ex) {
                                  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                              }
                          }
                      however appears a compilation error: error: non-static method <T>getController() cannot be referenced from a static context
                      Panel_MarcadoresController controller = (Panel_MarcadoresController) FXMLLoader.getController();

                      Also not very clear to me as it should instantiate that object from my class EvaluateInput.

                      Sorry ... just beginning to learn JavaFX, actually ... just beginning to learn programming in general

                      Edited by: biochemistry43 on 15/03/2013 09:33 AM
                      • 8. Re: I can't populate a TableView. NullPointerException in TableView
                        biochemistry43
                        Sorry, sorry ... was considering getController () as a static method. instantiate an object only FXMLLoader
                        FXMLLoader fxmlLoader = new FXMLLoader();
                        .
                        .
                        .
                        Panel_MarcadoresController controller = (Panel_MarcadoresController) fxmlLoader.getController();
                        Now ... how to say I have to call it from my class EvaluateInput

                        Incidentally, excuse the bad English
                        • 9. Re: I can't populate a TableView. NullPointerException in TableView
                          James_D
                          That's it...

                          So now the controller you have created in the start(...) method (the result of calling fxmlLoader.getController()) is the one with tablaCoordenadasDeLinea properly initialized. So you need to call the leerCoordenadasLineaDesdeMapa(...) method on that controller.

                          Currently in your EvaluateInput.changeMap(...) method you create a new Panel_MarcadoresController: that new object does not have the tablaCoordenadasDeLinea initialized.

                          You need the EvaluateInput.changeMap(...) method to have access to the correct Panel_MarcadoresController object. So I guess the question now is: where are you calling changeMap(...) from, and where are you creating a new EvaluateInput()?
                          • 10. Re: I can't populate a TableView. NullPointerException in TableView
                            biochemistry43
                            Here I am calling the changeMap
                            public class ClientExecution implements Runnable
                            {
                                private Socket socket;
                                //private Panel_MarcadoresController controladorDePanel;
                            
                            
                                //Given what you said, I tried to do this
                                public ClientExecution( Socket socket, Panel_MarcadoresController contPanel)
                                {
                                       this.socket = socket;
                                    this.controladorDePanel = contPanel;
                                }
                            
                                public void run ()
                                {
                                    try 
                                    {
                                          .
                                          .
                                          .
                                         String instruccion ="";
                                         String datos="";
                                         EvaluateInput evaluateInput = new EvaluateInput(controladorDePanel);
                                         evaluateInput.changeMap(instruccion, datos);      
                                    catch (IOException e) 
                                     {             e.printStackTrace();          } 
                                   }
                            }
                            I made this change in EvaluateInput:
                            public  class EvaluateInput extends Evaluate 
                            {
                                private String datos;
                                private Panel_MarcadoresController controladorMarcadores;
                                
                                public EvaluateInput (Panel_MarcadoresController contMarcadores)
                                {
                                    this.controladorMarcadores = contMarcadores;
                                }
                                
                                public EvaluateInput()
                                {
                                    
                                }
                                
                                
                                public void changeMap(String instruccion, String datos)
                                {
                                    Pattern p = Pattern.compile("[,]+");
                                    String[] result = p.split(datos);
                                    
                                   
                                    switch(instruccion)
                                    {
                                        case "@SELECT_OPEATIONAL_ AREA#" :
                                            
                                            CODE...
                                            
                                        break;
                                            
                                        case "@CLOSE_POLYLINE_VALUES#" :
                                               controladorMarcadores.leerCoordenadasLineaDesdeMapa(datos);
                                        break;
                                        .   
                                        .
                                        .
                                    }
                                }
                            }
                            Here I invoke the class ClientExecution
                            public class SocketCatcherClients implements Runnable   
                            {
                                 Panel_MarcadoresController controladorMarcadores;
                            
                                 public SocketCatcherClients(Panel_MarcadoresController contMarcadores) 
                                {
                                    this.controladorMarcadores = contMarcadores;
                                }
                            
                               public void run() 
                                {
                                   try
                                    {
                                      Runnable nuevoCliente = new ClientExecution(cliente, controladorMarcadores); //Here I tried to pass the object
                                      Thread hilo = new Thread(nuevoCliente); 
                                      hilo.start();
                                    }
                                    }catch (Exception e)
                                     {
                                         e.printStackTrace();
                                     }
                                 }
                            }
                            Finally, I made this change in Main class...
                            public class Main extends Application {
                                 public static void main(String[] args) {
                                    Application.launch(Main.class, (java.lang.String[])null);
                                 }
                            
                             public void start(Stage primaryStage) {
                                    try {
                                             CODE...
                                             FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Panel_Marcadores.fxml"));
                                             Panel_MarcadoresController controller = (Panel_MarcadoresController) fxmlLoader.getController();   //This object appears NULL
                                            (new Thread(new SocketCatcherClients(controller))).start();
                                        }
                                     catch (Exception ex) {
                                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                }
                            }
                            Debugging I found that the controller (Panel_MarcadoresController ) object is null
                            • 11. Re: I can't populate a TableView. NullPointerException in TableView
                              James_D
                              You need to call load() on your fxmlLoader before calling getController():
                              public class Main extends Application {
                                   public static void main(String[] args) {
                                      Application.launch(Main.class, (java.lang.String[])null);
                                   }
                               
                               public void start(Stage primaryStage) {
                                      try {
                                               CODE...
                                               FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Panel_Marcadores.fxml"));
                              
                                               // add here:
                                               AnchorPane root = (AnchorPane) fxmlLoader.load();
                                               // and presumably put root into a Scene, etc...
                              
                                               Panel_MarcadoresController controller = (Panel_MarcadoresController) fxmlLoader.getController();   //This object appears NULL
                                              (new Thread(new SocketCatcherClients(controller))).start();
                                          }
                                       catch (Exception ex) {
                                          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                      }
                                  }
                              }
                              • 12. Re: I can't populate a TableView. NullPointerException in TableView
                                biochemistry43
                                yes yes ... So if I have it. Here my complete start method
                                 public void start(Stage primaryStage) {
                                        try {
                                            AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("Estructura_General.fxml"));
                                            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Panel_Marcadores.fxml"));
                                            Scene scene = new Scene(page);
                                            primaryStage.setScene(scene);
                                            primaryStage.setTitle("SIGNA");
                                            primaryStage.initStyle(StageStyle.UNDECORATED);
                                            //primaryStage.setFullScreen(true);
                                            primaryStage.show();
                                            disableDividers(Estructura_GeneralController.splitPane1);
                                            disableDividers(Estructura_GeneralController.splitPane2);
                                            disableDividers(Estructura_GeneralController.splitPane3);
                                            disableDividers(Estructura_GeneralController.splitPane4);
                                            Panel_MarcadoresController controller = (Panel_MarcadoresController) fxmlLoader.getController();
                                            ClientSocket soc = new ClientSocket("@RESIZE#,11,137,1363,795");
                                           (new Thread(new SocketCatcherClients(controller))).start();
                                            
                                        } catch (Exception ex) {
                                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                    }
                                I have it as well as it..
                                • 13. Re: I can't populate a TableView. NullPointerException in TableView
                                  James_D
                                  You need to load the fxml file where the controller is defined; the fxmlLoader won't know about the controller until that happens.

                                  There are two fxml files in your code... what is the relationship between them?
                                  • 14. Re: I can't populate a TableView. NullPointerException in TableView
                                    biochemistry43
                                    Panel_Marcadores.fxml is included in Estructura_General.fxml

                                    <fx:define>
                                    <fx:include fx:id="marcadoresPanel" source="Panel_Marcadores.fxml" />
                                    </fx:define>
                                    //Next, put it considering what you tell me ... Maybe I misunderstood how to do
                                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Panel_Marcadores.fxml"));
                                    Edited by: biochemistry43 on 15/03/2013 02:11 PM
                                    1 2 Previous Next