Java - Cannot make a static reference to the non-static method "something" from the type "something"

StackOverflow https://stackoverflow.com/questions/23116845

  •  04-07-2023
  •  | 
  •  

Question

so I went on and started creationg game as usual. Except this time I got an error O.o. I was trying to find an answer, but non of the ones I thought maybe here I can get a good Answer! Heres the error:

Description Resource Path Location Type Cannot make a static reference to the non-static field image Game.java /POGA/src/packagehere line 71 Java Problem

Description Resource Path Location Type Cannot make a static reference to the non-static method createBufferStrategy(int) from the type Canvas Game.java /POGA/src/packagehere line 66 Java Problem

I get more of these type, but I guess If you show me and viewers of this question how to fix these errors we would be able to fix the rest of the "same" ones...

package packagehere;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;

BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public static boolean running = false;
JFrame frame;
public static String title = "POGA game thingy - 0.1 Aplha";
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final Dimension gameDim = new Dimension(WIDTH, HEIGHT);

synchronized void start() {
    Thread thread = new Thread();
    thread.start();
    running = true;
}

public void run() {
    while(running) {
        tick();
        render();
    }
}

synchronized void stop() {
    running = false;
    System.exit(0);
}

public Game() {
    setMaximumSize(gameDim);
    setMinimumSize(gameDim);
    setPreferredSize(gameDim);

    frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(this, BorderLayout.CENTER);
    frame.pack();

    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    frame.requestFocus();
}

public static void tick() {

}

public static void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null) {
        bs = createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();

    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

    g.dispose();
    bs.show();
}
}
Was it helpful?

Solution

The problem is at below line

g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

Second on what object are you calling getWidth() and getHeight() methods?

I think you want to get the width and height of Canvas.

To solve this issue remove static from render() method.

OTHER TIPS

Where are getBufferStrategy() and createBufferStrategy() declared? (Answer: In the type Canvas which you are extending)

They are NON-Static methods, you cannot access them from within your render() method, because that method is static.

You can access Static methods from within Static and non-Static methods. You can access non-static methods from non-static methods only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top