在队列中的对象中使用方法,Java

在队列中的对象中使用方法,Java,java,queue,Java,Queue,有人有解决办法吗? 谢谢 我终于解决了。我在队列中填充了一些空对象和一些非空对象,这使得实现起来更加困难。断言可以帮助调试此类问题。如果您期望队列中的所有元素都为空,那么考虑使用这样的代码: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at projekt.Modell.körSaker(Modell.java:110) at projekt.Controller$CloseListe

有人有解决办法吗?
谢谢

我终于解决了。我在队列中填充了一些空对象和一些非空对象,这使得实现起来更加困难。

断言可以帮助调试此类问题。如果您期望队列中的所有元素都为空,那么考虑使用这样的代码:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at projekt.Modell.körSaker(Modell.java:110) at projekt.Controller$CloseListener.actionPerformed(Controller.java:84) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.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) 如果将空对象从队列中拉出(这是意外的),则程序将在断言中终止。当断言就位时,问题的原因往往更清楚


您必须使用-ea来启用断言。请参见

您粘贴的代码甚至没有编译。什么是
对象
。Controller.java的第84行是什么。显然,我没有添加完整的代码。java的第84行调用包含上述代码的方法。java:110是object=obj.runTasks(int-somethingElse)。我只是想我会让它比包含我的变量更容易阅读。 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at projekt.Modell.körSaker(Modell.java:110) at projekt.Controller$CloseListener.actionPerformed(Controller.java:84) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.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)
obj=queue.remove();
assert(obj != null);
object=obj.runTasks(int somethingElse);