java练习赋值中的NullPointerException

时间:2013-06-07 14:36:00

标签: java debugging exception paintcomponent

他是java的初学者,我正在做一个需要画车的练习。我书中的代码不起作用,我不知道为什么。

我不是java的总菜鸟我知道类,对象,方法等但是如果它涉及到语法的确切行为我仍然很高兴。

这是代码:

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

我的书说它必须工作但是eclipse说:

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...

我不明白为什么!?有人知道代码抛出异常的原因吗?起初我没有看到日食中的任何错误,但是当我遇到麻烦时就开始了。我确实得到了一扇窗户,但没有车。

1 个答案:

答案 0 :(得分:8)

void的构造函数中删除Carpanel关键字,以便初始化car实例

public void Carpanel() {

应该是

public Carpanel() {