Results 1 to 2 of 2
  1. #1
    MikeB1955 is offline Member
    Join Date
    Nov 2011
    Location
    San Jose, CA USA
    Posts
    1
    Rep Power
    0

    Default Non-static method ... cannot be referenced from a static context

    Hello,

    Let me preface this by saying that I know very little about Java and the J2ME but I have been a programmer for over 20 years (assemblers and 'C') so I am not untechnical.

    I hope someone can help me with a problem that I am constantly running into. I am trying to learn Java and the J2ME and write my first applet but I am constantly running into a problem that says "Non-static method such-and-such(stuff) cannot be referenced from a static context.

    1. What exactly does this mean?

    2. How do I get around this problem.

    Here is a short example...

    ------------------------------------
    Java Code:
    package MyMidlet;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class MyMidlet extends MIDlet implements CommandListener {
    
        private midletLog log = new midletLog();
        private myClass mine = new myClass();
        private Command exitCommand;
        private Display display;
    
        public WorkScheduleMIDlet() {
            display = Display.getDisplay(this);
            exitCommand = new Command("Exit", Command.EXIT, 0);
        }
    
        public void startApp() {
            mine(log, "Creating textbox...");
            TextBox t = new TextBox("My Midlet", "Sample textbox", 256, 0);
            t.addCommand(exitCommand);
            t.setCommandListener(this);
    
            display.setCurrent(t);
            log.show(display);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    
        public void commandAction(Command c, Displayable s) {
            if (c == exitCommand) {
                destroyApp(false);
                notifyDestroyed();
            } 
        }
    }
    ------------------------------------
    Java Code:
    package MyMidlet;
    
    public class MyClass {
        public  String myData;
    
        public  MyClass() {
            myData = ""
            return;
        }
    
        public void myFunc(midletLog log, String text) {
            log.append(text);
            return;
        }
    }
    ------------------------------------
    Java Code:
    package MyMidlet;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class midletLog implements CommandListener {
        private Form mainForm;
        private Command exitCommand;
    
        public  midletLog() {
            mainForm = new Form("2BS Work Schedule Log");
            exitCommand = new Command("Back", Command.BACK, 1);
            mainForm.addCommand(exitCommand);
            mainForm.setCommandListener(this);
            return;
        }
    
        public void append(String textLine) {
            mainForm.append(textLine + "\n");
            return;
        }
    
    
        public  void show(Display display) {
            display.setCurrent(mainForm);
            return;
        }
    
    
        public void commandAction(Command c, Displayable s) {
            if (c == exitCommand) {
    		<unimportant code>
            }
        }
    }
    ------------------------------------

    One another note, where can I load a Java JDK 7 Class/API reference and a J2ME 3 Class/API reference (either .chm or .pdf)?


    Thanks,

    MikeB
    Last edited by pbrockway2; 11-17-2011 at 06:16 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Non-static method ... cannot be referenced from a static context

    Welcome!

    When you post code, use the "code" tags: put [code] at the start of the code and [/code] at the end. This will keep the code readable. (It also makes things more legible if you keep to standard Java coding conventions: variables, methods and packages all start with a lower case letter, and classes with an uppercase one. This will also mean there can be no confusion between a the class and the package both of which you call "MyMidlet".)

    Could you post the actual compiler messages? (and say which lines of your code they refer to)
    Last edited by pbrockway2; 11-17-2011 at 06:26 AM.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  2. Replies: 4
    Last Post: 04-16-2011, 05:15 PM
  3. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  4. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 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
  •