我的任務是模擬超級市場中隊列的演變過程,每當客戶端進入或存在商店時,我都必須使用計時器來輸出。在超市裏我有3個應該處理顧客的隊列。在每個客戶端,他們應該在我的GUI中輸出到達時間和離開時間。我可以爲每個隊列使用相同的計時器嗎? (他們不應該同時開始輸出或在相同的時間內開始輸出)。我試圖通過計時器作爲事件類中的參數,但我得到一個空指針異常。請幫助。Java隊列計時器
public void simulationPrep (Queue q,JPanel p,int time,Timer t, int selection)
{
TimeClass tc;
int queueCapacity=0;
float queueProcTime=0;
float tempSTMin, float tempSTMax;
tempSTMin=getServiceTimeMin();
tempSTMax=getServiceTimeMax();
queueCapacity = 10 + (int)(Math.random()*20);
queueProcTime = tempSTMin + (float)(Math.random()*tempSTMax);
q1.setCapacity(queueCapacity);
q1.setProcessingTime(queueProcTime);
tc = new TimeClass((int)(queueProcTime/queueCapacity),p,selection);
t = new Timer(time, tc);
t.start();
}
public void simulate()
{
if(getSelection()>=1)
{
simulationPrep(q1,p21,1000,timer1,1);
if(getSelection()>=2)
{
simulationPrep(q2,p22,1500,timer2,2);
if(getSelection()==3)
{
simulationPrep(q3,p23,1700,timer3,3);
}
}
}
}
public class TimeClass implements ActionListener
{
int counter;
JPanel p;
int selection;
public TimeClass(int counter,JPanel p,int selection)
{
this.counter = counter;
this.p = p;
this.selection = selection;
}
public void actionPerformed(ActionEvent e)
{
counter--;
if(counter>=1)
{
p.add(new JLabel("Hi "+counter));
p.updateUI();
}
if(counter<1)
{
p.add(new JLabel("Done"));
p.updateUI();
timer1.stop();
timer2.stop();
timer3.stop();
}
}
}
如果你不明白代碼請問,我會更具體,但這是我的第一篇文章,我對Java很陌生。
這是我得到的輸出窗口:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at simulation.Simulation$TimeClass.actionPerformed(Simulation.java:229)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at simulation.Simulation$TimeClass.actionPerformed(Simulation.java:229)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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)
如果在涉及異常的問題中包含異常堆棧跟蹤,您將獲得更多幫助。 – 2012-04-02 11:05:03
這就是我得到的: – 2012-04-02 11:06:30
'timer1','timer2'和'timer3'聲明在哪裏? – Jim 2012-04-02 11:34:37