Search code examples
javadebuggingexceptionpaintcomponent

NullPointerException in java exercise assignment


He I am a beginner in java and I am working on a exercise where I need to draw a car. The code in my book does not work and I don't know why.

I am not a total noob to java I know classes, objects, methods etc. but if it comes to the exact behavior of the syntax I am still nooby.

this is the code:

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

public class Vb1101 extends JFrame {
    public static void main( String args[] ){
        JFrame frame = new Vb1101();
        frame.setSize(350,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("exercise 1101 draw car parts");
        frame.setContentPane( new Carpanel() );
        frame.setVisible( true );
    }
}

class Carpanel extends JPanel {
    public Car car;

    public void Carpanel(){
        car = new Car( 20, 150, 80, 30 );
    }

    public void paintComponent( Graphics g ){
        super.paintComponent(g);
        setBackground(Color.WHITE);
        car.teken(g);
    }
}

 class Car {
    public ArrayList<Part> parts;

    public Car( int left, int under, int width, int height){
        parts = new ArrayList<Part>();
        int wielgrootte = 20;
        //int carosOnder = onder - wielgrootte / 2;

        //de carosserie
        parts.add( new Rectangle( Color.BLUE, left, under - 10, width, height ) );

        // De cabine
        parts.add( new Rectangle( Color.CYAN, left, under - 10 - height, 4 * width / 5, 4 * height / 5 ) );

        // het achterwiel
        parts.add( new Circle( Color.YELLOW, left + 5, under, wielgrootte) );

        // het voorwiel
        parts.add( new Circle( Color.YELLOW, left + width - 30, under, wielgrootte ) );
    }

    public void teken( Graphics g ){
        for(Part part : parts){
            part.teken( g );  //polymorfie says my book
        }
    }

    public interface Part {
        public void teken( Graphics g ); //abstract method says my book
    }

    public class Rectangle implements Part {
        public int left, under, width, height;
        public Color color;

        public Rectangle (Color color, int left, int under, int width, int height){
            this.color = color;
            this.left = left;
            this.under = under;
            this.width = width;
            this.height = height;
        }

        public void teken ( Graphics g ){
            g.setColor( color );
            g.fillRect(left, under - height, width, height);
            g.setColor(Color.black);
            g.drawRect(left, under - height, width, height);
        }
    }

    public class Circle implements Part {
        public int left, under, diameter;
        public Color color;

        public Circle( Color color, int left, int under, int diameter ){
            this.color = color;
            this.left = left;
            this.under = under;
            this.diameter = diameter;
        }

        public void teken( Graphics g ){
            g.setColor(color);
            g.fillOval(left, under - diameter, diameter, diameter);
            g.setColor(Color.black);
            g.drawOval(left, under - diameter, diameter, diameter);
        }
    }
}

My book says that it has to work but eclipse says:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at Carpanel.paintComponent(Vb1101.java:26)
  at javax.swing.JComponent.paint(Unknown Source)
  at javax.swing.JComponent.paintChildren(Unknown Source)
  at javax.swing.JComponent.paint(Unknown Source)
  at javax.swing.JLayeredPane.paint(Unknown Source)
  etc...

I don't get why!? Someone knows why the code throws an exception? At first I don't see any errors in eclipse but when I run the trouble starts. I do get a window but without a car.


Solution

  • Remove the void keyword from the constructor of Carpanel so that the car instance is initialized

    public void Carpanel() {
    

    should be

    public Carpanel() {