0

I'm trying to create a browser. I have an add button and when it's clicked it should create a new tab. The button works when it is clicked on the first initial tab when the program just opens. The newly created tab takes the whole contents of the initial tab so the same button is created in the new tab. The problem is I can't create a new tab when I click on the button that is on the newly created tab, only the button on the first tab works.

    @FXML public void addNewTab(ActionEvent event) {

    Tab newTab = new Tab("Untitled " + (tabPane.getTabs().size() + 1));
    try {

        tabPane.getTabs().add(newTab);
        tabPane.getSelectionModel().select(newTab);
        AnchorPane anchorPane = FXMLLoader.load(getClass().getResource("new-tab.fxml"));
        newTab.setContent(anchorPane);

    } catch(Exception e) {
        e.printStackTrace();
        System.out.println(e);
    }

}

This is the FXML file of the program and initial first tab:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mybrowser.MyBrowserFXMLController">
   <children>
      <TabPane fx:id="tabPane" layoutY="28.0" prefHeight="691.0" prefWidth="1200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab fx:id="tab1" text="Untitled">
               <content>
                  <AnchorPane prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Pane prefHeight="50.0" prefWidth="1200.0" style="-fx-border-width: 0px 0px 1px 0px; -fx-border-color: #FEDC01;">
                           <children>
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="12.0" mnemonicParsing="false" onAction="#addNewTab" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/add.png" />
                                       </image>
                                    </ImageView>
                                 </graphic>
                                 <padding>
                                    <Insets left="-10.0" right="-10.0" />
                                 </padding></Button>
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="40.0" layoutY="12.0" mnemonicParsing="false" onAction="#pagePrevious" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/back.png" />
                                       </image>
                                    </ImageView>
                                 </graphic>
                                 <padding>
                                    <Insets left="-12.0" right="-10.0" />
                                 </padding></Button>
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="70.0" layoutY="12.0" mnemonicParsing="false" onAction="#pageNext" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <padding>
                                    <Insets left="-10.0" right="-12.0" />
                                 </padding>
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/forward.png" />
                                       </image>
                                    </ImageView>
                                 </graphic></Button>
                              <TextField fx:id="addressBar" layoutX="110.0" layoutY="13.0" onKeyPressed="#enterURL" prefHeight="27.0" prefWidth="1050.0" promptText="Search or enter the website name" />
                              <Button alignment="CENTER" contentDisplay="CENTER" layoutX="1163.0" layoutY="13.0" mnemonicParsing="false" onAction="#pageRefresh" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
                                 <graphic>
                                    <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                                       <image>
                                          <Image url="@../images/refresh.png" />
                                       </image>
                                    </ImageView>
                                 </graphic>
                                 <padding>
                                    <Insets left="-12.0" right="-10.0" />
                                 </padding>
                              </Button>
                           </children>
                        </Pane>
                        <WebView fx:id="webView" layoutY="50.0" prefHeight="609.0" prefWidth="1200.0" />
                     </children>
                  </AnchorPane>
               </content>
          </Tab>
        </tabs>
      </TabPane>
      <MenuBar prefHeight="29.0" prefWidth="1200.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="New Tab" />
                  <MenuItem mnemonicParsing="false" text="Close Tab" />
                  <MenuItem mnemonicParsing="false" text="Close All Tabs" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="New Window" />
                  <MenuItem mnemonicParsing="false" text="Close Window" />
                  <MenuItem mnemonicParsing="false" text="Close All Windows" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Close Browser" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
                  <MenuItem mnemonicParsing="false" text="Select All" />
              <MenuItem mnemonicParsing="false" text="Copy" />
                  <MenuItem mnemonicParsing="false" text="Paste" />
                  <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="View">
            <items>
              <MenuItem mnemonicParsing="false" text="Zoom In" />
                  <MenuItem mnemonicParsing="false" text="Zoom Out" />
            </items>
          </Menu>
            <Menu mnemonicParsing="false" text="History">
              <items>
                <MenuItem mnemonicParsing="false" onAction="#historyFull" text="Show Full History" />
                  <Menu mnemonicParsing="false" text="Show Recent History...">
                    <items>
                      <MenuItem mnemonicParsing="false" />
                    </items>
                  </Menu>
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Delete History..." />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
               <items>
                  <MenuItem mnemonicParsing="false" text="Find..." />
                  <MenuItem mnemonicParsing="false" text="About" />
               </items>
            </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>

This is the FXML for the new tab ("new-tab.fxml"):

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>

<AnchorPane prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mybrowser.MyBrowserFXMLController" >
   <children>
      <Pane prefHeight="50.0" prefWidth="1200.0" style="-fx-border-width: 0px 0px 1px 0px; -fx-border-color: #FEDC01;">
         <children>
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="12.0" mnemonicParsing="false" onAction="#addNewTab" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/add.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-10.0" right="-10.0" />
               </padding>
            </Button>
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="40.0" layoutY="12.0" mnemonicParsing="false" onAction="#pagePrevious" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/back.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-12.0" right="-10.0" />
               </padding>
            </Button>
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="70.0" layoutY="12.0" mnemonicParsing="false" onAction="#pageNext" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/forward.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-10.0" right="-12.0" />
               </padding>
            </Button>
            <TextField fx:id="addressBar" layoutX="110.0" layoutY="13.0" onKeyPressed="#enterURL" prefHeight="27.0" prefWidth="1050.0" promptText="Search or enter the website name" />
            <Button alignment="CENTER" contentDisplay="CENTER" layoutX="1163.0" layoutY="13.0" mnemonicParsing="false" onAction="#pageRefresh" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
               <graphic>
                  <ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/refresh.png" />
                     </image>
                  </ImageView>
               </graphic>
               <padding>
                  <Insets left="-12.0" right="-10.0" />
               </padding>
            </Button>
         </children>
      </Pane>
      <WebView fx:id="webView" layoutY="50.0" prefHeight="609.0" prefWidth="1200.0" />
   </children>
</AnchorPane>

As you can see, it is only the contents of the first tab. This FXML file has the same controller as the last FXML file which includes the "base" of the whole GUI. The buttons in this FXML have also been assigned to the correct ActionEvent in the controller. But I keep on getting this error with the problem mentioned above:

Executing/Users/Reno/Desktop/MyBrowser/dist/run1010542926/MyBrowser.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 49 more
Caused by: java.lang.NullPointerException
at mybrowser.MyBrowserFXMLController.addNewTab(MyBrowserFXMLController.java:55)
... 59 more
  • `Caused by: java.lang.NullPointerException at mybrowser.MyBrowserFXMLController.addNewTab(MyBrowserFXMLController.java:55)` – SedJ601 Aug 09 '17 at 04:16
  • @SedrickJefferson I noticed that but I don't know how to fix it –  Aug 09 '17 at 04:30
  • Search "what's a null pointer exception and how to fix it." – SedJ601 Aug 09 '17 at 04:44
  • @SedrickJefferson but I did initialize the variable tab `Tab newTab = new Tab("Untitled " + (tabPane.getTabs().size() + 1));`. –  Aug 09 '17 at 05:00
  • Do you have `@FXML TabPane tabPane`? – SedJ601 Aug 09 '17 at 05:24
  • @SedrickJefferson yes I do –  Aug 09 '17 at 05:29
  • I could be wrong, but I don't think you can have two `FXML` files set to one `Controller` in this manner. – SedJ601 Aug 09 '17 at 06:11
  • @SedrickJefferson I just created a new controller for the second FXML file but it's still showing the same error –  Aug 09 '17 at 06:57
  • @SedrickJefferson I think because I loaded a separate FXML file for the new tab's content, although it made to look the same, the buttons in this FXML file doesn't recognise that it is inside a tab which is inside a tabPane. I know this because I made a `System.out.println(tabPane.getTabs().size())`. When I click on a button in the first tab, it prints that there is 1 tab in the tabPane because it loaded with the tabPane in the same FXML file. And when I do the same with a newly added tab. It shows 0 even though there should be two (first + itself) in the tabPane since the content is a... –  Aug 09 '17 at 09:28
  • @SedrickJefferson ...separate FXML file. How can I make the content (the separate FXML file) know the existence of the other nodes in the first/base FXML file? –  Aug 09 '17 at 09:30
  • 1
    `tabPane` is defined in the main FXML file, not in the FXML file that creates the new tab. So it is not initialized in the controller for the new tab. Hence you get a null pointer exception. – James_D Aug 09 '17 at 12:02

0 Answers0