Link to home
Start Free TrialLog in
Avatar of chinamox
chinamox

asked on

updated project is throwing a NullPointerException...

I have been trying to flesh out an elevator simulation for sometime now.  In the midst of adding exceptions I seem to have mangled the orginal program to the point that it no longer functions correctly.  I believe the problem is somewhere in the move() method, where I have likely screwed up the implemtation of the move method.  

Is this assumption correct or totally off base?  Any help in getting my little elevator simulation running again would be very welcome.  




__CODE__

// class Elevator

public class Elevator {
      int capacity = 10;
      int num_floors = 7;
      
      public int passengers;
      public int current_floor;
      static int cycle_count = 0;
      int print_floor = 0;
      
      // Direction of movement
      private boolean dir_up = true;
      
      // switched to boolean array to reflect calls to a particular floor
      // if you think of a real elevator it stops whether one person presses
      // the button or ten people press it.
      public boolean [] req_per_floor = new boolean[7];
      
      // records the number of passengers who want to head to a floor
      public int [] pass_to_floor ={0,0,0,0,0,0,0};
      
      //Creates
       public static Floor floors[] = new Floor[7];
      
       //class constructor inserted here.
      public Elevator() {
            current_floor = 0;
            dir_up = true;
            passengers = 0;
      }
      

/** Main Method
  * @param args
  *
*/                  
public static void main(String[] args){
      Elevator myElevator = new Elevator();
                  
                  //consturct som new floors
                  for (int f =0; f < 6; f++){//might have to be seven
                        floors[f] = new Floor();
                  }
                  
                  //set passengers waiting
                  floors[1].queued_passengers = 3;
                  floors[2].queued_passengers = 2;
                  floors[3].queued_passengers = 1;
                  floors[5].queued_passengers = 4;
            //      floors[6].queued_passengers = 2; // should thow exceptions
            
                  System.out.println(myElevator);            
                  myElevator.move();                  
      }
      
      
            //#4 move method
      public void move(){
            while (cycle_count <= 2){
                  if(dir_up == true){
                        current_floor = current_floor +1;
                        System.out.println("Going down one floor. There are currently " + passengers + " passengers on board.");
                  }
            else {
            current_floor = current_floor -1;
            System.out.println("Going down one floor.There are currently " + passengers + " passengers on board.");
            }
                  if(passengers < capacity){
                        if(floors[current_floor].queued_passengers > 0){
                              stop();
                        }
                  }
            //reverses dirction at bottom floor
            if (current_floor == 0){dir_up = true;}
            if (current_floor == 6){dir_up = false;}
            if (current_floor == 6){cycle_count++;}
            }
      }
/**Overide the toString() from java.lang.Object
 * @return A string reporting the elevator's state at each floor.
*/  
      public String toString(){
            print_floor = current_floor + 1;
            return "Currently "+passengers +" passengers are onboard."+ "\n"+"Current floor :" + print_floor ;
      }
            
/** Stops the Elevator at each floor.  
 * @param print_floor
 * @return No return value
*/
   public void stop(){
         System.out.println("stop!");
        
        if(pass_to_floor[current_floor] >= 1){
              disembark_passenger();
              }
         if(req_per_floor[current_floor] == true ){
               try{
               boardPassenger();
               if(passengers >= capacity){
                     throw new ElevatorFullException();
                     }
               }catch (ElevatorFullException myElevator){
                     System.out.println("ElevatorFullEception");
               }
               
         }
         print_floor = current_floor + 1;
      }
            
            
/** Board passanger method
 * @return No return value
*/
      public void boardPassenger() throws ElevatorFullException{
            if(floors[current_floor].queued_passengers == 0){
                  move();
            }
            
            while(passengers <= capacity){
                  floors[current_floor].queued_passengers = floors[current_floor].queued_passengers - 1;
                  passengers++;
                  pass_to_floor[0]++;
                  }
            if(passengers >= capacity)
            {
                 throw new ElevatorFullException();
            }      
      }
        
            
            //req_per_floor[current_floor]--;
      
/** Disembark passenger method
 * @return No return value
*/
      public void disembark_passenger(){
            passengers = passengers - 1;
            pass_to_floor[current_floor]--;
      }

      public void registerRequest(){
            if (floors[current_floor].queued_passengers >= 1){
            req_per_floor[current_floor]= true;
            }
            else{
                  req_per_floor[current_floor]= false;
            }
      }

}


// class Floor

public class Floor {
      
      
      public int queued_passengers;
      
            
      public void unloadPassenger(Elevator myElevator){
            int i = myElevator.pass_to_floor[myElevator.current_floor];
            if (i > 0){
                  myElevator.passengers = myElevator.passengers - 1;
                  System.out.println("One passenger unloaded on floor." + queued_passengers + " are left on floor#" + myElevator.current_floor);
                  }
            return;
      }
}

// class ElevatorFullException

import java.lang.Exception ;
public class ElevatorFullException extends Exception {
      public ElevatorFullException(){}
      public ElevatorFullException (Elevator myElevator){
            if(myElevator.passengers >= myElevator.capacity){
                  myElevator.move();
            }
      }
      
      public String toString() {
            return "Danger! Elevator is overloaded!";
      }
}


__END__

I run the above code and I get the following read out:

___CODE___

Currently 0 passengers are onboard.
Current floor :1
Going down one floor. There are currently 0 passengers on board.
stop!
Going down one floor. There are currently 0 passengers on board.
stop!
Going down one floor. There are currently 0 passengers on board.
stop!
Going down one floor. There are currently 0 passengers on board.
Going down one floor. There are currently 0 passengers on board.
stop!
Going down one floor. There are currently 0 passengers on board.
Exception in thread "main" java.lang.NullPointerException
      at cscie160.hw2.Elevator.move(Elevator.java:69)
      at cscie160.hw2.Elevator.main(Elevator.java:53)
__END__

Right now I am assuming that the NullPointerException is tied to the problem in move().

Regards,

chinamox
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sciuriware
sciuriware

This program is a mess.
I told you before that you should not have business logic inside your Exception constructor
and now it's fatal.
You did not give any arguments to the

       throw new ElevatorFullException();

statements, and I think that it's a bad design to do so.

Furthermore, I tried to compile your program in ECLIPSE and I get zillions of warnings and errors.

;JOOP!
The handling code for an exception should be in the

    catch(ElevatorFullException e)
    {


block.

;JOOP!
Avatar of chinamox

ASKER

wow.  ok so now I have this

__CODE__
      public void move(){
            while (cycle_count <= 2){
                  if(dir_up == true){
                        current_floor = current_floor +1;
                        System.out.println("Going up one floor. There are currently " + passengers + " passengers on board.");
                  }
            else {
            current_floor = current_floor -1;
            System.out.println("Going down one floor.There are currently " + passengers + " passengers on board.");
            }
                  if(passengers < capacity){
                  //      if(floors[current_floor].queued_passengers > 0){
                              stop();
                  //      }
                  }
            //reverses dirction at bottom floor
            if (current_floor == 0){dir_up = true;}
            if (current_floor == 6){dir_up = false;}
            if (current_floor == 6){cycle_count++;}
            }
      }
__END__

but and now my elevator seems to board negative passengers!:

__CODE__
Currently 0 passengers are onboard.
Current floor :1
Going up one floor. There are currently 0 passengers on board.
stop!
Going up one floor. There are currently -1 passengers on board.
stop!
Going up one floor. There are currently -1 passengers on board.
stop!
Going up one floor. There are currently -1 passengers on board.
stop!
Going up one floor. There are currently -2 passengers on board.
stop!
Going up one floor. There are currently -2 passengers on board.
stop!
Going down one floor.There are currently -2 passengers on board.
stop!
Going down one floor.There are currently -2 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going down one floor.There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
Going up one floor. There are currently -3 passengers on board.
stop!
__END__

Any idea why that is happening?  Would you add an exception to stop that from happening?

cheers,

mox
I give up.
Put this program in ECLIPSE and run it in debug mode statement by statement.
Then you'll find that you still have no idea about programming.
Please, please first follow a tutorial about programming.

Goodbye.

;JOOP!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial