簡體   English   中英

在Java隊列中的對象中使用方法

[英]Using methods in Objects in a Queue, Java

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)

有沒有人有辦法解決嗎? 謝謝!

斷言可以幫助調試此類問題。 如果希望隊列中的所有元素都為空,請考慮使用如下代碼:

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

如果將空對象從隊列中拉出(這是意外的),則程序將在斷言時死亡。 當有了斷言時,問題的原因通常會更加清楚。

您必須使用-ea來啟用斷言。 參見http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html

我終於解決了。 我在隊列中填充了一些空對象和一些非空對象,這使得實現起來更加困難。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM