我的任务是模拟超市中队列的演变方式,每当客户进入或存在商店时,我都必须使用计时器进行输出。在超市,我有3个队列,应该处理客户。在每个客户端,他们应该在我的GUI中输出到达时间和离开时间。我可以为每个队列使用相同的计时器吗? (他们不应该同时或同一时间开始输出)。我试图将计时器作为参数传递给事件类,但我得到一个空指针异常。请帮助。
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)
答案 0 :(得分:1)
Simulation.java文件的第229行导致NullPointerException。具体来说,它位于TimeClass内部类的actionPerformed(...)方法中。
转到那条线,你就会遇到问题。
我怀疑它是这3行之一:
timer1.stop();
timer2.stop();
timer3.stop();
其中一个计时器变量可能为空。
快速修复(尽管可能无法解决实际问题)是:
if (timer1 != null) {
timer1.stop();
}
以及其他两个计时器变量。