1

Im calling 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);
}

when some criteria are met. My problem is that, when I try to access getLSB by using gui.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);
    }

Error appears - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

How can I fix this?

NightEye
  • 352
  • 2
  • 19
  • Can you share the stack trace of the exception you're getting? – Mike Laren Apr 24 '15 at 22:29
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – copeg Apr 24 '15 at 22:34
  • My guess is something in that first line of getLSB is null, most likely the file. – lambgrp425 Apr 24 '15 at 22:37

3 Answers3

1

You are trying to call getLSB() in your DataComparison class, but you don't give it the reference of your SteganographyGUI class. So change the following line:

 DataComparison dataComp;
 dataComp = new DataComparison();

to:

DataComparison dataComp;
dataComp = new DataComparison(this);

And change the constructor as well:

public DataComparison(SteganographyGUI guiRef){
    gui = guiRef;
}
alainlompo
  • 4,130
  • 3
  • 27
  • 39
1

When you try to initiate static "gui" variable

gui = new SteganographyGUI();

you execute SteganographyGUI class constructor which (probably) calls DataComparison class constructor

public DataComparison(){

    lsb = gui.getLSB(); 

}

which uses static "gui" variable which is not set yet. This is the reason you got "java.lang.NullPointerException".

Yes, I know "the exception message is misleading" :)

Karol Król
  • 2,844
  • 1
  • 29
  • 36
0

You are doing cyclic calls in your code. Try another class just for the main and call its methods in order:

class 1: `public class SteganographyGUI {...}`

class 2: `public class DataComparison {...}

class 3:

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

I have helped :)
Alobes5
  • 383
  • 3
  • 8