Results 1 to 11 of 11
  1. #1
    Den3107 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Compiler thinks non-static method is static

    Hello people

    As the title says itself, the compiler (I'm using JCreator) thinks a certain context is static while it's not, the error that comes with this is that a non-static variable cannot be declared in that method due to it not being static.

    I've checked all the document and nowhere stands static but in a few comments.

    Let me give a file as an example

    Java Code:
    public class A1
    {
    	InfoTracker saveInfo;
    
    	public A1()
    	{
    	}
    
    	public String process(InfoTracker info)
    	{
    		String tempScenario, text;
    		tempScenario = "";
    		switch(info.getScenario())
    		{
    			default: text = "This action is not yet supported.";
    					 tempScenario = "";
    		}
    
    		info.setPreviousScenario(info.getScenario());
    		info.setScenario(tempScenario);
    		saveInfo = info;
    		info.setPreviousText(GUI.mainInfo.getText());
    		return text;
    	}
    
    	public InfoTracker getInfo()
    	{
    		return saveInfo;
    	}
    }
    The error that occurs here is:
    (Build Output)
    Java Code:
    C:\Users\Dennis\Dropbox\Install\JCreator LE\MyProjects\Java in TWO semesters\Own Made\Interactive novel\Control Buttons\A1.java:31: error: non-static variable mainInfo cannot be referenced from a static context
    		info.setPreviousText(GUI.mainInfo.getText());
    		    ^
    (Task View)
    Java Code:
    error: non-static variable mainInfo cannot be referenced from a static context
    As the compiler says, mainInfo is non-static:
    Java Code:
    public JTextArea mainInfo = new JTextArea();

    Help would be much appreciated wince there's a certain error if I would make everything static (process() is not the only method that has this problem, pretty much every method that requests a variable from another class).

    Thank you for reading,
    ~Den3107

    P.S. Don't comment about what this class does, it's all a work in progress

    P.S.S. GUI is a class
    Last edited by Den3107; 08-19-2013 at 02:42 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Compiler thinks non-static method is static

    its not the mainInfo that is the problem, it is the fact that you're using

    Java Code:
    GUI.mainInfo
    I'm guessing that GUI is not a variable but the name of a class. So you're TRYING to refer to mainInfo in a static way (ClassName.memberName), but it is not static.


    BTW: JCreator is your IDE, not the compiler. Don't confuse the JDK with the pretty graphical tool you're using, JCreator is not Java.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Den3107 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Compiler thinks non-static method is static

    Quote Originally Posted by gimbal2 View Post
    its not the mainInfo that is the problem, it is the fact that you're using

    Java Code:
    GUI.mainInfo
    I'm guessing that GUI is not a variable but the name of a class. So you're TRYING to refer to mainInfo in a static way (ClassName.memberName), but it is not static.
    Thank you for the quick reply

    I expect there's no solution except making it static? Since I have problems with saving and loading the save files if I have to make everything static.

    ~Den3107

    Edit: Yes, GUI is a class.

    Edit: Also, I can then understand I shouldn't need to request public variables in that fashion, but why does it also give the error if I request the variable in the fashion of a method within a class that returns it?

    Example:
    Java Code:
    public String getMainInfoText()
    	{
    		return mainInfo.getText();
    	}
    Last edited by Den3107; 08-19-2013 at 02:42 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Compiler thinks non-static method is static

    So what is GUI defined as? You need to show more of your code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Compiler thinks non-static method is static

    Quote Originally Posted by Den3107 View Post
    Thank you for the quick reply

    I expect there's no solution except making it static? Since I have problems with saving and loading the save files if I have to make everything static.

    Edit: Yes, GUI is a class.
    No. Somewhere you are creating an object out of your GUI class; if you are not doing that then you need to be doing that. You need a reference to that GUI object in your AI class so you can get the mainInfo attribute from it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Den3107 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Compiler thinks non-static method is static

    Quote Originally Posted by gimbal2 View Post
    No. Somewhere you are creating an object out of your GUI class; if you are not doing that then you need to be doing that. You need a reference to that GUI object in your AI class so you can get the mainInfo attribute from it.
    Could you please reply to the last edit?

    Quote Originally Posted by Den3107 View Post
    Edit: Also, I can then understand I shouldn't need to request public variables in that fashion, but why does it also give the error if I request the variable in the fashion of a method within a class that returns it?

    Example:
    Java Code:
    public String getMainInfoText()
    	{
    		return mainInfo.getText();
    	}

    Quote Originally Posted by jim829 View Post
    So what is GUI defined as? You need to show more of your code.

    Regards,
    Jim
    As gimbal2 expected, GUI is a class

  7. #7
    Den3107 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Compiler thinks non-static method is static

    Nevermind, I have found the solution

    The problem was that it always makes it a static unless you make the class the called method is in as a variable

    So the new code would be:
    Java Code:
    public class A1
    {
        InfoTracker saveInfo;
     
        public A1()
        {
        }
     
        public String process(InfoTracker info)
        {
            GUI gui = new GUI(); // the new line
            String tempScenario, text;
            tempScenario = "";
            switch(info.getScenario())
            {
                default: text = "This action is not yet supported.";
                         tempScenario = "";
            }
     
            info.setPreviousScenario(info.getScenario());
            info.setScenario(tempScenario);
            saveInfo = info;
            info.setPreviousText(gui.getMainInfoText()); // note how the text between the first bracelets has changed
            return text;
        }
     
        public InfoTracker getInfo()
        {
            return saveInfo;
        }
    }
    Where mainInfo.getText() looks like:
    Java Code:
    public String getMainInfoText()
        {
            return mainInfo.getText();
        }
    Edit: With thanks to gimbal2
    Last edited by Den3107; 08-19-2013 at 03:05 PM.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Compiler thinks non-static method is static

    Your latest code is weird. First of all I expect that you would be doing this:

    Java Code:
    info.setPreviousText(gui.mainInfo.getText());
    and not as it is now, still through the GUI class. On top of that I would expect the code to be this:

    Java Code:
    info.setPreviousText(gui.getMainInfo().getText());
    Unless you made mainInfo public in stead of properly adding a getMainInfo() method.

    So please don't tell me that you made mainInfo static...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Den3107 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Compiler thinks non-static method is static

    Quote Originally Posted by gimbal2 View Post
    Your latest code is weird. First of all I expect that you would be doing this:

    Java Code:
    info.setPreviousText(gui.mainInfo.getText());
    and not as it is now, still through the GUI class. On top of that I would expect the code to be this:

    Java Code:
    info.setPreviousText(gui.getMainInfo().getText());
    Unless you made mainInfo public in stead of properly adding a getMainInfo() method.

    So please don't tell me that you made mainInfo static...
    No no, check the edit, I changed the coding a bit, check it again now

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Compiler thinks non-static method is static

    Oops, right you are. Thumbs up!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Den3107 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Compiler thinks non-static method is static

    Quote Originally Posted by gimbal2 View Post
    Oops, right you are. Thumbs up!
    One for me, two for you.
    After all you helped me with the first step, without it I would've been nowhere.

    Thanks again.

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 07:56 PM
  2. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  3. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  4. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 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
  •