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
Solved

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

Posted on 2007-03-25
7
1,942 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
web services creation SOAP vs REST 5 56
runtime exception 2 41
throw exception 21 58
mysql jsp example issue 32 48
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

839 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