Page 3 of 3 FirstFirst 123
Results 41 to 48 of 48
  1. #41
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I just went back and fixed several errors including the problem with the variable total.

    I ran the test program to see what else was coming up and I have a problem here:

    Java Code:
    	public double getRentalSales()  
    	{ 
    	
    	double total = 0; 		 					
    	for(int i = 0; i < theCars.length; i++)
    		{ 	
    			total += theCars[i].getRentalCost(); 	
    		} 
    	return total; 
    	
    	}
    It yields the following error:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at RentalCarCompany.getRentalSales(RentalCarCompany.java:31)
    	at RentalCarTest.main(RentalCarTest.java:22)
    Not sure why...

  2. #42
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    did you initialize all the elements in the array?

    If you create the following array
    Java Code:
    Car[] c = new Car[10];
    c[0] = new Car(); //default constructor
    System.out.println(c[1]);
    That element isn't initialized so it's still null. All the elements in the array need to be initialized, or you need to test if the element is null, and if it is null continue looping.

    Java Code:
    Car[] c = new Car[5];
    for(int i = 0; i < 3; i++){
      c[i] = new Car();
    }
    for(int i = 0; i < c.length; i++){
      if(c[i] == null){
        continue;
      }
      else{
        //do something
      }
    }

  3. #43
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I don't think I initialized the array. I thought it would place the items in the first empty array and move up from there.

    This is my test class. I have been building it little by little:
    Last edited by camaro01; 03-07-2011 at 05:38 AM.

  4. #44
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    you are creating the cars as seperate items. Try creating a method in the rental car class that adds a car to the array, or a method that randomly generates cars and populates the array.

  5. #45
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Man, of course, I have been worring about everything else that I didn't think about it.

    The addReservation method in the the rental class is suppose to add the cars to the array and keep counting the cars as they are added to the array.

    Java Code:
    	public void addReservation(String carRenter, int carNumber, String carName, 
    	                           String carType, int carDoor, double rate, int days)
    	{
    	   theCars[rentCnt] = new Car(carRenter, carNumber, carName, carType, carDoor, rate, days);
    		rentCnt++;
    		totalRate += rate;
    		totalDays += days;
    	}
    This method should do it.

  6. #46
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, in the tester class use this method to populate the array with cars before running tests on it. You will also need to override toString for the rental class which loops through the array calling each cars toString method, and then adds the retnal specific toString data.

  7. #47
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I lost you in the second half of your post. What about the toString from the Car class? that is printing out the list of customers at this point. I was thinking that I need a toString in the rental car to get the rest of the information needed such as how many 2 or 4 door cars were rented and how much money was made.

  8. #48
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Sorry, I meant that untie rental toString method you will use the cars toString. Just like lots of the rental methods it uses loops. In this case each step of the loop calls a cars toString method and adds it to a stringbuilder or string. After it finishes looping you will need to add more to the string for the totals and finally return the string

Page 3 of 3 FirstFirst 123

Similar Threads

  1. CS project
    By seanfmglobal in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 04:53 AM
  2. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 11:15 AM
  3. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 09:12 PM
  4. Help With Project!!!
    By jackhammer in forum New To Java
    Replies: 5
    Last Post: 12-04-2008, 06:10 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
  •