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

时间:2010-12-15 17:42:26

标签: java queue

object = new Object(int something);
for(int i=0;i<5;i++) {
    //obj=queue.element();
    obj=queue.remove();
    object=obj.runTasks(int somethingElse);
    queue.offer(obj);
    //queue.remove();
}

我基本上是尝试在队列中首先使用对象的方法,然后将它放在队列的后面,然后使用下一个对象,并将其放在队列中等等。

object=obj.runTasks(int...)导致程序终止,并发出很多警告,例如“线程中的异常”AWT-EventQueue-0“。

还有这些:

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)

有没有人有解决方案? 谢谢!

2 个答案:

答案 0 :(得分:1)

断言有助于调试此类问题。如果您希望队列中的所有元素都为null,那么请考虑使用如下代码:

obj=queue.remove();
assert(obj != null);
object=obj.runTasks(int somethingElse);

如果将空对象从队列中拉出(这是意外的),程序将在断言上死亡。当断言到位时,问题的原因通常会更清楚。

您必须使用-ea运行才能启用断言。见http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html

答案 1 :(得分:0)

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