tecnocrata tecnocrata -4 years ago 297
Java Question

springboot + hibernate + javaFX

im stuck in this for a few days...not getting anywhere.
Here's the deal..im building an app that uses springboot (mainly o use crudeRepository) with javafx and hibernate.

but i keeping geting this 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: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(Unknown Source)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 52 more
Caused by: java.lang.NullPointerException
at br.jus.trt9.PjeMonitor.view.MonitorPjeController.mouseEvent(MonitorPjeController.java:153)
... 62 more


this happens couse the crudrepository clientRepo is null, dont know why...in normal execution it should have somethinh like this:

org.springframework.data.jpa.repository.support.SimpleJpaRepository@5a0a3243

anybody, know why ? thank you in advance.

ClientRepo.java
imports ...

/*TODO essa classe deve implementar o CrudRepository
que vem do spring-DATA-core
*/
@Repository
public interface ClientRepo extends CrudRepository<Client, Integer> {

}


MainApp.java

package br.jus.trt9.PjeMonitor;

imports
...

/*
* @AutoConfigurationPackage
*
* @ComponentScan(basePackages = "br.jus.trt9.PjeMonitor")
*
* @EnableAutoConfiguration
*/

//FIXME esse warning na inicialização
/* Unable to proxy method [public final void br.jus.trt9.PjeMonitor.dao.Ge
nericDao.setClazz(java.lang.Class)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.*/
@SpringBootApplication
@ComponentScan(basePackages = "br.jus.trt9.PjeMonitor")
public class MainApp extends Application {

private Stage primaryStage;
private BorderPane rootLayout;

private ObservableList<Audiencia> audData = FXCollections.observableArrayList();
private ConfigurableApplicationContext springContext;
private static String[] args;

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

@Override
public void init() throws Exception {
// applicationContext.ge3tAutowireCapableBeanFactory().autowireBean(this);


}

@Override
public void start(Stage primaryStage) {

// primaryStage.setScene(new Scene(rootLayout));
// primaryStage.show();

this.primaryStage = primaryStage;
this.primaryStage.setTitle("Pje Midias Monitor");

initRootLayout();

showTableAudiencia();

}

public void stop() throws Exception {
springContext.close();
}

public MainApp() {

audData.add(new Audiencia("12123123213213", LocalDate.now(), "sim"));
audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
audData.add(new Audiencia("55555555555555", LocalDate.now(), "sim"));
audData.add(new Audiencia("6666666666666", LocalDate.now(), "nao"));
audData.add(new Audiencia("7777777777777", LocalDate.now(), "nao"));
audData.add(new Audiencia("8888888888888", LocalDate.now(), "sim"));
audData.add(new Audiencia("99999999999999", LocalDate.now(), "nao"));
audData.add(new Audiencia("100010110101001", LocalDate.now(), "nao"));

}

/**
* Initializes the root layout.
*/

public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();

// problema do classLoader do javaFX
loader.setClassLoader(this.getClass().getClassLoader());

// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();

} catch (IOException e) {
e.printStackTrace();
}
}

public void showTableAudiencia() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/visao.fxml"));
AnchorPane TableOverview = (AnchorPane) loader.load();

//loader.setControllerFactory(springContext::getBean);

// Set person overview into the center of root layout.
rootLayout.setCenter(TableOverview);

// Give the controller acckess to the main app.
MonitorPjeController controller = loader.getController();
controller.setMainApp(this);
System.out.println("carregou main app");

} catch (IOException e) {
e.printStackTrace();
}
}

public void showCadastroClients() {

try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/clientcadastro.fxml"));
AnchorPane page = (AnchorPane) loader.load();

// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Cadastro de máquinas");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public Stage getPrimaryStage() {
return primaryStage;
}

public ObservableList<Audiencia> getAudData() {
return audData;
}

public void setAudData(ObservableList<Audiencia> audData) {
this.audData = audData;
}

}


MonitorPjeController.java

package br.jus.trt9.PjeMonitor.view;

import ...;

public class MonitorPjeController {

@FXML
private TableView<Audiencia> audTable;

@FXML
private TableColumn<Audiencia, String> audId;

@FXML
private TableColumn<Audiencia, LocalDate> dataId;

@FXML
private TableColumn<Audiencia, String> assId;

@FXML
private ComboBox<Client> clientcomboBox;

@FXML
private Button BtnListaId = new Button();

@FXML
private Button BtnListarMaq;

private ObservableList<Client> myClientComboBoxData = FXCollections.observableArrayList();

// referencia a aplicacao principal
private MainApp mainapp;

//FIXME
@Autowired
ClientRepo clientRepo;



public MonitorPjeController() {

}

@FXML
private void initialize() {
audId.setCellValueFactory(cellData -> cellData.getValue().processoProperty());
dataId.setCellValueFactory(cellData -> cellData.getValue().dataProperty());
assId.setCellValueFactory(cellData -> cellData.getValue().assinadoProperty());

Client client = new Client("teste123", "21 vt");

// sets the combobox options
myClientComboBoxData.add(client);
myClientComboBoxData.add(new Client("cta122223", "10 vt"));
myClientComboBoxData.add(new Client("cta122223", "17 vt"));
myClientComboBoxData.add(new Client("cta120337", "18 vt"));

clientcomboBox.setItems(myClientComboBoxData);

clientcomboBox.setCellFactory((comboBox) -> {
return new ListCell<Client>() {
@Override
protected void updateItem(Client item, boolean empty) {
super.updateItem(item, empty);

if (item == null || empty) {
setText(null);
} else {
setText(item.getVara());
}
}
};
});

clientcomboBox.setConverter(new StringConverter<Client>() {

@Override
public String toString(Client maquina) {
if (maquina == null) {
return null;
} else {
return maquina.getVara();
}
}

@Override
public Client fromString(String string) {
// TODO Auto-generated method stub
return null;
}
});

}

public void setMainApp(MainApp mainApp) {
this.mainapp = mainApp;

audTable.setItems(mainApp.getAudData());

}


// FIXME nao salva
// TODO em caso de erro enviar msg de erro
@FXML
public void mouseEvent() {
Client client = new Client(1, "cta 12344", "18 vt");
clientRepo.save(client);

}

public ObservableList<Client> getMyClientComboBoxData() {
return myClientComboBoxData;
}

public void setMyClientComboBoxData(ObservableList<Client> myClientComboBoxData) {
this.myClientComboBoxData = myClientComboBoxData;
}

}


as pointed by @minus, i created an springBoot EntryPoint, but the application starts and exits...i never see the javaFX view.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Lazy;


@SpringBootApplication
public class LaunchClass {

public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);

}

}

Answer Source

I finally did it! Following the tip that @jewelsea gave, I studied the framework created by Felix Roske. It took a little bit of refactoring of code, mainly in the controller part.

App.java imports ...

@SpringBootApplication
public class App extends AbstractJavaFxApplicationSupport {

    /**
     * Note that this is configured in application.properties
     */
    @Value("${app.ui.title:PJe Midia Monitor}")//
    private String windowTitle;



    public static void main(String[] args) {

        launchApp(App.class, VisaoView.class, args);
    }

}

2- the view class

imports ....


@FXMLView(value = "view/visao.fxml")
public class VisaoView extends AbstractFxmlView {

}

3- the controller

imports ....

@FXMLController
public class MonitorPjeController {

    @FXML
    private TableView<Audiencia> audTable;

    @FXML
    private TableColumn<Audiencia, String> audId;

    @FXML
    private TableColumn<Audiencia, LocalDate> dataId;

    @FXML
    private TableColumn<Audiencia, String> assId;

    @FXML
    private ComboBox<Client> clientcomboBox;

    @FXML
    private Button BtnListaId = new Button();

    @FXML
    private Button BtnListarMaq;

    @FXML
    private ObservableList<Client> myClientComboBoxData = FXCollections.observableArrayList();

    @FXML
    private ObservableList<Audiencia> audData = FXCollections.observableArrayList();

    @Autowired
    private ClienteRepo clienteRepo;

    @FXML
    private void initialize() {

        populaTabela();

        Client client = new Client("teste123", "21 vt");

        // sets the combobox options
        myClientComboBoxData.add(client);
        myClientComboBoxData.add(new Client("cta122223", "10 vt"));
        myClientComboBoxData.add(new Client("cta122223", "17 vt"));
        myClientComboBoxData.add(new Client("cta120337", "18 vt"));

        clientcomboBox.setItems(myClientComboBoxData);

        clientcomboBox.setCellFactory((comboBox) -> {
            return new ListCell<Client>() {
                @Override
                protected void updateItem(Client item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {
                        setText(null);
                    } else {
                        setText(item.getVara());
                    }
                }
            };
        });

        clientcomboBox.setConverter(new StringConverter<Client>() {

            @Override
            public String toString(Client maquina) {
                if (maquina == null) {
                    return null;
                } else {
                    return maquina.getVara();
                }
            }

            @Override
            public Client fromString(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });

    }


    public void populaTabela() {
        audData.add(new Audiencia("12123123213213", LocalDate.now(), "sim"));
        audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
        audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
        audData.add(new Audiencia("55555555555555", LocalDate.now(), "sim"));
        audData.add(new Audiencia("6666666666666", LocalDate.now(), "nao"));
        audData.add(new Audiencia("7777777777777", LocalDate.now(), "nao"));
        audData.add(new Audiencia("8888888888888", LocalDate.now(), "sim"));
        audData.add(new Audiencia("99999999999999", LocalDate.now(), "nao"));
        audData.add(new Audiencia("100010110101001", LocalDate.now(), "nao"));


        audTable.setItems(getAudData());

        audId.setCellValueFactory(cellData -> cellData.getValue().processoProperty());
        dataId.setCellValueFactory(cellData -> cellData.getValue().dataProperty());
        assId.setCellValueFactory(cellData -> cellData.getValue().assinadoProperty());
    }

    public ObservableList<Audiencia> getAudData() {
        return audData;
    }

    public void setAudData(ObservableList<Audiencia> audData) {
        this.audData = audData;
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download