2012-04-02 3 views
0

대기열이 슈퍼마켓에서 어떻게 전개되는지 시뮬레이션 할 작업이 있으며 클라이언트가 들어가거나 저장소가있을 때마다 타이머를 사용하여 출력해야합니다. 슈퍼마켓에는 고객을 처리해야하는 대기열이 3 개 있습니다. 각 클라이언트에서 내 GUI에 도착 시간과 이탈 시간을 출력해야합니다. 각 큐에 동일한 타이머를 사용할 수 있습니까? (이들은 동시에 또는 동일한 시간 동안 출력을 시작해서는 안됨). 이벤트 클래스에서 매개 변수로 타이머를 전달하려고했지만 Null 포인터 예외가 발생합니다. 도움말 pls. 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(); 
     } 
    } 
} 

하나가 코드를 이해하지 않는 경우

내가 더 구체적인 수 있습니다, 문의하시기 바랍니다,하지만 여기 내 첫 번째 게시물 내가 가지 새로운 자바입니다. 당신의 Simulation.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

예외와 관련된 질문에 예외 스택 추적을 포함하면 훨씬 많은 도움을 얻을 수 있습니다. –

+0

이것은 내가 얻는 것입니다 : –

+0

'timer1','timer2' 및'timer3'는 어디에 선언되어 있습니까? – Jim

답변

1

라인 (229)는 NullPointerException를 일으키는 :

이것은 내가 출력 창에 무엇을 얻을 수 있습니다. 특히 TimeClass 내부 클래스의 actionPerformed (...) 메소드에 있습니다.

해당 줄로 이동하면 문제가 발생합니다.

는 나는이 3 선 중 하나입니다 의심 :이 타이머 변수의

 timer1.stop(); 
     timer2.stop(); 
     timer3.stop(); 

하나는 아마도 null입니다. (아마도 진짜 문제를 해결하지 것은 있지만)입니다

빠른 수정이 : 다른 두 개의 타이머 변수

if (timer1 != null) { 
    timer1.stop(); 
} 

등등.

+0

이 시도했지만 지금은 타이머가 절대 멈추지 않습니다 –

+0

이것은 실제 문제를 가리켜 야합니다 ... 타이머 이 null이고 당신이 생각하는 타이머가 포함되어 있지 않습니다. –