Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    emceenugget gave you the right direction...

    problem is your for loop always start from 0
    say..
    if you call the method at "second" time, array element at 0 should fill something and not null.
    then ... return false...
    do you understand?

  2. #22
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    thank you, ys I understood you now, Waht the solute that you advice ?

  3. #23
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    I did the following but this is too didn't work correctly :(

    PHP Code:
    	public boolean addCarToArray(int licensePlate,int year, String 
    			manufacturer, String model,Color colour,int numDoors, double 
    			topSpeed, int totalRaces) {
    		int temp=0;
    		if (carsArr.length<MAX_ARRAYS_LENGTH){
    			for (current_Length_Car_Array=temp;current_Length_Car_Array<
    			MAX_ARRAYS_LENGTH;current_Length_Car_Array++){
    				if (carsArr[current_Length_Car_Array]==null){
    					ArrayList <RaceCar> car=new ArrayList<RaceCar>();		
    					car.add(new RaceCar(licensePlate,year,manufacturer, model,  colour,
    					numDoors,topSpeed, totalRaces));
    					temp=current_Length_Car_Array;
    					return true;
    				}
    				else return false;
    			}			
    		}
    		return false;
    	 }

  4. #24
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Hello,
    I solute the problem, but Now I have a last problem
    When I run the program, apears to me this message:

    Exception in thread "main" java.lang.NullPointerException
    at RaceCar.<init>(RaceCar.java:8)
    at SysF1.addCarToArray(SysF1.java:130)
    at MainClass.main(MainClass.java:59)

    this is the lines from 128-130 on SysF1 class:
    PHP Code:
    if (carsArr[current_Length_Car_Array]==null){
    carsArr[current_Length_Car_Array]=new RaceCar(licensePlate,
    year,manufacturer, model,colour,numDoors,topSpeed, totalRaces);
    this is th lines from 4-8 on RaceCar class:
    PHP Code:
    public class RaceCar extends Car{
    protected double topSpeed;
    protected int totalRaces;
    protected RaceDriver driver;
    protected double performanceRate=driver.totalScore/totalRaces;
    and this is the lines from 58-61 on MainClass class :
    PHP Code:
    else if (command.equals("addRaceCarToArray")){
    sys.addCarToArray(input.nextInt(),input.nextInt(),input.next(),input.next(), 
    new Color(input.nextInt(),input.nextInt(),input.nextInt()),
    input.nextInt(),input.nextDouble(),input.nextInt());
    I think that the reason is on this line:
    PHP Code:
    protected double performanceRate=driver.totalScore/totalRaces;
    because the driver = null, so I will get nullPointerException, and I think that the solution is to move this line to another place after defining the driver, but i doesn't know exacly where should move it.
    Can anyone help me plz ?

  5. #25
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You are right, the exception is thrown because driver is null. To make this work, you must initialize driver. You would probably want to initialize it within a constructor or a method, if the user enters values for driver.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #26
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    The program read input from txt, I wrote this code:
    PHP Code:
    				driversArr[i].car.getRaces();
    				driversArr[i].getTotalScore();
    				driversArr[i].car.setRaces(driversArr[i].car.totalRaces);
    				driversArr[i].setTotalScore(driversArr[i].totalScore);
    				driversArr[i].car.performanceRate=driversArr[i].car.totalRaces/driversArr[i].totalScore;
    Is this code is right ?
    if yes where I should add it plz ?

Page 2 of 2 FirstFirst 12

Similar Threads

  1. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 10:20 PM
  2. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 08:59 PM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  4. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 PM

Posting Permissions

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