Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2007, 07:00 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
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?
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; } }
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-05-2007, 07:02 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
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.
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NullPointerException stevemcc AWT / Swing 2 02-08-2008 10:01 AM
java.lang.NullPointerException ravian New To Java 1 01-13-2008 08:39 PM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 07:32 AM
AWT-EventQueue-0 java.lang.NullPointerException susan NetBeans 2 07-16-2007 07:21 AM
Error Java.lang.NullPointerException in JBuilder2006 Jack Other IDEs 2 07-02-2007 03:29 AM


All times are GMT +3. The time now is 06:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org