0

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...

  • 3
    You will want to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. So on that note: which line throws the NPE?? – Hovercraft Full Of Eels Nov 17 '14 at 14:40
  • 2
    http://stackoverflow.com/a/24100776/829571 – assylias Nov 17 '14 at 14:42
  • What are you trying to do with `head = tail = 0;` ? If you want both `head` and `tail` to be `0` you should use `head=0; tail=0;` if you want `head` to be `tail` and then `tail` to be `0` you should use `head=tail; tail=0;` – Charlie Nov 17 '14 at 14:43
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Florent Bayle Nov 17 '14 at 14:43

1 Answers1

0

There are various places where NPE can be thrown in your code . couple of them are :-

 a = input.nextInt();// input can be null
 String[] parts = s.split(" ");// s can be null
 fifo[tail].isEmpty()) fifo[tail].put(A[j] +4)//fifo[tail] can be null
M Sach
  • 33,416
  • 76
  • 221
  • 314