如何解决javafx错误引发的异常

时间:2018-10-11 17:05:46

标签: javafx

你好,我试图在eclipse中生成一些代码,但是,当我运行该程序时,在主代码或FXML代码中的代码或对此的处理似乎是一个问题,我有控制器的类, Cliente类,这是主要代码,我将此代码放在此站点中,以获取一些帮助。

我认为这可能是FXML代码或主要问题。请在此处寻求帮助或帮助,

错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at application.Main.mostrarplantilla(Main.java:28)
    at application.Main.start(Main.java:20)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application application.Main

Controller

package application;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import javafx.scene.control.cell.PropertyValueFactory;



public class Controlador implements Initializable{

    ObservableList <Cliente> data =FXCollections.observableArrayList();
   @FXML TableView<Cliente>  tablacliente;
   @FXML TableColumn<Cliente, String> nombrescol;
   @FXML TableColumn<Cliente,String > apellidoscol;
   @FXML TableColumn<Cliente, Integer>  clienteid;
   @FXML private Button add;
   @FXML private Button show;
   PreparedStatement preparedStatement=null;
   ResultSet rs=null;
   Connection Conexion=null;
   @FXML  private TextField nm;
    @FXML private TextField ap;
    @FXML private Button btn;


@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    clienteid.setCellValueFactory(new PropertyValueFactory <Cliente, Integer>("id_cliente"));
    nombrescol.setCellValueFactory(new PropertyValueFactory <Cliente, String>("nombres"));
     apellidoscol.setCellValueFactory(new PropertyValueFactory <Cliente, String>("apellidos"));
    agregar();
}

// connect main class to controller

public void conexion(){

    String query="select * from cliente";
    try {
        Conexion=DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=prueba", "sa", "123");
        preparedStatement=Conexion.prepareStatement(query);
        rs=preparedStatement.executeQuery(query);

        while ( rs.next() ) 
        {
        data.add(new Cliente(
                rs.getString("nombre"),
                rs.getString("apellido"),
                rs.getInt("id")
                ));
        tablacliente.setItems(data);
        }

        preparedStatement.close();
        rs.close();

        } 

    catch (SQLException e) {
         e.printStackTrace();
     }
     if(Conexion!=null) {
         System.out.println("conexion exitosa");

     }

}

public void agregar() {
 String Nombre=nm.getText();
 String Apellido=ap.getText();
 String query="INSERT INTO CLIENTE (nombre,apellido)VALUES (?,?)";

 preparedStatement=null;

 try
 {
     preparedStatement=Conexion.prepareStatement(query);
     preparedStatement.setString(1, Nombre);
     preparedStatement.setString(2, Apellido);

 }
 catch (SQLException e) {
     e.printStackTrace();
 }
 finally {
     try {
        preparedStatement.execute(query);
    } catch (SQLException e) {

        e.printStackTrace();
    }
     try {
        preparedStatement.close();
    } catch (SQLException e) {

        e.printStackTrace();
    }
 }
   nm.clear();
   ap.clear();
   conexion();


}
}

Main

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;

import java.io.IOException;

public class Main extends Application {
    public static Stage stage;
    private static BorderPane Miplantilla;
    @Override
    public void start(Stage stage) throws IOException {

        Main.stage=stage;
        Main.stage.setTitle("ARGUS V1.0");
        stage.setMaximized(true);
        Main.mostrarplantilla();

         }


    private static void mostrarplantilla() throws IOException{
        FXMLLoader loader=new FXMLLoader();
        loader.setLocation(Main.class.getResource("/Datos.fxml"));
        Miplantilla=loader.load();
        Scene scene=new Scene(Miplantilla);
        stage.setScene(scene);
        stage.show();

    }


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


    }
}

FXML代码

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

<?import application.Cliente.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="505.0" prefWidth="1060.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controlador">
   <children>
      <Pane layoutX="-70.0" layoutY="-162.0" prefHeight="200.0" prefWidth="200.0" />
      <TableView fx:id="tablacliente" layoutX="387.0" layoutY="53.0" prefHeight="402.0" prefWidth="615.0">
        <columns>
          <TableColumn fx:id="nombrescol" prefWidth="170.0" text="NOMBRES" />
          <TableColumn fx:id="apellidoscol" prefWidth="207.0" text="APELLIDOS" />
            <TableColumn fx:id="clienteid" prefWidth="237.0" text="ID" />
        </columns>
      </TableView>
      <Label layoutX="56.0" layoutY="117.0" text="NOMBRES" />
      <Label layoutX="56.0" layoutY="179.0" text="APELLIDOS" />
      <TextField fx:id="nm" layoutX="130.0" layoutY="113.0" />
      <TextField fx:id="ap" layoutX="130.0" layoutY="184.0" />
      <Button fx:id="btn" layoutX="130.0" layoutY="261.0" mnemonicParsing="false" onAction="#agregar" prefHeight="25.0" prefWidth="139.0" text="AGREGAR" />
   </children>
</AnchorPane>

0 个答案:

没有答案