Solved

Cannot make a static reference to the non-static field...

Posted on 2006-11-06
4
8,056 Views
Last Modified: 2012-08-13
How do I stop getting the following error message:

'Cannot make a static reference to the non-static field Elevator.passengers'

__CODE__

package cscie160.hw2;


public class Elevator {
      int capacity = 10;
      int num_floors = 7;
      int passengers = 3;
      int current_floor = 0;
      static int cycle_count = 0;
      int print_floor = 0;
      boolean dir_up = true;
      int [] req_per_floor = {0,0,0,0,0,0,0};
      int [] pass_to_floor ={0,2,1,0,0,0,0};
/** Main Method
  * @param args
  *
*/                  
      public static void main(String[] args){
      Elevator myElevator = new Elevator();
                  
            
                  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;
            stop();
            }
            else {
            current_floor = current_floor -1;
            stop();                  
            }
            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(){
               
         while (pass_to_floor[current_floor] >= 1){
               disembark_passenger();
         }
         while(req_per_floor[current_floor] >= 1){
               board_passenger();
         }
         print_floor = current_floor + 1;
      }
            
            
/** Board passanger method
 * @return No return value
*/
      public void board_passenger(){
            passengers = passengers + 1;
            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 class Floor {
      
      

      int queued_passengers =0;
      
      public void BoardPassenger(){
            {
            if (Elevator.passengers <= Elevator.capacity){
                  queued_passengers = queued_passengers - 1;
                  Elevator.passengers++;
            }
            else{

            }
            return ;
            }
      }
}

__END__
0
Comment
Question by:chinamox
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17885706
In class Floor you need to have created an instance or got a reference to Elevator
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17885711
pass in a elevator instance

     public void BoardPassenger(Elevator elevator){
          {
          if (elevator.passengers <= elevator.capacity){
               queued_passengers = queued_passengers - 1;
               levator.passengers++;
          }
          else{

          }
          return ;
          }
     }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17888088
chinamox, why did you ignore my answer?
0
 

Author Comment

by:chinamox
ID: 17897350
CEHJ,  I found the code that objects posted more understandable, as it illistrated what I was doing wrong.  Thank you for your response though, if I had a way to divide my points I would have awarded you some as well.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now