I am getting a nullpointerexeption when i try this code
package computerscience;
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JFrame;
public class Recursion extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public Recursion() {
setTitle("recursion");
setSize(600, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void repeatSquare(Graphics g, double x, double y, double size, int count, int times) {
if (count < times)
{
count++;
double doublex = x - size/2;
double doubley = y - size/2;
double scale = 2.2;
int intsize = (int) Math.round(size);
int intx = (int) Math.round(doublex);
int inty = (int) Math.round(doubley);
try {
g.drawRect(intx, inty, intsize, intsize);
// DrawSquare(null, x - size/2, y - size/2, size/scale, count, times);
// DrawSquare(null, x - size/2, y + size/2, size/scale, count, times);
// DrawSquare(null, x + size/2, y - size/2, size/scale, count, times);
// DrawSquare(null, x + size/2, y + size/2, size/scale, count, times);
}catch(Exception e){
System.out.println(e);
}
}
else
System.out.println("Program is finished! ");
}
public static void main(String[] agrs)
{
Recursion r = new Recursion();
Scanner myScanner = new Scanner(System.in);
System.out.println("How many times would you like to repeat the pattern: ");
int times = Integer.parseInt(myScanner.next());
r.repeatSquare( null, 600/2, 300/2, 100.0f, 0, times);
}
}
The problem is at the line where it says:
g.drawRect(intx, inty, insize, intsize);
i know that you cant call drawrect to a null object but every tutorial i watch passes in null so i dont know why i am getting this problem and any help would be appreciated. Thanks.