Results 1 to 4 of 4
  1. #1
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default "Cannot Make a Static Reference to the Non-Static Reference"

    I'm getting this error in reference to the roomEntry and printData methods (see code below). I've never used the public static void main(String[] args) declaration before. In the past, I've always used the acm library so I could use public void run() as the declaration for my main method. Can someone explain this issue to me? Thanks.

    Java Code:
    /*
     * File: FootTrafficAnalysis.java
    */
    
    
    import java.util.*;
    
    
    public class FootTrafficAnalysis{
    	
    	public static void main(String[] args){
    		int visitor = 0;
    		int roomNo = 0;
    		String inOrOut = "";
    		int time = 0;
    		Scanner s = new Scanner("trafficdata.txt");
    		int size = s.nextInt();
    		for (int i = 0; i < size; i++){
    			visitor = s.nextInt();
    			roomNo = s.nextInt();
    			inOrOut = s.next();
    			time = s.nextInt();
    			roomEntry(roomNo, inOrOut, time);
    		}
    		printData();
    		
    	
    	}
    	private void roomEntry(int roomIndex, String direction, int time){
      		visitors = 0;
      		averageTime = 0;
      		if (!trafficData.containsKey(roomIndex)){
      			roomData = new Integer[2];
      			if (direction.equals("I")){
      				visitors ++;
      				averageTime += time;
      				}
      			if (direction.equals("O")){
      				time --;
      			}
      			roomData[0] = visitors;
      			roomData[1] = averageTime;
      			trafficData.put(roomIndex, roomData);
      		}
      		if (trafficData.containsKey(roomIndex)){
      			roomData = getMapValues(roomIndex);
      			if (direction.equals("I")){
      				roomData[0] += visitors;
      				roomData[1] += averageTime;
      			}
      			if (direction.equals("I")){
      				roomData[1] -= time;
      			}
      			trafficData.put(roomIndex, roomData);
      		}
      		
         }
    	
    	private Integer[] getMapValues(int roomNo){
      		return trafficData.get(roomNo);
      	}
    	
    	private void printData(){
    		Iterator<Integer> it = trafficData.keySet().iterator();
    		while (it.hasNext()){
    			int key = it.next();
    			Integer[] valuesToPrint = trafficData.get(key);
    			System.out.print("Room " + key + valuesToPrint[0] + " minute average visit, "
    					+ valuesToPrint[1] +  " visitor total.");
    		}
    		
    	}
    
    
    	//Instance variables
    	 private HashMap<Integer, Integer[]> trafficData = new HashMap<Integer, Integer[]>();
    	 private Integer[] roomData;
    	 private int visitors;
    	 private int averageTime;
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: "Cannot Make a Static Reference to the Non-Static Reference"

    In Java, you can't make a reference to an instance method or field from a static context. The main entry point is a static context. The simplest solution is to take all of your code inside of main, and place it in a startUp method which also contains the main arguments as a parameter. Then from main, do something like this:

    Java Code:
    new FootTrafficAnalysis().startUp(args);
    I also suggest you read up on static variables and fields in the tutorials linked in my signature.

    Regards,
    Jim
    Last edited by jim829; 07-24-2013 at 05:00 PM. Reason: Stupid mistake - corrected parameter call
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kathmandu is offline Member
    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Default Re: "Cannot Make a Static Reference to the Non-Static Reference"

    Quote Originally Posted by jim829 View Post
    I also suggest you read up on static variables and fields in the tutorials linked in my signature.

    Regards,
    Jim
    Will do. Thank you.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: "Cannot Make a Static Reference to the Non-Static Reference"

    I made a mistake in my previous post. I was thinking method and not method call.

    Java Code:
    // Assuming a startup method like
    
    public void startUp(String[] args) {
        // do stuff
    }
    
    // It would be called as
    
    new FootTrafficAnalysis().startUp(args); // passing args from main
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2013, 02:30 AM
  2. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  3. Cannot make a static reference...
    By Guy in forum New To Java
    Replies: 12
    Last Post: 07-28-2011, 07:22 AM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •