Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to add new tab functionality to browser.I already search a lot on google as well as on stackoverflow but nothing work.I want to load main fxml file again by clicking on new tab button.

Below is Main.java file

Java
package tabcheck;
import javafx.application.Application;
//all imports..

public class Main extends Application{

public static void main(String[] args){
    launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
   try{
       Parent root = FXMLLoader.load(getClass().getResource("/tabcheck/First page.fxml"));
       Scene scene = new Scene(root);
       scene.getStylesheets().add("tabcheck/application.css");
       primaryStage.setScene(scene);
       primaryStage.show();
      } 
      catch(Exception e){
       e.printStackTrace();}
   }
}

Below is controller of First-page.fxml
Java
Home_controller.java
package tabcheck;
import java.io.IOException;
import java.net.URL;
//All imports...

public class Home_controller implements Initializable{
    @FXML private Button forward_btn;
    @FXML private Button refresh_btn;
    @FXML private Button go_btn;
    @FXML private TextField addressField;
    @FXML private WebView webView;
    @FXML private TabPane tabpane ;
    @FXML private BorderPane mainLayout;
    private Main main;
    private WebEngine webEngine;
      
     @Override
      public void initialize(URL url, ResourceBundle rb){
          webEngine = webView.getEngine();
          webEngine.load("http://www.google.com");
          webEngine.locationProperty().addListener(new ChangeListener<String>()        {
          @Override
          public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
              addressField.setText(newValue);
          }
         });
          addressField.setText("http://www.google.com");
        }

    public String fload(ActionEvent ae )
    {// code....
     }
     @FXML
     private void addTab() throws IOException{
         Tab tab = new Tab("New Tab");
         tabpane.getTabs().add(tab);
        // tabpane.getSelectionModel().clearSelection();
         FXMLLoader loader = new FXMLLoader();
         loader.setLocation(Main.class.getResource("/tabcheck/Tab-First-page.fxml"));
         AnchorPane tabAnchor = loader.load();
         tab.setContent(tabAnchor);
      }
}


In Controller class Tab-First-page.fxml is the copy of first-page.fxml and changes in Tab-first-page.fxml is that it does not contain Tabpane otherwise it is same.
I am able to load multiple tab from First-page.fxml but not able to open new tab from Tab-first-page.fxml that is, From main page multiple tabs can be open dynamically but from those open tabs I am not able to open new tabs. Following is the error I am getting...

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
**Caused by: java.lang.NullPointerException
at tabcheck.Home_controller.addTab(Home_controller.java:200)
... 62 more**
Home_controller.java:200 is tabpane.getTabs().add(tab); in addTab() method. I have tried every possible solution but not working,please any one can help?

What I have tried:

I have also try to do it by adding listener in addTab() but still null pointer exception is coming...also search on other websites but nothing is working...
Posted
Comments
Richard Deeming 17-Mar-16 15:55pm    
A NullPointerException means you're trying to call a method on a variable which hasn't been initialized. The most likely culprit is tabpane - none of the code you've shown initializes it.

Use the debugger. Set a break-point just before the line that throws the exception, step through the code, and examine each variable to find which one is null when you expect it to contain a value. Then go back and work out why it hasn't been initialized.
Member 12399879 17-Mar-16 16:16pm    
@Richard Demming.. I have already initialize it as--@FXML private TabPane tabpane ; in the controller class but still it throws null pointer exception.Please can you elaborate your solution.
Richard Deeming 18-Mar-16 8:20am    
That line DECLARES the variable. You haven't INITIALIZED it in the code you've shown.

And, as I said, YOU need to debug your code to find out what variable has not been initialized, and then work out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900