Jlist - 具有观察者模式的ClearSelection

时间:2013-10-14 15:04:16

标签: java swing nullpointerexception observer-pattern jlist

这是我的第一篇文章,我在我的应用程序中遇到了问题,我是用Java编程的。

我需要实现观察者模式,因为我有一个主窗口,我需要添加元素。当我添加元素时,应该更新主窗口。我把工具和Observable和Observer扩展到正确的类和主窗口中,我也覆盖了更新方法。但是当更新执行时,它会抛出异常。

以下是代码和例外:

@Override
public void update(Observable o, Object arg) {

    // Update Hotels
    this.popularHoteles(unDepartamento.getListaHoteles());

}
private void popularHoteles(ArrayList<Hotel> Hoteles) {

    listaHoteles.removeAll();

    if (fechasCorrectas()) {
        listaHoteles.setListData(Hoteles.toArray());
    }
}

其中&#34; listaHoteles&#34;是来自我的一个Jlist项目。

当我执行setListDadta时,我会回复这个例子:

  

线程中的异常&#34; AWT-EventQueue-0&#34;显示java.lang.NullPointerException     在   diaztaranto.interfaces.MenuPrincipal.listaHotelesValueChanged(MenuPrincipal.java:937)     在   diaztaranto.interfaces.MenuPrincipal.access $ 800(MenuPrincipal.java:20)     在   diaztaranto.interfaces.MenuPrincipal $ 9.valueChanged(MenuPrincipal.java:345)     在javax.swing.JList.fireSelectionValueChanged(JList.java:1798)at   javax.swing.JList中的$ ListSelectionHandler.valueChanged(JList.java:1812)     在   javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)     在   javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)     在   javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)     在   javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)     在   javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)     在   javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576)     在   javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420)     在javax.swing.JList.clearSelection(JList.java:2045)at   diaztaranto.interfaces.MenuPrincipal.popularHoteles(MenuPrincipal.java:998)     在   diaztaranto.interfaces.MenuPrincipal.update(MenuPrincipal.java:1043)     在java.util.Observable.notifyObservers(Observable.java:159)at   java.util.Observable.notifyObservers(Observable.java:115)at   diaztaranto.dominio.Hotel.agregarComentario(Hotel.java:212)at   diaztaranto.interfaces.Evaluacion.botonEvaluarActionPerformed(Evaluacion.java:135)     at diaztaranto.interfaces.Evaluacion.access $ 000(Evaluacion.java:6)     在   diaztaranto.interfaces.Evaluacion $ 1.actionPerformed(Evaluacion.java:56)     在   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)     在   javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2341)     在   javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在   javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     在java.awt.Component.processMouseEvent(Component.java:6505)at   javax.swing.JComponent.processMouseEvent(JComponent.java:3321)at   java.awt.Component.processEvent(Component.java:6270)at   java.awt.Container.processEvent(Container.java:2229)at   java.awt.Component.dispatchEventImpl(Component.java:4861)at   java.awt.Container.dispatchEventImpl(Container.java:2287)at   java.awt.Component.dispatchEvent(Component.java:4687)at   java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)     在java.awt.Container.dispatchEventImpl(Container.java:2273)at   java.awt.Window.dispatchEventImpl(Window.java:2719)at   java.awt.Component.dispatchEvent(Component.java:4687)at   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)at at   java.awt.EventQueue.access $ 200(EventQueue.java:103)at   java.awt.EventQueue $ 3.run(EventQueue.java:688)at   java.awt.EventQueue $ 3.run(EventQueue.java:686)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)     在java.awt.EventQueue $ 4.run(EventQueue.java:702)at   java.awt.EventQueue $ 4.run(EventQueue.java:700)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:699)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

任何人都知道为什么会这样?

谢谢你的建议!

0 个答案:

没有答案