-3

我从paintcomponent方法中得到了这段代码的空指针异常

  public A1Panel() {
            final ArrayList<MySnowFlake> myShapes = new ArrayList<MySnowFlake>();   //Stage 1: Replace this line
            popup = new JPopupMenu();               //create the popup menu
            makePopupMenu();

            addMouseListener( new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }

            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
            public void mouseClicked( MouseEvent e ) {  //Stage 1: Modify the following statements
                myShapes.add(currentIndex, new MySnowFlake(e.getX(), e.getY(), currentLevel, currentLength));
                currentIndex += 1;
                repaint();
            }
            });
        }

        public void paintComponent(Graphics g) {
                //g.drawString("" + currentIndex, 50,50);
            //Stage 1: Modify the following statements
            try{
            for (int i = 0; i < currentIndex; i++) {
                myShapes.get(i).paint(g);
            }
            } catch(NullPointerException e){
                System.out.println("NullPointerException caught!!");
            }
        }

我的雪花

public class MySnowFlake {
    protected int level;            // the recursion level
    protected Turtle turtle;        // the turtle object
    protected double turn = Math.PI/3; // the turning angle
    public int length;              // the length of the snowflake
    public Point p;                 // the initial position


    /** Constructor to create a Snowflake and initialize all values
    */
    public MySnowFlake(int x, int y, int level, int length) {
        this.level = level;
        this.length = length;
        p = new Point(x,y);
        turtle = new Turtle();
    }

    public void translate(int dx, int dy){
        p.x += dx;
        p.y += dy;
    }

    public void LevelUp(){
        level++;
    }

    public void LevelDown(){
        level--;
    }


    /**
     * Recursive draw method
    * @param lev -  the level of the koch shape
    * @param size -  the size of the koch shape
    */
    public void draw(int lev, double size) {
        // 1) define the base case
        if(lev < 0){

        // 2) define the recursive case

    }

    /**
     * Paint a snowflake
    * @param g  the graphics control
    */

    public void paint(Graphics g) {
        double size = length;

        // replace this line with recursive calls
        g.drawRect(p.x, p.y, level*5, level*5);


        // 1) set up the turtle object first

        // 2) call the recursive draw method


    }
}
4

2 回答 2

2

当您使用 ArrayList 的实际长度时,循环会更可靠:

for (int i = 0; i < myShapes.size(); i++) {

但请注意,异常也可能发生在 MySnowFlake 类的 paint 方法中。当您捕获可能由许多不同问题(如 NullPointerException)引起的异常时,您应该始终打印它的堆栈跟踪,以便您可以看到实际抛出异常的位置:

        } catch(NullPointerException e){
            System.out.println("NullPointerException caught!!");
            e.printStackTrace();
        }

在生产软件中,将所有意外异常的堆栈跟踪保存到日志文件或数据库中通常是个好主意,这样您以后可以进行事后分析。

于 2013-01-29T10:17:55.793 回答
0

您能否更改这两行的顺序,即代替

myShapes.add(currentIndex, new MySnowFlake(e.getX(), e.getY(), currentLevel, currentLength));
currentIndex += 1;

尝试这个:

currentIndex += 1;
myShapes.add(currentIndex, new MySnowFlake(e.getX(), e.getY(), currentLevel, currentLength));
于 2013-01-29T11:48:51.373 回答