Java 7中方法pack()中的NullPointerException

时间:2013-02-11 07:15:03

标签: java swing nullpointerexception

我使用NetBeans Swind应用程序创建。为了形成我添加了自己的JList:

public class JStaticMessagesList extends JList<StaticMessagePanel> implements StaticMessages<StaticMessagePanel> {

private JStaticMessagesList.MyListModel messages = new MyListModel();
private final StaticMessagesForm messgeForm;

public JStaticMessagesList(StaticMessagesForm messgeForm) {
    this.messgeForm = messgeForm;
}

public JStaticMessagesList(StaticMessagesForm messgeForm, StaticMessages<StaticMessage> staticMessages) {
    this(messgeForm);

    for (StaticMessage msg : staticMessages) {
        messages.addElement(new StaticMessagePanel(messgeForm, msg));
    }
}

@Override
public void add(StaticMessagePanel msg) {
    messages.addElement(msg);
}

@Override
public void deleteStaticMessage(StaticMessagePanel msg) {
    messages.removeElement(msg);
}

@Override
public Iterator<StaticMessagePanel> iterator() {
    return messages.iterator();
}

@Override
public ListModel<StaticMessagePanel> getModel() {
    return null;
}

@Override
public final void setModel(ListModel<StaticMessagePanel> model) {
    if (model instanceof MyListModel) {
        super.setModel(model);
    }
}

public class MyListModel extends DefaultListModel<StaticMessagePanel> implements Iterable<StaticMessagePanel> {

    @Override
    public Iterator<StaticMessagePanel> iterator() {
        return new MyModelIterator(this);
    }

    private class MyModelIterator implements Iterator<StaticMessagePanel> {

        private int idx = 0;
        private final MyListModel model;

        private MyModelIterator(MyListModel model) {
            this.model = model;
        }

        @Override
        public boolean hasNext() {
            return model.getSize() > idx + 1;
        }

        @Override
        public StaticMessagePanel next() {
            return model.get(idx++);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Removing with iterator is not supported!");
        }
    }
}
}

添加我的组件而不是普通的JList后,我得到了非常讨厌的异常,我不知道该怎么做。此外,我很确定问题是这个组件的问题,因为当我删除它时,一切都会好的。

Stacttrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at java.awt.Container.validateUnconditionally(Container.java:1660)
at java.awt.Window.pack(Window.java:817)
at com.p.staticmessages.gui.StaticMessagesForm.initComponents(StaticMessagesForm.java:319)
at com.p.staticmessages.gui.StaticMessagesForm.<init>(StaticMessagesForm.java:37)
at com.p.staticmessages.gui.StaticMessagesForm.<init>(StaticMessagesForm.java:27)
at com.p.staticmessages.gui.StaticMessagesForm$14.run(StaticMessagesForm.java:411)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at java.awt.Window.dispatchEventImpl(Window.java:2711)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

PS
这行 at com.p.staticmessages.gui.StaticMessagesForm。(StaticMessagesForm.java:37)代码(此处未列出)与pack()一致。

1 个答案:

答案 0 :(得分:3)

恕我直言的问题在这里:

@Override
public ListModel<StaticMessagePanel> getModel() {
    return null;
}

没有模型,列表无法工作。删除此方法。