Solved

Java error 'non-static variable numOfPass cannot be referenced from a static context'

Posted on 2007-03-25
7
1,945 Views
Last Modified: 2013-11-23
I am trying to write a java Elevator simulator with two classes, Class Elevator and Class Floor. I am trying to call a method in Class Floor from a method in class Elevator and I get the following errors:
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:144: non-static variable numOfPass cannot be referenced from a static context
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
                        ^
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:144: non-static variable numOfPass cannot be referenced from a static context
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
                                             ^
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:144: non-static variable destReq cannot be referenced from a static context
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
                                                                  ^
E:\My Documents\Harvard\csci-e160\HomeWork\cscie160\hw2\Elevator.java:145: non-static variable destReq cannot be referenced from a static context
            Elevator.destReq[s] = 0;

Here are my two methods:

      public void stop(int s)
      {
            System.out.println("\nStopping on floor " + curFloor);
            myFloors[curFloor].unloadPassengers(s);
      } // end of method stop

and

      public void unloadPassengers(int s)
      {
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
            Elevator.destReq[s] = 0;
      }

Any ideas on how to resolve the errors so my code will compile?

Here is the whole code:

package cscie160.hw2;

public class Elevator
{
      int[] destReq = new int[8];  // add 1 to array size so current floor matches array elememt, for ex., curFloor 7 = int[7].
      Floor[] myFloors = new Floor[8]; // add 1 to array size so Floor[] = current floor.


      public static void main(String[] args)
      {
            Elevator myElevator = new Elevator();  // create new Elevator object

            myElevator.curFloor = BOTTOMFLOOR;
            myElevator.curDirectionUp = 1;
            myElevator.curDirectionDown = 2;
            myElevator.curDirection = myElevator.curDirectionUp;

            for (int i = 0; i < MAXELEVATORRUNS; i++)
            {
                  myElevator.boardPassenger(2);
                  myElevator.boardPassenger(2);
                  myElevator.boardPassenger(3);
                  myElevator.registerRequest(6, 6);


                  while (myElevator.curFloor <= TOPFLOOR && myElevator.curDirection == myElevator.curDirectionUp)   // create loop to move elevator up and down stopping only on requested floors
                  {
                        myElevator.move();
                  }

                  while (myElevator.curFloor >= BOTTOMFLOOR && myElevator.curDirection == myElevator.curDirectionDown)
                  {
                        myElevator.move();
                  }
            }  // end of for loop to board passengers and move the elevator
      } // end of method main


      public void move()
      {
            for (int s = 0; s <= 7; s++)  // for loop to determine if elevator should stop
            {
                  if (curFloor == s && destReq[s] > 0)
                  {
                        stop(s);
                  }
            }

            System.out.println(toString());

            if (curDirection == curDirectionUp)
            {
                  curFloor++;
            }

            if (curDirection == curDirectionDown)
            {
                  curFloor--;
            }

            if (curFloor == TOPFLOOR)
            {
                  curDirection = curDirectionDown;
            }

            if (curFloor == BOTTOMFLOOR)
            {
                  curDirection = curDirectionUp;
            }
      } // end of method move


      public void stop(int s)
      {
            System.out.println("\nStopping on floor " + curFloor);
            myFloors[curFloor].unloadPassengers(s);
      } // end of method stop


      public void boardPassenger(int floor)
      {
            destFloor = floor;

            for (int b = 0; b <= 7; b++)  // loop to enter requested floor in destReq array.
            {
                  if (floor == b)
                  {
                  destReq[destFloor] = destReq[destFloor] + 1;
                  numOfPass++;
                  }
            }
      } // end of method boardPassenger

      public void registerRequest(int floor, int passengers)
      {
            reqFloor = floor;
            reqPass = passengers;
            myFloors[reqFloor] = new Floor(reqPass);
      }


      public String toString()  // override toString method
      {
            StringBuffer status = new StringBuffer();
            status.append("Currently " + numOfPass + " passengers onboard. \nCurrent Floor: " + curFloor + "\n");

            return status.toString();
      } // end of method toString


      // declare values for constants

      private static final int CAPACITY = 10;
      private static final int BOTTOMFLOOR = 1;
      private static final int TOPFLOOR = 7;
      private static final int MAXELEVATORRUNS = 5;


      // declare instance values

      int curFloor;
      int curDirection;
      int curDirectionUp;
      int curDirectionDown;
      int destFloor;
      int numOfPass;
      int reqFloor;
      int reqPass;

} // end of class Elevator




class Floor
{
      public Floor(int passengers)
      {
            numOfWaitingPass = passengers;
      }

      public void unloadPassengers(int s)
      {
            Elevator.numOfPass = Elevator.numOfPass - Elevator.destReq[s];
            Elevator.destReq[s] = 0;
      }

      int numOfWaitingPass;
      int requestedFloor;
      int numOfUnloadingPass;


}
0
Comment
Question by:jhall13291
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18788947
You have to create an object of Elevator and use it.

class Floor
{
      private Elevator e ;
      public Floor(int passengers)
      {
            e = new Elevator () ;
            numOfWaitingPass = passengers;
      }

      public void unloadPassengers(int s)
      {
            e.numOfPass = e.numOfPass - e.destReq[s];
            e.destReq[s] = 0;
      }

      int numOfWaitingPass;
      int requestedFloor;
      int numOfUnloadingPass;

}
 
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 18788954
Better to have a method to set the numOfPass instead of doing it using the data member.

// in Elevator
public void setNumOfPass ( int numOfPass )
{
    this.numOfPass = numOfPass ;
}

public int getNumOfPass ()
{
  return numOfPass ;
}

// same way put get and set methods for all data members

// in Floor
e.setNumOfPass ( e.getNumOfPass () - e.getDestReq ()[s] ) ;
0
 

Author Comment

by:jhall13291
ID: 18789605
mayankeagle,

Thanks for the help. That cleared up the errors, but I guess my ultimate question is how do I pull the variable values from the Elevator class to the Floor class.

Also, I have already created an Elevator object and reference to that object in the Elevator class, e.g., 'Elevator myElevator = new Elevator()' Do I really need to construct a new elevator in the Floor class?

Thanks again for you help,
JHall
0
Industry Leaders: 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!

 
LVL 30

Expert Comment

by:Mayank S
ID: 18789637
What is the relationship between the two?
0
 
LVL 16

Expert Comment

by:imladris
ID: 18793306
In the Elevator class you have:

 Floor[] myFloors = new Floor[8]; // add 1 to array size so Floor[] = current floor.

I would expect that to be followed by something like:

for(int i=0; i<7; ++i)myFloors[i]=new Floor(0,this);

The this variable should then be remembered in the Floor object so that it has a reference to the Elevator object for calling the Elevator objects methods and/or variables.


0
 
LVL 2

Expert Comment

by:jhshen
ID: 18793544
I think the method unloadPassengers(int s) is not suitable to be put in class Floor.
From the meaning of this method, it should be an action of Elevator.

My suggestion is:
1. remove the method unloadPassengers(int s) from Floor class
2. create a new method public void unloadPassengers(int currentFloor, int s) in Elevator class
Here is the part of sample code you may need to modify in class Elevator:
public void stop(int s)
      {
            System.out.println("\nStopping on floor " + curFloor);
            unloadPassengers(curFloor, s);
      } // end of method stop

      public void unloadPassengers(int currentFloor, int s)
      {
            this.numOfPass = this.numOfPass - this.destReq[s];
            this.destReq[s] = 0;
            //myFloors[currentFloor].numOfUnloadingPass = s;
      } // end of method unloadPassengers
0
 
LVL 16

Expert Comment

by:imladris
ID: 18808638
Did any of that help?

If so, it is time to select an answer and grade it.

If not, perhaps a clarifying question would help.
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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 36
egit plugin on eclipse 8 96
mysql jsp example issue 32 70
ejb entity bean example issue 2 25
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…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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