good morning, I'm working on a code, make the connection, built the user class, and everything else, but at the time of executing it I have this error which is in the FXMLOADER, here I leave the code of controlador
:
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 <Usuario> data =FXCollections.observableArrayList();
@FXML TableView<Usuario> tablacliente;
@FXML TableColumn<Usuario, String> nombrescol;
@FXML TableColumn<Usuario,String > apellidoscol;
@FXML TableColumn<Usuario, 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 agregar;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
clienteid.setCellValueFactory(new PropertyValueFactory <Usuario, Integer>("id_cliente"));
nombrescol.setCellValueFactory(new PropertyValueFactory <Usuario, String>("nombres"));
apellidoscol.setCellValueFactory(new PropertyValueFactory <Usuario, String>("apellidos"));
agregar();
}
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 Usuario(
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();
}
}
code FXML
<?import application.Usuario.*?>
<?import application.Controlador.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controlador">
<children>
<AnchorPane layoutY="-11.0" prefHeight="527.0" prefWidth="855.0">
<children>
<TextField fx:id="nm" layoutX="175.0" layoutY="103.0" />
<TextField fx:id="ap" layoutX="175.0" layoutY="176.0" prefHeight="25.0" prefWidth="149.0" />
<Label layoutX="50.0" layoutY="107.0" prefHeight="17.0" prefWidth="107.0" text="NOMBRES" />
<Label layoutX="50.0" layoutY="180.0" text="APELLIDOS" />
<TableView layoutX="344.0" layoutY="54.0" prefHeight="397.0" prefWidth="488.0">
<columns>
<TableColumn fx:id="clienteid" prefWidth="181.0" text="ID" />
<TableColumn fx:id="nombrescol" prefWidth="140.0" text="NOMBRES" />
<TableColumn fx:id="apellidoscol" prefWidth="140.0" text="APELLIDOS" />
</columns>
</TableView>
<Button fx:id="agregar" layoutX="65.0" layoutY="264.0" mnemonicParsing="false" onAction="#agregar" prefHeight="25.0" prefWidth="149.0" text="AGREGAR" />
</children>
</AnchorPane>
</children>
</AnchorPane>
and the code of main
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
public class Main extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("ARGUS V1.0 ");
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("Datos.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root, Color.WHITE);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class code of usuario
package application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Usuario {
private StringProperty nombres;
private StringProperty apellidos;
private IntegerProperty id_cliente;
public Usuario ( String nombres, String apellidos, Integer id_cliente) {
this.nombres= new SimpleStringProperty (nombres);
this.apellidos= new SimpleStringProperty ( apellidos);
this.id_cliente=new SimpleIntegerProperty (id_cliente);
}
public String getNombres() {
return nombres.get();
}
public void setNombres(String nombres) {
this.nombres=new SimpleStringProperty (nombres);
}
public String getApellidos() {
return apellidos.get();
}
public void setApellidos(String apellidos) {
this.apellidos=new SimpleStringProperty ( apellidos);
}
public Integer getId_cliente() {
return id_cliente.get();
}
public void setid_cliente(Integer id_cliente) {
this.id_cliente=new SimpleIntegerProperty (id_cliente);
}
}
the errors
javafx.fxml.LoadException:
/C:/Users/Soporte/eclipse-workspace/ProyectoArgus/bin/application/Datos.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:19)
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)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.Controlador.agregar(Controlador.java:98)
at application.Controlador.initialize(Controlador.java:43)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
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.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:235)
at application.Main.start(Main.java:23)
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