xjj314 2016-11-16 07:28 采纳率: 0%
浏览 1039

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上也没找到。

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

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2017-02-22 19:48
    关注
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集