Solved

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

Posted on 2007-03-25
7
1,937 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

773 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