0

Here is the method I wrote, it gives an error when I call the method that the object g is null and break. Please help me fix it, thank you. Base on my school, I must use Applet.

public void drawing(Graphics g,int a,int b,int c){ //method
    double d,e,f,x,y,m;
    d = (double) a;
    e = (double) b;
    f = (double) c;
    if (e>f){
        m=e;
        e=f;
        f=m;
    }

I have try to initialize the object g... but I can't really do that..

    ex=(e*e-f*f+d*d)/2/d;
    //calculate the point required
    ey=(Math.pow((d+e+f)*(d+e-f)*(d-e+f)*(e+f-d),0.5))/2/d;
    //drawing the line
    g.drawLine(0,0,(int) ex,(int) ey);    //this is the main problem
}

I have try to initialize the object g... but I can't really do that..

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Please refer the teacher to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) **For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/).** – Andrew Thompson Apr 08 '16 at 02:10
  • *"`g` is null"* `@Override` the `paint(Graphics g)` method and pass that `g` to the `drawing(g, ..)` method. When overriding a paint method, immediately call the `super.paint(g);` method. It is important to understand that AWT & Swing containers paint when told to do so by the underlying JRE. – Andrew Thompson Apr 08 '16 at 02:14

1 Answers1

0

If you call the method drawing() from the paint() method and your paint method is defined in this way

public void paint(Graphics g){
    ...
}

then in the function call statement, pass the Graphics object g to the drawing() method which is an argument of the paint() method.

public void paint(Graphics g){
    ....
    drawing(g, int, int, int);
}

Since the Graphics object g in paint is not null, the same object passed to drawing() method should solve your problem.