Results 1 to 4 of 4
  1. #1
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Non static method cannot be referenced in static context

    I have a non static method that I want to call in the main method of my application.
    I'm getting this error: Non static method cannot be referenced in static context.
    How can I fix this?
    Thanks in advance!
    This is the code for the method I want to call in public static void main:
    Java Code:
    public void sendData(){
            while(true){
                    if(lb7.getText()==batteryStatus.getBatteryLifePercent2()){
                iface.QWrite(envio1, 1, 1000);
            }
            else if(lb8.getText()==batteryStatus.getBatteryLifePercent2()){
                iface.QWrite(envio2, 1, 1000);
            }
            }
    
        }

  2. #2
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Non static method cannot be referenced in static context

    Make sendData method static

  3. #3
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Non static method cannot be referenced in static context

    Now I am calling that method after the init components, but now the gui does not show.
    Can someone please help me?
    Thanks!

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

    Default Re: Non static method cannot be referenced in static context

    I wouldn't recommend that. Perhaps sendData needs to reference instance fields. What then? Normally, to combat this problem
    I usually have one statement in my main class which invokes a non-static startup routine.

    Java Code:
    public class Foo {
        public static void main(String[] args) {
             new Foo().startUp();
        }
    
        public void startUp() {
         // do rest of stuff
         // here.
        }
    }
    If you need to access the args, just pass them to startUp.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

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