2013-02-11 49 views
0

我已經使用NetBeans Swind應用程序創建。爲了形成我加入我自己的JList:Java 7中的方法pack()中的NullPointerException

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
這條線在com.p.staticmessages.gui.StaticMessagesForm(StaticMessagesForm.java:37)在代碼(此處未列出)與包線( )。

回答

3

恕我直言,問題就在這裏:

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

列表不能沒有一個模型。刪除這個方法。

+0

哇!這麼簡單的解決非常感謝你。 (PS。我改變它返回消息。) – MAGx2 2013-02-11 07:29:20

相關問題