0

Note: This is not a duplicate of questions like this, because those are trying to call instance methods on classes, and not instances. Mine is that I'm trying to call an instance method on an instance and it still gives the error.

I'm getting the classic error that happens when you try to call an instance method on a class. My problem is that I'm trying to call an instance method on an instance and I'm getting that error. My code is:

public class PixelsManipulation{

// Load in the image
private final BufferedImage img = getImage("strawberry.jpg");
Sequential sequentialGrayscaler = new Sequential(img, 2, 2);//img.getWidth(),img.getHeight());

public static void main(String[] args) {  

    // Sequential:
    long startTime = System.currentTimeMillis();

    sequentialGrayscaler.ConvertToGrayscale(); // error here
    // ... etc.
}

Why might this be happening? Is there something really obvious I've missed? I have declared an instance of Sequential called sequentialGrayscaler, and I'm trying to call .ConvertToGrayscale() on it, not the class itself.

The Sequential code is just:

public class Sequential {
private int width, height; // Image params
private BufferedImage img;

// SEQUENTIAL
// Load an image from file into the code so we can do things to it

Sequential(BufferedImage image, int imageWidth, int imageHeight){
    img = image;
    width = imageWidth;
    height = imageHeight;
}

public void ConvertToGrayscale(){
// etc.

EDIT: If I comment out the image and just instantiate the Sequential object with integer params, it works. So the problem must be something to do with the BufferedImage.

Here is the code I use to read in images:

private static BufferedImage getImage(String filename) {
    try {
        InputStream in = getClass().getResourceAsStream(filename); // now the error is here
        return ImageIO.read(in);
    } catch (IOException e) {
        System.out.println("The image was not loaded. Is it there? Is the filepath correct?");
        System.exit(1);
    }
    return null;
}

The last place I can "chase" the error to is the line where I create an InputStream. The error there is "non static method getClass() cannot be referenced from a static context". This is after making the Sequential declaration static along with the ConvertToGrayscale() method. This is after saying:

private static BufferedImage img = getImage("strawberry.jpg");
private static Sequential sequentialGrayscaler = new Sequential(img, 2, 2);

And making getImage() static as well (have to do that otherwise I get the error when I try and create the BufferedImage).

EDIT: Ultimately I just had to move my getImage() method out of my main class and into the Sequential class. Ideally I didn't want to do this since it probably means I'll have a lot of duplicate getImage() methods if I want to implement them in other classes, but at least it works for now.

Community
  • 1
  • 1
Touchdown
  • 494
  • 4
  • 19
  • Try this: `private static Sequential sequentialGrayscaler = new Sequential(img, 2, 2);` – user2004685 Jan 30 '16 at 19:49
  • That gets rid of the error in main(), but it just moves it to getImage() when I try and create an InputStream. – Touchdown Jan 30 '16 at 19:51
  • Refer this answer http://stackoverflow.com/questions/35094934/how-do-you-access-a-static-variable-of-an-object-in-java/35095535#35095535 – thedarkpassenger Jan 30 '16 at 19:55
  • That is because `private final BufferedImage img = getImage("strawberry.jpg");` is `final` and not `static`. You need to change that also to `static`. – user2004685 Jan 30 '16 at 19:56
  • Changing them both to static means I get an error in the getImage() method (which also had to be changed to static in order for the img variable to be valid), specifically "non static method getClass cannot be referenced from a static context". – Touchdown Jan 30 '16 at 19:59
  • @user2004685 That doesn't begin to make sense. `final` has nothing to do with it. – user207421 Jan 31 '16 at 01:20
  • @EJP Thanks for your response. Sorry for the confusion. – user2004685 Jan 31 '16 at 01:37

1 Answers1

1

It is because both img and sequentialGrayscaler object of PixelsManipulation Class are non-static. Change them to static.

private static BufferedImage img = getImage("strawberry.jpg");
private static Sequential sequentialGrayscaler = new Sequential(img, 2, 2);

Moreover, you can't do getClass().getResourceAsStream(...) from a static method. Use name of the class instead.

public class PixelsManipulation {

    /* Your Code Here */

    private static BufferedImage getImage(String filename) {
        try {
            /* Code Change */
            InputStream in = PixelsManipulation.class.getResourceAsStream(filename);
            return ImageIO.read(in);
        } catch (IOException e) {
            System.out.println("The image was not loaded. Is it there? Is the filepath correct?");
            System.exit(1);
        }
        return null;
    }
}

It should start work fine after you do these changes.

user2004685
  • 9,548
  • 5
  • 37
  • 54
  • Doing that gives the same error, but with an added "Accessing static method ConvertToGrayscale". Even if I change absolutely everything (all the variables and methods) to static, it still happens. In fact, I seem to get a "chain" where the error just shifts around until it ends up in the default getClass() method. – Touchdown Jan 30 '16 at 19:33
  • I followed what you said and moved my getImage() method out of my main class and into the Sequential class. It works now, but I don't really understand why it wouldn't work when it was in the main class. As another note: the Sequential class doesn't have to be final, but the sequentialGrayscaler object does have to be static. – Touchdown Jan 30 '16 at 20:10
  • It should work even if you keep it in your main class. – user2004685 Jan 30 '16 at 20:15