我一直在应用程序中获取此NPE,但似乎无法摆脱它,因为它没有显示在我的任何源代码中。从堆栈跟踪中可以看出,它不是发生在我的代码中,而是发生在Swing平台中。你们中有人遇到过这个问题,也许想知道这里发生了什么?

   11:28:23,273 [AWT-EventQueue-0] ERROR [is.althingi.styran.utlit.styran.StyranImpl]
   - uncaughtException
   java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicTableUI$Handler.setValueIsAdjusting(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

我设法解决了这个问题!

问题是我在ListSelectionListener中添加了JTable;然后在我的侦听器的valueChanged方法中,依次调用scrollRectToVisibleupdateUI,这将导致异常。

我所做的就是在invokeLater调用周围添加updateUI,没有更多异常!

像这样:

  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      updateUI();
    }
  });


现在,我对问题有了“答案”(尽管我不确定我是否仍然理解invokeLater为何起作用?),我认为我的问题还不够好。我没有提供有关我的问题的足够信息,任何人都无法回答该问题。在问之前是否应该对这个问题进行更多的研究?我不知道。

提供足以帮助我的解决方案可能就足够了!

09-10 15:26