JavaFX,JasperReports和JRDataSource

时间:2017-04-04 19:40:37

标签: java javafx actionevent invocationtargetexception

我正在使用此应用程序,以便使用JRDataSource接口生成报告。我想我做得很好,但是很明显我错过了什么。我正在尝试做一个GUI,我可以在其中引入4个参数,通过在列表中捕获事件(按钮onClick)来保存它们,并使用另一个按钮onClick将其打印在使用JRDataSource的报告中。我的猜测是与onClick第二个事件(打印报告)相关的东西,因为直到那里应用程序运行良好。

您可以看到我的代码以及NetBeans在此处抛出的异常。

英语不是我的母语,儿子我很抱歉,如果我没有很好地解释它。

主类:

package di_t4;

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

public class DI_T4 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLFichaAlumno.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

来自FXMLController的活动:

@FXML
    private void guardar(ActionEvent event) {

        //Botón correspondiente al botón guardar
        String dniVal = txtDNI.getText();
        String modVal = cmbModulo.getValue();
        int notaVal = spnNota.getValue();
        double recuperaVal = spnRecuperacio.getValue();

        if (dniVal.isEmpty()) {
            Alert alerta = new Alert(Alert.AlertType.ERROR);
            alerta.setTitle("ALERT DNI FAIL");
            alerta.setContentText("El DNI que ha introducido no es correcto");
            alerta.showAndWait();
            return;
        }
        Nota nota = new Nota(dniVal, modVal, notaVal, recuperaVal);
        tbNotas.getItems().add(nota);
        listaNota.addNota(nota);
//        notaAlmacenada.insertarNota(nota);
        //limpiamos variables
        txtDNI.setText("");
        cmbModulo.setValue("DI");
        spnNota.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 10, 5));
        spnRecuperacio.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 5, 0, 0.5));
    }

    @FXML
    private void generaInforme(ActionEvent event) {
        try {

            JasperReport reporte = (JasperReport) JRLoader.loadObjectFromFile("InformeNotaAlumnos.jasper");
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null, listaNota);

            JasperViewer jviewer = new JasperViewer(jasperPrint);
            jviewer.setVisible(true);
        } catch (JRException jrex) {
            System.out.println(jrex.getCause());
        }
    }

我试过把它放在initialize方法中,但它不会改变结果:

btnGuardar.setOnAction(this::guardar);
btnMostrarInforme.setOnAction(this::generaInforme);

这是JRDataSource类:

public class NotaDatasource implements JRDataSource {

    private List<Nota> listaNota = new ArrayList<Nota>();
    private int indiceNotaActual = -1;

    @Override
    public boolean next() throws JRException {
        return ++indiceNotaActual < listaNota.size();
    }

    @Override
    public Object getFieldValue(JRField jrf) throws JRException {
        Object valor = null;

        if ("dni".equals(jrf.getName())){
            valor=listaNota.get(indiceNotaActual).getNota();
        }
        else if ("asignatura".equals(jrf.getName())){
            valor=listaNota.get(indiceNotaActual).getAsignatura();
        }
        else if ("nota".equals(jrf.getName())){
            valor=listaNota.get(indiceNotaActual).getNota();
        }
        else if ("notaRecuperacion".equals(jrf.getName())){
            valor=listaNota.get(indiceNotaActual).getNotaRecuperacion();
        }

        return valor;
    }

    public void addNota(Nota nota) {
        this.listaNota.add(nota);
    }
}

这是输出异常:

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.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.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(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:498)
    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:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:81)
    at di_t4.FXMLFichaAlumnoController.generaInforme(FXMLFichaAlumnoController.java:125)
    ... 58 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 60 more

在输出中的某个时刻,它说:

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:81)
        at di_t4.FXMLFichaAlumnoController.generaInforme(FXMLFichaAlumnoController.java:125)

它指的是来自FXMLController类的这一行,但我不认为这是真正的问题:

JasperReport reporte = (JasperReport) JRLoader.loadObjectFromFile("InformeNotaAlumnos.jasper");

对于这个looooong帖子感到抱歉,如果你能帮助我,我会非常高兴。

谢谢!

0 个答案:

没有答案