Results 1 to 2 of 2
  1. #1
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default java.lang.NullPointerException

    I'm trying to write a supermarket simulation where customers have a 1/3 chance of being spawned every minute.
    The time it takes to wait on them is 1-4 minutes determined randomly.
    I want it to print out when a customer has entered and when one has left.
    However I keep getting null pointer exceptions on the bolded parts. Anyone got any ideas how to fix it?
    Java Code:
    public class Customer {
    
    	private int arrivalTime;
    	private int exitTime;
    	private int timeInQueue;
    	private int customerNumber;
    	private int processTime;
    	
    	Customer(int customerNumber, int arrivalTime, int exitTime, int timeInQueue,
    			int processTime){
    		this.arrivalTime= arrivalTime;
    		this.exitTime=exitTime;
    		this.timeInQueue=timeInQueue;
    		this.customerNumber=customerNumber;
    		this.processTime=processTime;
    	}
    	
    	Customer(int customerNumber, int arrivalTime, int processTime){
    		this.customerNumber=customerNumber;
    		this.arrivalTime=arrivalTime;
    		this.processTime=processTime;
    	}
    	
    	public void setArrivalTime(int timeArrived){
    		timeArrived = arrivalTime;
    	}
    	
    	public int getArrivalTime(){
    		return arrivalTime;
    	}
    	
    	public void setExitTime(int timeLeft){
    		timeLeft = exitTime;
    	}
    	
    	
    	public int getExitTime(){
    		return exitTime;
    	}
    	
    	public int getTimeInQueue(){
    		timeInQueue= exitTime-arrivalTime;
    		return timeInQueue;
    	}
    	
    	public void setCustomerNumber(int number){
    		customerNumber=number;
    	}
    	
    	public int getCustomerNumber(){
    		return customerNumber;
    	}
    	
    	public void setProcessTime(int time){
    		processTime=time;
    	}
    	
    	public int getProcessTime(){
    		return processTime;
    	}
    }
    Java Code:
    import java.util.LinkedList;
    
    public class Simulation {
    
    	private LinkedList<Customer> customerList;
    	private int minutesThatHavePast;
    	private int customerNumber;
    	private int customerProcessTime;
    	
    	public Simulation(){
    		minutesThatHavePast=0;
    		createCustomer();
    	}
    	
    	public void createCustomer(){
    		if(Math.random()*3==1){
    			int theProccessTimeForThisCustomer =  (int) (Math.random()*4+1);
    			customerNumber++;
    			
    			customerList.add(new Customer(customerNumber, minutesThatHavePast, 
    					theProccessTimeForThisCustomer));
    			System.out.println("Customer number "+customerNumber+
    					 " has entered the queue"); 
    					
    			determineIfTheCustomerIsFinished();
    			addToMinutesHavePast();
    			}
    		
    		else{determineIfTheCustomerIsFinished();
    		addToMinutesHavePast();}
    				
    	}
    	
    	public void addToMinutesHavePast(){
    		if(minutesThatHavePast<=720) {minutesThatHavePast++; 
    		customerProcessTime++;
    		createCustomer();}
    	}
    	
    	
    	public void determineIfTheCustomerIsFinished(){
    		Customer customerBeingServiced=customerList.get(0);
    		
    		if (customerBeingServiced.getProcessTime()==customerProcessTime){
    			System.out.println("Customer Number "+
    				customerBeingServiced.getCustomerNumber()+" has left the queue");
    			
    			customerList.remove(0);
    			customerProcessTime=0;
    		}
    	}
    	
    	public static void main(String args[]){
    		
    		new Simulation();
    		
    	}
    	
    }
    Thanks.
    Felissa:p :p

  2. #2
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    The problem is you haven't instantiated the List yet.
    My suggestion is you do that in the constructor as we know that will only be called once per simulation, and so we can be assured that it will only occur once.
    Java Code:
    private LinkedList<Customer> customerList;
    	private int minutesThatHavePast;
    	private int customerNumber;
    	private int customerProcessTime;
    	
    	public Simulation(){
    		minutesThatHavePast=0;
                    customerList = new LinkedList<Customer>();
    		createCustomer();
    	}
    Greetings
    Eric

Similar Threads

  1. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 10:01 AM
  2. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:39 PM
  3. Replies: 1
    Last Post: 07-16-2007, 07:32 AM
  4. Replies: 2
    Last Post: 07-16-2007, 07:21 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •