Results 1 to 8 of 8
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default make static ref to non-static method?

    My compiler is giving me the message "Cannot make a static reference to the non-static method setLines() from the type Test" in the main function on both lines that I call functions. This is what I have so far -
    Java Code:
    package test;
    import java.util.HashMap;
    
    public class Test {
    	
    	private HashMap<String, String> theLines = new HashMap<String, String>();
    	private String theList[];
    	
    	/**
    	 * Set the lines into a hashmap
    	 */
    	public void setLines()
    	{
    		theLines = new HashMap<String, String>();
    		theLines.put("1", "This is the first line");
    		theLines.put("2", "This is the second line");
    		theLines.put("3", "This is the third line.");
    		theLines.put("4", "This is the fourth line.");
    	}
    	
    	/**
    	 * Set the lines into an array
    	 */
    	public void setList()
    	{
    		theList = new String[4];
    		for(int x = 0; x < 4; x++)
    			theList[x] = theLines.get(x);
    		
    	}
    	
    
    	public String[] getList()
    	{
    		return theList;
    	}
    	
    	/**
    	 * A function to switch two lines.
    	 * @param line1 The line to be switched
    	 * @param line2 The line to switch it with
    	 */
    	public void switchLines(int line1, int line2)
    	{
    		String middleMan = "";
    		middleMan = theLines.get(line1);
    		theList[line1] = theLines.get(line2);
    		theList[line2] = middleMan;
    	}
    	
    	/**
    	 * I want to make this a function to randomize all the lines
    	 * preferably after setting them into an array.
    	 * I just don't know how to do this yet.
    	 */
    	public void randomizeList()
    	{
    		
    	}
    	
    	//Displays the list
    	public void displayTheList()
    	{
    		for(int x = 0;x < 4;x++)
    			System.out.println(theList[x] + "\n");
    	}
    	
    	
    	
    	
    	public static void main(String args[]) {
    		setLines();		//These two lines are where 
    		setList();		//I get the compiler message.
    						
    	
    		
    		
    		
    	
    		
    		
    		
    
    	}
    
    }
    I want to know what I'm doing wrong with the static functions or how I could make it work. I'm not done yet obviously, but I want to make sure this is working before continuing because I'll most likely get the same message for all the functions :(

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    quick solution: in your main method, you need to do "Test t = new Test();" followed by whatever you're trying to execute on that specific instance of your class, like "t.method();"

    better solution: to learn about why this happens, read up on static vs instance in java. it's a fundamental concept.

  3. #3
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default cool

    Okay cool this is what I have now.
    Java Code:
    package test;
    import java.util.HashMap;
    import java.util.Random;
    
    public class Test {
    	
    	private HashMap<String, String> theLines = new HashMap<String, String>();
    	private String theList[];
    	private Random random;
    	
    	public Test() {}
    	
    	/**
    	 * Set the lines into a hashmap
    	 */
    	public void setLines()
    	{
    		theLines = new HashMap<String, String>();
    		theLines.put("1", "This is the first line");
    		theLines.put("2", "This is the second line");
    		theLines.put("3", "This is the third line.");
    		theLines.put("4", "This is the fourth line.");
    	}
    	
    	/**
    	 * Set the lines into an array
    	 */
    	public void setList()
    	{
    		theList = new String[4];
    		for(int x = 0; x < 4; x++)
    			theList[x] = theLines.get(x);
    		
    	}
    	
    
    	public String[] getList()
    	{
    		return theList;
    	}
    	
    	/**
    	 * A function to switch two lines.
    	 * @param line1 The line to be switched
    	 * @param line2 The line to switch it with
    	 */
    	public void switchLines(int line1, int line2)
    	{
    		String middleMan = "";
    		middleMan = theLines.get(line1);
    		theList[line1] = theLines.get(line2);
    		theList[line2] = middleMan;
    	}
    	
    	/**
    	 * I want to make this a function to randomize all the lines
    	 * preferably after setting them into an array.
    	 * I just don't know how to do this yet.
    	 */
    	public void randomizeList()
    	{
    		random = new Random();
    		for(int x=0;x<10;x++)
    			switchLines(random.nextInt(4), random.nextInt(4));
    			
    	}
    	
    	//Displays the list
    	public void displayTheList()
    	{
    		for(int x = 0;x < 4;x++)
    			System.out.println(theList[x] + "\n");
    	}
    	
    	
    	
    	
    	public static void main(String args[]) {
    		Test t = new Test();
    		t.setLines();		
    		t.setList();		
    		t.randomizeList();
    		System.out.println("The lines have been randomized.\n");
    		System.out.println("It is up to you to put them back in order.\n");
    		t.displayTheList();
    		
    						
    	
    		
    		
    		
    	
    		
    		
    		
    
    	}
    
    }
    Whenever I run the program, it prints the "the lines have been.." and "its up to you.." lines but when it displays the list, it displays 3 nulls. I tried over-looking the setLines, setList, and displayList methods but I didn't see why null would be displayed. Sorry if this is a really obvious question (as most of mine are), but all I have is the Internet and my book lol.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    In. your 'setLines' method, you're using String literal as your has keys. In 'setList', you're trying to look up elements as Integers (auto boxed int's). Since the keys are strings and not Integers, theLines.get(x) returns null, so the contents of 'theList' become {null, null, null, null}... and nulls are what you are seeing displayed.

    Make your keys Integers instead of Strings, and it should work.

  5. #5
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    when i change
    Java Code:
    private HashMap<int, String> theLines = new HashMap<int, String>();
    to
    Java Code:
    private HashMap<int, String> theLines = new HashMap<int, String>();
    it tells me - "syntax error on token "int", dimensions expected after this token"
    i dont really know what it is telling me. what dimensions should follow the int? or be around it in general? i thought all i needed was a variable type for the keys.

  6. #6
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    whoops sorry i mean when i change
    Java Code:
    private HashMap<String, String> theLines = new HashMap<String, String>();
    to what i have in the message above

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    hashmap cannot use int as a key. it can only use objects, and int is a primitive. use the integer wrapper class.

    also, you should really try to learn to decipher what the compiler is telling you. it will tell you the exact location of what is causing the error and what the error is. you probably aren't the first to encounter the error, so searching not just the forum but just in search engines will do you good. if you learn to do this, you'll learn how to recognize and avoid making the mistakes in the future.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Use Integer, not int as the key type. Sorry I didn't make that clear.

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  2. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  3. Please help, need to make my class static.
    By sumak in forum New To Java
    Replies: 1
    Last Post: 04-19-2008, 08:29 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

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