-1

i have class named sagum which has a method name paint

public void paint(Graphics g) {

    int[] apw1 = null;
    int n1 = 0;
    for (int j = 1; j <= n1; j++) {
        g.drawRect(50, 50, (apw1[j] * 20), 30);
        String[] p = null;
        g.drawString("p" + p[j - 1], (55 + (apw1[j - 1] * 20)), 70);
        g.drawString("" + apw1[j - 1], 50 + (apw1[j - 1] * 20), 100);
    }
    g.drawString("" + apw1[n1], 50 + (apw1[n1] * 20), 100);

}

im trying to call it

n1=count+1; 
paint(g);

and im getting an error

non-static method paint(Graphics) cannot be referenced from a static context

im simply trying to call it after a method is executed to do an a graphical app of my method (scheduling algorithm).

please help

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Paolo Sofio
  • 9
  • 1
  • 5
  • i dont have any static context – Paolo Sofio Sep 17 '13 at 16:46
  • Is method that contains your code `n1=count+1; paint(g);` static? – Pshemo Sep 17 '13 at 16:48
  • 1
    Where is the code that is calling `paint(g)`? That's your static context. Note that if you do manage to call your `paint` method, it's not going to do anything you want. The loop body is not going to execute at all because `n1` is 0 and `j` starts at 1, but the last line will generate a `NullPointerException` because `apw1` is `null`. – Ted Hopp Sep 17 '13 at 16:49
  • Instead of just writing `paint(g)`, write `this.paint(g)`, which is what it's implicitly calling. Then it should be more obvious where your problem is. You should ask yourself, what is `this`? Where is the object? For example, if you're in main, you'll realize, "Oh! I never created an object of this type, did I?" – Ben Sep 17 '13 at 17:30

2 Answers2

1

You shouldn't be calling the paint(...) method directly, so your error, while important to understand (please see this link), is a non-issue. You need to restructure your program so that you don't feel that you have the need to make this call. Most Swing and AWT applications work best by doing "passive" drawing by having you change class properties and then calling repaint() on a rendered component so that the JVM is suggested to repaint it.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

This compiler error means that you are trying to invoke an instance method, but you haven't specified an instance.

You need an instance of your sagum class, and invoke the paint() method on it, like this:

sagum s = new sagum();
...
s.paint(g);
...

If you invoke an instance method from another instance method in the same instance, you can explicitly use this to indicate the instance, or you can leave it out altogether and this will be implied.

However, as others have indicated, in this specific case, you shouldn't be painting directly. Write your program so that Swing (or AWT or JavaFX) manages the painting schedule.

Community
  • 1
  • 1
erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    No, please don't advise this. This is a Swing application and he should not be calling the paint method directly. He needs to re-structure his program so that it uses passive rendering (except in very rare situations where he absolutely needs active rendering). – Hovercraft Full Of Eels Sep 17 '13 at 16:49
  • Well, that's true in this context. I was trying to show him generally how instance methods work. – erickson Sep 17 '13 at 17:10