我正在呼叫DataComparison()

public class SteganographyGUI {

    ...

    DataComparison dataComp;
    dataComp = new DataComparison();

}

public int getLSB(){
    String x = fileChooser1.getSelectedFile().getAbsolutePath();
    x = x.substring(x.length() - 10, x.length() - 9);
    return Integer.parseInt(x);
}


当满足某些条件时。我的问题是,当我尝试使用gui.getLSB()访问getLSB时

public class DataComparison {

    public static SteganographyGUI gui;

    ...


    public DataComparison(){

        lsb = gui.getLSB();

    }
    public static void main(String[] args) {
        gui = new SteganographyGUI();
        gui.setVisible(true);
    }


出现错误-Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

我怎样才能解决这个问题?

最佳答案

您试图在DataComparison类中调用getLSB(),但是没有给它提供SteganographyGUI类的引用。因此,更改以下行:

 DataComparison dataComp;
 dataComp = new DataComparison();


至:

DataComparison dataComp;
dataComp = new DataComparison(this);


并更改构造函数:

public DataComparison(SteganographyGUI guiRef){
    gui = guiRef;
}

07-28 00:23