Drawing an object in my canvas class, the object is created in a separate class
Hello everyone, I'm quite a noob at Java and this is my first post but I don't know where else to ask and I can't find problem specific solutions to this, I'm sure the answer is something really simple but like I said i'm rather new at Java.
basically Im making a program which prints figures onscreen after I press a button. the figures are branched from a class called JFigure I created. so basically this is what my code to achieve this looks like:
First up I have a class called StackClass which among other things declares a vector of JFigures and has the following function. JRect is another function with JFigure inheritance which draws a rectangle.
Code:
public boolean AddE(){
int n =0;
if(jTextField1 == null)
return false;
else{
stackFig[n]= new JRect();
n++;
valor = Integer.parseInt(jTextField1.getText());
stack.push(valor);
System.out.println("Added "+ value+" to the stack");
System.out.println(Figure's height: "+(int) stackFig[0].getDim().getHeight() + ".");
return true;
}
now in my Canvas class I have the following method to get the currently drawn figure and display onscreen:
Code:
public void paint (Graphics g){
s1= new StackClass();
figures= s1.getFig();
g.setColor(Color.blue);
// g.drawRect(2, 2, (int) dimP.getWidth( ) - 4, (int) dimP.getHeight( ) - 4);
// g.drawRect(4, 4, (int) dimP.getWidth( ) - 8, (int) dimP.getHeight( ) - 8);
figures.print(g);
repaint();
}
i've left uncommented the relevant parts of this function here.
so basically the error I get after I try to run this is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
which if I suspect right, comes from the fact that i'm trying to print the figure before it actually exists, however I'm not sure as to how to prevent this from happening, I'd appreciate any help! thanks!