File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i don't get to know why this program cannot be executed?

 
rahul aditya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[Edit - added code tags - see UseCodeTags for details]
 
E Armitage
Rancher
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you try to execute it and what error messages did you get?
 
rahul aditya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
at bert.GameShape.main(GameShape.java:40)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
this was the error i got, could you please help me find out the problem?
 
E Armitage
Rancher
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be trying to run the code before compiling it first.
Also, I doubt that the code you posted is all the code you tried to run.
 
rahul aditya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, but i use net beans.. is there any compile option in net beans?
 
Winston Gutkowski
Bartender
Pie
Posts: 9476
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rahul aditya wrote:sorry, but i use net beans.. is there any compile option in net beans?

As I recall, Netbeans (like Eclipse) compiles on the fly, so you probably have a few red markers in your text.

Winston
 
rahul aditya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i couldn't find out what the error is? so i posted it here..
 
Winston Gutkowski
Bartender
Pie
Posts: 9476
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rahul aditya wrote:yes, i couldn't find out what the error is? so i posted it here..

Well usually, if you click (or hover) on the offending line, it'll tell you precisely what the error is (and maybe suggest ways to correct the problem).

If you still have problems, post the message you don't understand here (the whole message; exactly as it was given to you).

Winston
 
rahul aditya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it says non-static content this cannot be referenced from static content
 
rahul aditya
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at lines 27 & 28.
 
Winston Gutkowski
Bartender
Pie
Posts: 9476
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rahul aditya wrote:it says non-static content this cannot be referenced from static content

And you still can't see what the problem is?

Look at your class definitions. What you have defined are inner classes, which is almost certainly NOT what you want (I can still count the number of inner classes I've defined on my fingers after 11 years of Java).

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your classes PlayerPiece and TilePiece are inner classes, defined inside the GameShape class.

An instance of an inner class needs a reference to an instance of its enclosing class. You need an instance of GameShape to create the instances of PlayerPiece and TilePiece on in lines 27 and 28. Since the main method is static, you don't have an implicit instance of GameShape to create those instances on.

In this example, there is no reason why the classes PlayerPiece and TilePiece should be inner classes. Move them out into their own source files, as top-level classes.

Another way to solve this is to make PlayerPiece and TilePiece nested classes instead of inner classes. A third way would be to change your main method to create an instance of GameShape first, and then to create the PlayerPiece and TilePiece instances on the instance of GameShape. But since there isn't a good reason to make PlayerPiece and TilePiece inner classes, I think making them top-level classes would be the best solution here.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic