Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-25
7
Medium Priority
?
1,956 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 2000 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

670 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