I have this class which creates a simulation of registers and clients and asks for total time to finish with them and the average waiting time. I have created a FIFO array which is consisted of listed queues of the IntQueueImpl class i have. Now the code compliles fine but when i run it i get a runtime.
Here's the class:
import java.util.Scanner;
public class QueueSimulation
{
public static void main(String[] args)
{
int a, tail, head;
int i = 0;
String s;
Scanner input = new Scanner(System.in);
System.out.println("This programs simulates a queue of customers at registers.");
System.out.println("We assume the every customer take 4 seconds to leave the register.");
System.out.println("Enter the number of registers you want to simulate:");
a = input.nextInt();
while(a==0 || a <0){
System.out.println("0 registers or no registers is invalid. Enter again: ");
a = input.nextInt();
}
IntQueueImpl fifo[] = new IntQueueImpl[a];
System.out.println("Enter how many customers enter per second.For example: 0 0 1 1 2 2 2 3 3. Enter: ");
s = input.nextLine();
s = input.nextLine();
String[] parts = s.split(" ");
int[] A = new int[parts.length];
for(int j = 0; j < parts.length; j++){
A[j]= Integer.parseInt(parts[j]);
while(A[j] < 0){
System.out.println(A[j]+" is invalid.Enter positive integer: ");
A[j]= input.nextInt();
}
}
input.close();
head = tail = 0;
int T[] = new int[A.length];
int temp= 0;
for(int j = 0 ; j< A.length ; j++){
if(fifo[tail].isEmpty()) fifo[tail].put(A[j] +4);
else{
temp = fifo[tail].size();
fifo[tail].put(A[j] + 4*temp);
}
if(tail == a-1){
tail=0;
}else tail++;
if(j>=4){
temp = fifo[head].get();
T[j]= j-temp;
if(head == a-1){
head=0;
}else head++;
}
}
int sum = 0;
for(int j= 0; j<T.length; j++){
sum =+ T[j];
}
System.out.println("All customers leaved at: " + sum + " seconds.");
}
}
And the runtime error i get is this:
Exception in thread "main" java.lang.NullPointerException
at QueueSimulation.main(QueueSimulation.java:58)
which is this command:
if(fifo[tail].isEmpty()) fifo[tail].put(A[j] +4);
I have tried many things to avoid it but nothing happens...