Java空指针异常java fx

时间:2016-02-07 11:03:29

标签: java opencv javafx nullpointerexception

我写了一个代码,它在JavaFX8中给出了一个Java空指针异常。有两个类:一个用于处理图像Mat,另一个用于显示处理过的图像,位于不同的窗口中。代码是:

public class ImagineComprimata
{

public Mat imComp;

@FXML
private ImageView imagineCompresata;

@FXML
private Button bSalveaza;
@FXML
private TextArea campRC;
@FXML
private TextArea campEMP;

@FXML
public void salveaza(ActionEvent eveniment)
{
 //this will be edited later
}

public void arata() throws IOException
{
    Stage scena = new Stage();
    FXMLLoader fl = new FXMLLoader(getClass().getResource("/fxml/ImagineComprimata.fxml"));
    AnchorPane p = (AnchorPane) fl.load();
    Scene s = new Scene(p, 742, 495);
    scena.setScene(s);
    scena.showAndWait();
}

public void setMat(Mat imagine)
{
    this.imComp = imagine;
}

public void setImagineCompresata(Image imagine)
{
    this.imagineCompresata.setImage(imagine);
}

}

现在在主窗口中,我将调用此类以在屏幕上显示:

public class Compresia
{

private Mat imagine;
private int parametru;

@FXML
public Button bSelecteaza;
@FXML
public Button bComprima;
@FXML
public Button bIntrodu;

@FXML
private ImageView foto;

@FXML
private TextField valoarea;

private String denumireFotografie;

private Matrix[] desc;

@FXML
public TextArea campRang;

private int rang;

@FXML
public void comprima(ActionEvent eveniment) throws InterruptedException, IOException
{
    if (imagine == null)
    {
        Notificare.informeaza("Notificare", "Nu ai selectat o imagine!");
    }
    else
    {
        double[][] mc = AlgoritmDeCompresie.comprimaImaginea(desc,   Util.extragereValori(imagine), parametru);
        Mat im = Util.construiesteImagine(mc);
        ImagineComprimata ic = new ImagineComprimata();
        ic.setMat(im);
        ic.setImagineCompresata(Util.conversieMat2Image(im));
        ic.arata();
    }

}

@FXML
public void cautaFisier(ActionEvent eveniment) throws InterruptedException
{
    Matrix[] desc;
    Mat im;
    String denumire;
    FileChooser f = new FileChooser();
    File fotoSelectat = f.showOpenDialog(null);
    if (fotoSelectat != null)
    {
        denumire = fotoSelectat.getPath();
        im = Highgui.imread(denumire);
        Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2GRAY);
        this.desc = AlgoritmDeCompresie.descompune(Util.extragereValori(im));//////
        rang = Util.returneazaRangul(Util.extragereValori(im));
        imagine = im;
        campRang.setText(Integer.toString(rang));
        foto.setImage(Util.conversieMat2Image(im));
    }
}

@FXML
public void seteazaParametru(ActionEvent eveniment)
{
    int p = Integer.parseInt(this.valoarea.getText());
    if (p >= this.rang)
    {
        Notificare.informeaza("Notificare", "Introdu un parametru mai mic   decat rangul!");
        this.valoarea.setText(null);
    }
    else
    {
        this.parametru = p;
        Notificare.informeaza("Notificare", "parametru setat :" + p);
    }
    ///////////////
}

public void start() throws Exception
{
    Stage scena = new Stage();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Compresia.fxml"));
    AnchorPane b = (AnchorPane) loader.load();
    Scene s = new Scene(b, 955, 586);
    scena.setScene(s);
    scena.setTitle("Compresie");
    scena.setResizable(false);
    scena.show();
}

public Matrix[] getDescompunere()
{
    return this.desc;
}

public void setDescompunere(Matrix[] descompunere)
{
    this.desc = descompunere;
}
}

它在setImageCompressed(im)上给出了一个错误,它跟踪到setImageCompressed(Mat im)。 ImageCompressed是JavaFX控制器,显示处理的图像。我哪里做错了?错误报告如下所示:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown  Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
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)
... 49 more
Caused by: java.lang.NullPointerException
at   interfata.ImagineComprimata.setImagineCompresata(ImagineComprimata.java:56)
at interfata.Compresia.comprima(Compresia.java:66)
... 58 more

1 个答案:

答案 0 :(得分:0)

可能是ImagineComprimata类的“私有ImageView imagCompresata”实例没有初始化并且仍然指向null,因此当你调用this.imagineCompresata.setImage(想象)时会出现NullPointerException提高。