1

Soo i had been making program with login and with sql database . the Programs begins login scene pops out and then u press login and there sould be shown new scene with main program but error pops out and thats it dont know weres the problem rlly

Main.java-------------------------------------

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
   Parent root = FXMLLoader.load(getClass().getResource("view/Login.fxml"));
   Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
   primaryStage.setScene(scene);
   primaryStage.show();
  } catch(Exception e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  launch(args);
 }
}

LoginControler.java--------------------------

public class LoginController implements Initializable {
public LoginModel loginModel = new LoginModel();

   @FXML
   private Label isConnected;
   @FXML
   private TextField txtUsername;
   @FXML
   private TextField txtPassword;

 @Override
 public void initialize(URL location, ResourceBundle resources) {
  // TODO Auto-generated method stub
  if (loginModel.isDbConnected()) {
   isConnected.setText("Connected");
  } else {
   isConnected.setText("Not Connected");
  }
 }
 public void Login(ActionEvent event){
     try {
        if (loginModel.isLogin(txtUsername.getText(), txtPassword.getText())){
            isConnected.setText("username and password is correct");
            ((Node)event.getSource()).getScene().getWindow().hide();
            Stage primaryStage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane root = loader.load(getClass().getResource("view/test1.fxml").openStream());
            CompanyMainController companyMainController = (CompanyMainController)loader.getController();
            companyMainController.GetUser(txtUsername.getText());
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
} else {
    isConnected.setText("username and password is not correct");
}
} catch (SQLException e) {
isConnected.setText("username and password is not correct");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

mainprogram controler------------------------------------

public class CompanyMainController implements Initializable {
@FXML
private Label userLbl;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}

public void GetUser(String user) {
// TODO Auto-generated method stub
userLbl.setText(user);
}

public void SignOut(ActionEvent event) {
 try {
 ((Node)event.getSource()).getScene().getWindow().hide();
 Stage primaryStage = new Stage();
 FXMLLoader loader = new FXMLLoader();
 Pane root = loader.load(getClass().getResource("/application/Login.fxml").openStream());

   Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  primaryStage.setScene(scene);
  primaryStage.show();
  } catch (Exception e) {
 // TODO: handle exception
}
}
}

login model---------------------------------

 public class LoginModel {
  Connection conection;
  public LoginModel () {
   conection = SqliteConnection.Connector();

  //*******************************if not conected all    closes************************************
    if (conection == null) {

    System.out.println("connection not successful");
    System.exit(1);}
   }

  public boolean isDbConnected() {
   try {
  return !conection.isClosed();
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return false;
 }
}
  public boolean isLogin(String user, String pass) throws SQLException {
       PreparedStatement preparedStatement = null;
       ResultSet resultSet = null;
       // soo this one is searcing in db employee  section for username and password
       String query = "select * from employee where username = ? and password = ?";
       try {
        preparedStatement = conection.prepareStatement(query);
        preparedStatement.setString(1, user);
        preparedStatement.setString(2, pass);

        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
         return true;
        }
        else {
         return false;
        }

     } catch (Exception e) {
        return false;
      // TODO: handle exception
     } finally {
      preparedStatement.close();
      resultSet.close();
     }
      }
    }

error====================================

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:8411)
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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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:497)
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:497)
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 ch.makery.address.view.LoginController.Login(LoginController.java:46)
... 62 more

soo anyone know why it dont show anyting for me? and how to fix it ???

  • 2
    "at ch.makery.address.view.LoginController.Login(LoginController.java:46)" - Which is line 46? Also, see [this](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). – Itai May 01 '16 at 07:48
  • this one Pane root = loader.load(getClass().getResource("view/test1.fxml").openStream()); – Deive Relas May 01 '16 at 08:42
  • Then it is probably that you have the resource name wrong. Double check that "view/test1.fxml" indeed exists and is in this location and name. – Itai May 01 '16 at 08:48
  • well nothing wrong with it it certainely is correct "view/test1.fxml" because it is in "view" folder and fxml named "test1.fxml" – Deive Relas May 01 '16 at 09:18
  • The problem might lie in the way your project is structured. It would help if you can add the project structure to your question. – ItachiUchiha May 01 '16 at 11:48

1 Answers1

0

You are getting a NullPointerException. This means that something you are calling a method on is null. In this case, the result of getResource() is null. From the documentation of Class.getResource(String name):

Returns:
A URL object or null if no resource with this name is found

This indicates that the resource with the name view/view1.fxml could not be found.

The problem is that you are resolving a resource file relative to the class. Again, from the documentation of Class.getResource(String name):

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a / (\u002f), then the absolute name of the resource is the portion of the name following the /.
  • Otherwise, the absolute name is of the following form:

    modified_package_name/name
    

    Where the modified_package_name is the package name of this object with / substituted for . (\u002e).

This means that if you had a class with the full name of ch.makery.address.view.LoginController and you used the resource name view/test1.fxml, it would actually try to resolve a resource with the absolute name /cm/makery/address/view/view/test1.fxml, and this resource does not exist, the method returns null and you get a NullPointerException.

Instead, use a resource name starting with a / to make the path absolute:

getClass().getResource("/view/test1.fxml");
randers
  • 5,031
  • 5
  • 37
  • 64