Solved

updated project is throwing a NullPointerException...

Posted on 2006-11-13
6
386 Views
Last Modified: 2011-09-23
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
Comment
Question by:chinamox
6 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 17928775
yes, at line 69 u are referencing a var that is null.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17928807
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 17928814
The handling code for an exception should be in the

    catch(ElevatorFullException e)
    {


block.

;JOOP!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:chinamox
ID: 17928855
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 17928904
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
 
LVL 1

Accepted Solution

by:
bondzoro earned 400 total points
ID: 17936403
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question