Results 1 to 2 of 2
- 11-17-2011, 05:36 AM #1
Member
- Join Date
- Nov 2011
- Location
- San Jose, CA USA
- Posts
- 1
- Rep Power
- 0
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,
MikeBLast edited by pbrockway2; 11-17-2011 at 06:16 AM. Reason: code tags added
- 11-17-2011, 06:21 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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
-
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
non-static method cannot be referenced from a static context
By yma16 in forum New To JavaReplies: 4Last Post: 04-16-2011, 05:15 PM -
Non-static method cannot be referenced from a static context
By GRoss in forum New To JavaReplies: 7Last Post: 05-19-2010, 11:12 AM -
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks