defaultListModel空指针错误

JList中通过defaultListCard插入列表,产生一个可供选择的列表,但是运行时某些时候会出现空指针错误。
代码如下:

 public void showCard(String[] cardsList) {
            cardListModel.clear();
            System.out.println(cardListModel.capacity());
            for (int i = 0; i < cardsList.length; i++) {
                String str = CardType.cardShowParse(cardsList[i]);
                System.out.println(str);
                cardListModel.addElement(str);
            }
        }

控制台输出:


20
4
4
4
5
Exception in thread "Thread-2" java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1368)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
    at javax.swing.plaf.basic.BasicListUI$Handler.valueChanged(BasicListUI.java:2623)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
    at javax.swing.DefaultListSelectionModel.insertIndexInterval(DefaultListSelectionModel.java:632)
    at javax.swing.plaf.basic.BasicListUI$Handler.intervalAdded(BasicListUI.java:2581)
    at javax.swing.AbstractListModel.fireIntervalAdded(AbstractListModel.java:148)
    at javax.swing.DefaultListModel.addElement(DefaultListModel.java:367)
    at douDiZhu.Table$SouthPanel.showCard(Table.java:279)
    at douDiZhu.Table.showCard(Table.java:186)
    at douDiZhu.Client$ReceiveThread.handle(Client.java:147)
    at douDiZhu.Client$ReceiveThread.receive(Client.java:129)
    at douDiZhu.Client$ReceiveThread.run(Client.java:115)
    at java.lang.Thread.run(Thread.java:745)

可见容量为20,所以应该不是容量不够,str也是有值的,错误信息指向defaultlistmodel,最终指向下面这个不知道干什么用的代码的
cellHeights[index] = cellSize.height;
这一行。

     /**
     * Recompute the value of cellHeight or cellHeights based
     * and cellWidth, based on the current font and the current
     * values of fixedCellWidth, fixedCellHeight, and prototypeCellValue.
     *
     * @see #maybeUpdateLayoutState
     */
    protected void updateLayoutState()
    {
        /* If both JList fixedCellWidth and fixedCellHeight have been
         * set, then initialize cellWidth and cellHeight, and set
         * cellHeights to null.
         */

        int fixedCellHeight = list.getFixedCellHeight();
        int fixedCellWidth = list.getFixedCellWidth();

        cellWidth = (fixedCellWidth != -1) ? fixedCellWidth : -1;

        if (fixedCellHeight != -1) {
            cellHeight = fixedCellHeight;
            cellHeights = null;
        }
        else {
            cellHeight = -1;
            cellHeights = new int[list.getModel().getSize()];
        }

        /* If either of  JList fixedCellWidth and fixedCellHeight haven't
         * been set, then initialize cellWidth and cellHeights by
         * scanning through the entire model.  Note: if the renderer is
         * null, we just set cellWidth and cellHeights[*] to zero,
         * if they're not set already.
         */

        if ((fixedCellWidth == -1) || (fixedCellHeight == -1)) {

            ListModel dataModel = list.getModel();
            int dataModelSize = dataModel.getSize();
            ListCellRenderer renderer = list.getCellRenderer();

            if (renderer != null) {
                for(int index = 0; index < dataModelSize; index++) {
                    Object value = dataModel.getElementAt(index);
                    Component c = renderer.getListCellRendererComponent(list, value, index, false, false);
                    rendererPane.add(c);
                    Dimension cellSize = c.getPreferredSize();
                    if (fixedCellWidth == -1) {
                        cellWidth = Math.max(cellSize.width, cellWidth);
                    }
                    if (fixedCellHeight == -1) {
                        cellHeights[index] = cellSize.height;
                    }
                }
            }
            else {
                if (cellWidth == -1) {
                    cellWidth = 0;
                }
                if (cellHeights == null) {
                    cellHeights = new int[dataModelSize];
                }
                for(int index = 0; index < dataModelSize; index++) {
                    cellHeights[index] = 0;
                }
            }
        }

        columnCount = 1;
        if (layoutOrientation != JList.VERTICAL) {
            updateHorizontalLayoutState(fixedCellWidth, fixedCellHeight);
        }
    }

这个错误有时出现有时不出现,我只在网上找到了这个http://www.tc5u.com/java/1862834.htm ,但是没有解决方法,stackoverflow上也没找到。

请问这个错误是什么原因,怎么解决,或者有没有替代方法。

http://www.java123.net/v/79691.html