多窗口的JavaFX问题

时间:2016-05-01 07:15:04

标签: java javafx

所以我一直用登录和sql数据库制作程序。 程序开始登录场景弹出,然后你按登录,然后可以显示主程序的新场景但错误弹出,这就是它不知道问题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
}
}
}

登录模型---------------------------------

 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();
     }
      }
    }

错误====================================

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任何人都知道为什么它不显示任何对我?以及如何解决它?

1 个答案:

答案 0 :(得分:0)

你得到NullPointerException。这意味着您调用方法的内容是null。在这种情况下,getResource()的结果为null。来自Class.getResource(String name)的文档:

  

<强>返回:
  如果找不到具有此名称的资源,则为URL对象或null

这表示找不到名称为view/view1.fxml的资源。

问题是您正在解析相对于该类的资源文件。同样,来自Class.getResource(String name)的文档:

  

在委托之前,使用此算法从给定的资源名称构造绝对资源名称:

     
      
  • 如果名称以/\u002f)开头,则资源的绝对名称是/后面的名称部分。
  •   
  • 否则,绝对名称的格式如下:

    modified_package_name/name
    
         

    其中modified_pa​​ckage_name是此对象的包名称,/替换为.\u002e)。

  •   

这意味着如果您有一个全名为ch.makery.address.view.LoginController且您使用资源名称view/test1.fxml的类,它实际上会尝试使用绝对名称/cm/makery/address/view/view/test1.fxml来解析资源,并且此资源不存在,该方法返回null,您将获得NullPointerException

相反,请使用以/开头的资源名称使路径成为绝对路径:

getClass().getResource("/view/test1.fxml");