0

I keep getting a NullPointerException when I want to draw my polygon Pizza object. I get this error message:

Exception in thread "main" java.lang.NullPointerException
    at Pizza.<init>(Pizza.java:9)
    at PolyDemo$PolyDemoPanel.getRandShape(PolyDemo.java:91)
    at PolyDemo$PolyDemoPanel.<init>(PolyDemo.java:54)
    at PolyDemo.<init>(PolyDemo.java:19)
    at PolyDemo.main(PolyDemo.java:28)

I didn't have problems with circles and rectangles, why is this not working? Here is my Pizza class:

    import java.awt.*;

    public class Pizza extends Shape{
        private Polygon P;

        public Pizza(int x, int y) {
            super(x,y);
            P.xpoints = new int[]{x, x+100, x+200};
            P.ypoints = new int[]{y, y+100, y};
            P.npoints = 3;
        }

        @Override
        public void draw(Graphics g){
            g.setColor(Color.RED);
            g.drawPolygon(P);
        }
    }

Here is the driver:

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

    class PolyDemo extends JFrame {
        public PolyDemo() {
            getContentPane().add( new PolyDemoPanel());
            setSize( 300,300 );
            setVisible( true );
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        }
    public static void main( String args[] ) {
        PolyDemo myApp = new PolyDemo();
    }
    public class PolyDemoPanel extends JPanel {     
        Shape[] myShapes= new Shape[20];

        public PolyDemoPanel() {
            for( int i = 0; i < 20; i++ ) {
                myShapes[i] =  getRandShape();
            }
        }
        public void paint( Graphics g ) {
            super.paint(g); 

            for( int i = 0; i < myShapes.length; i++ ){
                myShapes[i].draw( g );  
            }   
        }
        public int getRandInt() {
            return ( (int) ( Math.random() * 200 ) );   
        }
        public Shape getRandShape() {
            Shape retVal = null;
            final int x = getRandInt();
            final int y = getRandInt();
                retVal = new Pizza(x, y);
                return retVal;
            }
        }   
    }
  • Note: Don't call the Polyingon `P`. Only type names should have a capital letter in the beginning (and constants are all-caps). Fields, local variables and methods should start with a lowercase letter. – RealSkeptic May 04 '15 at 07:24
  • You might like to take a look at [Working with Geometry](https://docs.oracle.com/javase/tutorial/2d/geometry/index.html) – MadProgrammer May 04 '15 at 07:25

2 Answers2

1

You declare the poligon but doesn't create the object. This way it is null when you use it in Pizza's constructor. You will need to create an instance before you use it in the constructor. Also P is a bad name for a variable

public Pizza(int x, int y) {
        super(x,y);
       //P is null here - add P=new Poligon()
        P.xpoints = new int[]{x, x+100, x+200};
        P.ypoints = new int[]{y, y+100, y};
        P.npoints = 3;
    }
Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

You did not initialize your Polygon field P. Try this:

public Pizza(int x, int y) {
    super(x,y);
    P = new Polygon();
    P.xpoints = new int[]{x, x+100, x+200};
    P.ypoints = new int[]{y, y+100, y};
    P.npoints = 3;
}
Luo
  • 445
  • 2
  • 10