0

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.

AlexZeCoder
  • 95
  • 1
  • 4
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joe C Nov 26 '18 at 21:24
  • 1
    Your own code is passing `null` to `repeatSquare`... I find it unlikely that tutorials do that as well, as that would mean the code of those tutorials doesn't work either, – Mark Rotteveel Nov 27 '18 at 19:49

2 Answers2

2

How to pass in graphics g

You don't.

Swing will pass the Graphics object to the painting method of the component when Swing determines the component needs to be painted.

So you need to override the paintComponent(...) method of a JPanel with your custom painting. Then you add the panel to the frame.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You will never create a Graphics object. This is AWT concern, not yours. To do customized drawings, you'll need to overwrite the paint (for JFrame, for example) or the paintComponent (for JPanel, for example) method and there you do what you want if the Graphics Object. Your code should look like something like this:

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;
    private int times;

    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! " );
        }
    }

    @Override
    public void paint( Graphics g ) {
        super.paint( g );
        repeatSquare( g, 600 / 2, 300 / 2, 100.0f, 0, times );
    }

    public void setTimes( int times ) {
        this.times = times;
    }

    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.setTimes( times );
        r.repaint();
    }
}
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50