0

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 の代わりにコンポーネントを追加した後、どうしたらよいか分からない非常に厄介な例外が発生します。また、問題はこのコンポーネントにあると確信しています。これを削除すると、すべてがうまくいくからです。

スタクトトレース:

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コード内の com.p.staticmessages.gui.StaticMessagesForm.(StaticMessagesForm.java:37)
の この行(ここには記載されていません) は、pack() の行です。

4

1 に答える 1

3

IMHOの問題はここにあります:

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

リストはモデルなしでは機能しません。このメソッドを削除します。

于 2013-02-11T07:26:56.883 に答える