title: Not allowing me to do each action more than once
body: <p>I am having an issue with my code where whenever I try to do something more than once, the program will crash on me. For example, if I login, then log out and then log back in for a second time, the program will crash. This is happening for other things that I want to do more than once such as switching to another scene, going back to home then trying to go to the other scene again.</p>
  
  <p>Will just post the code for the login part.</p>
  
  <p><strong>Main</strong></p>
  
  <pre><code>package libraryapp;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.prefs.Preferences;
  
  import javafx.application.Application;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.scene.control.Alert;
  import javafx.scene.control.Alert.AlertType;
  import javafx.scene.layout.AnchorPane;
  import javafx.scene.layout.BorderPane;
  import javafx.stage.Stage;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.Marshaller;
  import javax.xml.bind.Unmarshaller;
  import libraryapp.model.Book;
  import libraryapp.model.BookListWrapper;
  import libraryapp.view.BrowseController;
  import libraryapp.view.HomeOverviewController;
  import libraryapp.view.LoginController;
  import libraryapp.view.RootLayoutController;
  
  public class LibraryApp extends Application {
  
  private Stage primaryStage;
  private BorderPane rootLayout;
  /**
   * The data as an observable list of Books.
   */
  private ObservableList&lt;Book&gt; bookData = FXCollections.observableArrayList();
  
  /**
   * Constructor
   */
  public LibraryApp() {
      // Add some sample data
      bookData.add(new Book("Hans", "Muster"));
      bookData.add(new Book("Ruth", "Mueller"));
      bookData.add(new Book("Heinz", "Kurz"));
      bookData.add(new Book("Cornelia", "Meier"));
      bookData.add(new Book("Werner", "Meyer"));
      bookData.add(new Book("Lydia", "Kunz"));
      bookData.add(new Book("Anna", "Best"));
      bookData.add(new Book("Stefan", "Meier"));
      bookData.add(new Book("Martin", "Mueller"));
  }
  
  /**
   * Returns the data as an observable list of Books. 
   * @return
   */
  public ObservableList&lt;Book&gt; getBookData() {
      return bookData;
  }
  
  
  
  
  @Override
  public void start(Stage primaryStage) {
      this.primaryStage = primaryStage;
      this.primaryStage.setTitle("LibraryApp"); 
      initRootLayout();
       showLogin();
  }
  
  
  /**
  * Initializes the root layout and tries to load the last opened
  * person file.
   */
  public void initRootLayout() {
  try {
      // Load root layout from fxml file.
      FXMLLoader loader = new FXMLLoader();
          loader.setLocation(LibraryApp.class.getResource("view/RootLayout.fxml"));
      rootLayout = (BorderPane) loader.load();
  
      // Show the scene containing the root layout.
      Scene scene = new Scene(rootLayout);
      primaryStage.setScene(scene);
  
      // Give the controller access to the main app.
      RootLayoutController controller = loader.getController();
      controller.setLibraryApp(this);
  
      primaryStage.show();
  } catch (IOException e) {
      e.printStackTrace();
  }
  
  // Try to load last opened person file.
  File file = getBookFilePath();
  if (file != null) {
      loadBookDataFromFile(file);
  }
  }
  
  
  
  
  
  /**
   * Shows the home overview inside the root layout.
   */
  public void showHomeOverview() {
      try {
  
          FXMLLoader loader = new FXMLLoader();
          loader.setLocation(LibraryApp.class.getResource("view/HomeOverview.fxml"));
          AnchorPane homeOverview = (AnchorPane) loader.load();
  
  
  
          // Set home overview into the center of root layout.
          rootLayout.setCenter(homeOverview);
  
      // Give the controller access to the main app.
      HomeOverviewController controller = loader.getController();
      controller.setLibraryApp(this);
  
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public void showLogin() {
      try {
  
          FXMLLoader loader = new FXMLLoader();
          loader.setLocation(LibraryApp.class.getResource("view/Login.fxml"));
          AnchorPane login = (AnchorPane) loader.load();
  
  
  
          // Set home overview into the center of root layout.
          rootLayout.setCenter(login);
  
      // Give the controller access to the main app.
      LoginController controller = loader.getController();
      controller.setLibraryApp(this);
  
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public void showBrowse() {
  try {
  
      FXMLLoader loader = new FXMLLoader();
      loader.setLocation(LibraryApp.class.getResource("view/Browse.fxml"));
      AnchorPane browse = (AnchorPane) loader.load();
  
      // Set browse into the center of root layout.
      rootLayout.setCenter(browse);
  
      // Give the controller access to the main app.
      BrowseController controller = loader.getController();
      controller.setLibraryApp(this);
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  }
  
  
  /**
   * Returns the main stage.
   * @return
   */
  public Stage getPrimaryStage() {
      return primaryStage;
  }
  
  public static void main(String[] args) {
      Application.launch(args);
  }
  </code></pre>
  
  <p><strong>Login Controller</strong></p>
  
  <pre><code>package libraryapp.view;
  
  import java.io.File;
  import java.io.IOException;
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Label;
  import javafx.scene.control.PasswordField;
  import javafx.scene.control.TextField;
  import javafx.scene.layout.AnchorPane;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import libraryapp.LibraryApp;
  import org.w3c.dom.Document;
  import org.w3c.dom.NodeList;
  import org.xml.sax.SAXException;
  
  /**
  * FXML Controller class
  *
  * @author Michael
  */
  public class LoginController implements Initializable {
  
  /**
   * Initializes the controller class.
   */
  
      @FXML
  private AnchorPane loginPane;
  
  
  
  @FXML
  private Label lblMessage;  
  @FXML
  private TextField txtUsername;
  @FXML
  private PasswordField txtPassword;
  
  private LibraryApp libraryApp;
  
  
  public String getString(String tagName) throws SAXException, IOException,     ParserConfigurationException{
      File file = new File("src/libraryapp/account.xml");
      DocumentBuilderFactory docBuildFactory =   DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = docBuildFactory.newDocumentBuilder();
      Document document = docBuilder.parse(file);
  
      NodeList list = document.getElementsByTagName(tagName);
      if (list != null &amp;&amp; list.getLength() &gt; 0){
          NodeList subList = list.item(0).getChildNodes();
  
          if (subList != null &amp;&amp; subList.getLength() &gt; 0) {
              return subList.item(0).getNodeValue();
          }
      }
          return null;
     }
  
  @FXML
  private void login(ActionEvent event) throws IOException, ParserConfigurationException, SAXException {
  
  
       String email = getString("email");
  
       String password = getString("password");
  
  
      if (txtUsername.getText().equals(email) &amp;&amp; txtPassword.getText().equals(password)){
  
          libraryApp.showHomeOverview();
      } else{
          lblMessage.setText("Username or password invalid!");
      }
  
  
  }
  
      @FXML
  private void register(ActionEvent event) throws IOException {
  
     AnchorPane pane = FXMLLoader.load(getClass().getResource("Register.fxml"));
     loginPane.getChildren().setAll(pane);
  }
  
  @Override
  public void initialize(URL url, ResourceBundle rb) {
      // TODO
  }    
  
   public void setLibraryApp(LibraryApp libraryApp) {
      this.libraryApp = libraryApp;
  }
  
  }
  </code></pre>
  
  <p>And this is the error that I get</p>
  
  <pre><code>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)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
  at java.lang.Thread.run(Thread.java:745)
  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)
  ... 52 more
  Caused by: java.lang.NullPointerException
  at libraryapp.view.LoginController.login(LoginController.java:82)
  ... 62 more
  </code></pre>
  
  <p><strong>Logout</strong></p>
  
  <pre><code>@FXML
  public void logout(ActionEvent event) throws IOException {
  
          File bookFile = libraryApp.getBookFilePath();
          libraryApp.saveBookDataToFile(bookFile);
  
  
      AnchorPane pane = FXMLLoader.load(getClass().getResource("Login.fxml"));
      browsePane.getChildren().setAll(pane);
  }
  </code></pre>