• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[Edit - added code tags - see UseCodeTags for details]
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you try to execute it and what error messages did you get?
 
rahul aditya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, but i use net beans.. is there any compile option in net beans?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i couldn't find out what the error is? so i posted it here..
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it says non-static content this cannot be referenced from static content
 
rahul aditya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at lines 27 & 28.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic