ArrayList导致NullPointerException

时间:2013-01-29 10:16:01

标签: java swing arraylist nullpointerexception

我从paintcomponent方法

获取此代码的nullpointer异常
  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!!");
            }
        }

MySnowFlake

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


    }
}

2 个答案:

答案 0 :(得分:2)

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

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

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

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

在生产软件中,通常最好将所有意外异常的堆栈跟踪保存到日志文件或数据库中,以便稍后进行事后分析。

答案 1 :(得分: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));