• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

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
0
chinamox
Asked:
chinamox
2 Solutions
 
objectsCommented:
yes, at line 69 u are referencing a var that is null.
0
 
sciuriwareCommented:
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!
0
 
sciuriwareCommented:
The handling code for an exception should be in the

    catch(ElevatorFullException e)
    {


block.

;JOOP!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
chinamoxAuthor Commented:
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
0
 
sciuriwareCommented:
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!
0
 
bondzoroCommented:
To get RID of null pointer exception
change the following line in main method

from  for (int f =0; f < 6; f++){

to  for (int f =0; f <= 6; f++){

here you have f<6 i.e 0-5

and in move method you are referencing to currentfloor value which goes upto "6" max .

in this line
if(floors[current_floor].queued_passengers > 0){ is substituted as follows

if(floors[6].queued_passengers > 0){

floors[6] is an null object since in main method you have initialized only tilll floors[5].

here you are referencing to null object which throws NullPointer Exception .

Hope this helps . However your elevator program is not loading/unloading passengers appropriately .

I got following results
 current floor 1
Going up one floor. There are currently 0 passengers on board.
 current floor 2
Going up one floor. There are currently 0 passengers on board.
 current floor 3
Going up one floor. There are currently 0 passengers on board.
 current floor 4
Going up one floor. There are currently 0 passengers on board.
 current floor 5
Going up one floor. There are currently 0 passengers on board.
 current floor 6
Going down one floor.There are currently 0 passengers on board.
 current floor 5
Going down one floor.There are currently 0 passengers on board.
 current floor 4
Going down one floor.There are currently 0 passengers on board.
 current floor 3
Going down one floor.There are currently 0 passengers on board.
 current floor 2
Going down one floor.There are currently 0 passengers on board.
 current floor 1
Going down one floor.There are currently 0 passengers on board.
 current floor 0
Going up one floor. There are currently 0 passengers on board.
 current floor 1
Going up one floor. There are currently 0 passengers on board.
 current floor 2
Going up one floor. There are currently 0 passengers on board.
 current floor 3
Going up one floor. There are currently 0 passengers on board.
 current floor 4
Going up one floor. There are currently 0 passengers on board.
 current floor 5
Going up one floor. There are currently 0 passengers on board.
 current floor 6
Going down one floor.There are currently 0 passengers on board.
 current floor 5
Going down one floor.There are currently 0 passengers on board.
 current floor 4
Going down one floor.There are currently 0 passengers on board.
 current floor 3
Going down one floor.There are currently 0 passengers on board.
 current floor 2
Going down one floor.There are currently 0 passengers on board.
 current floor 1
Going down one floor.There are currently 0 passengers on board.
 current floor 0
Going up one floor. There are currently 0 passengers on board.
 current floor 1
Going up one floor. There are currently 0 passengers on board.
 current floor 2
Going up one floor. There are currently 0 passengers on board.
 current floor 3
Going up one floor. There are currently 0 passengers on board.
 current floor 4
Going up one floor. There are currently 0 passengers on board.
 current floor 5
Going up one floor. There are currently 0 passengers on board.


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now