Need Help: JAVA, getGraphics() function

Discussion in 'OT Technology' started by ndnxtc, Sep 29, 2004.

  1. ndnxtc

    ndnxtc New Member

    Joined:
    Apr 10, 2004
    Messages:
    3,208
    Likes Received:
    0
    Location:
    SE MI
    hey guys, im am just doing an exercise for my computer science class in which i have to make a frame and show a Multiplication Table but i am having problems with this getGraphics function and i have to use the draw function

    heres the code so far:

    package programmingex10_6;

    import javax.swing.*;
    import java.awt.*;

    /**
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2004</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    */

    public class ProgrammingEx10_6
    extends JFrame {

    ProgrammingEx10_6() {
    MultiplicationTable newTable = new MultiplicationTable();

    getContentPane().add(newTable);

    newTable.WriteString("Multiplication Table", 30, 30);
    newTable.DrawSquare(20, 50, 120, 150);

    int x = 30;
    int y = 40;
    for (int a = 1; a < 10; a++) {
    newTable.WriteString("" + a, x, y);
    x = x + 10;
    }

    }

    public static void main(String[] args) {
    ProgrammingEx10_6 frame = new ProgrammingEx10_6();

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    class MultiplicationTable
    extends JPanel {
    private Graphics g;

    public void WriteString(String a, int x, int y) {
    g = getGraphics();
    g.drawString(a, x, y);
    g.dispose();
    }

    public void DrawSquare(int x, int y, int x2, int y2) {
    g = getGraphics();
    g.drawRect(x, y, x2, y2);
    g.dispose();
    }
    }
    }

    and here is the errors that i am getting:

    java.lang.NullPointerException

    at programmingex10_6.ProgrammingEx10_6$MultiplicationTable.WriteString(ProgrammingEx10_6.java:49)

    at programmingex10_6.ProgrammingEx10_6.<init>(ProgrammingEx10_6.java:23)

    at programmingex10_6.ProgrammingEx10_6.main(ProgrammingEx10_6.java:36)

    Exception in thread "main"


    line 49: g.drawString(a, x, y);
    line 23: newTable.WriteString("Multiplication Table", 30, 30);
    line 36: ProgrammingEx10_6 frame = new ProgrammingEx10_6();


    please help, i tried all kinds of crap, and i am using Borland JBuilder X, the free one
     
  2. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    1. different class belongs to different files, I am sure you have already done so...

    2. Try to use the code tag next time

    3. Catch the exception and see what else it gives you...
     

Share This Page