File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes i don't get to know why this program cannot be executed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i don Watch "i don New topic
Author

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

rahul aditya
Greenhorn

Joined: Mar 14, 2012
Posts: 9


[Edit - added code tags - see UseCodeTags for details]
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
How did you try to execute it and what error messages did you get?
rahul aditya
Greenhorn

Joined: Mar 14, 2012
Posts: 9
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

Joined: Mar 17, 2012
Posts: 761
    
    9
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

Joined: Mar 14, 2012
Posts: 9
sorry, but i use net beans.. is there any compile option in net beans?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
rahul aditya
Greenhorn

Joined: Mar 14, 2012
Posts: 9
yes, i couldn't find out what the error is? so i posted it here..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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

Joined: Mar 14, 2012
Posts: 9
it says non-static content this cannot be referenced from static content
rahul aditya
Greenhorn

Joined: Mar 14, 2012
Posts: 9
at lines 27 & 28.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: i don't get to know why this program cannot be executed?
 
Similar Threads
Compiling multiple source files.
please solve my query
error pg 89 of K&b 5.0 study guide
K&B book code example question..
Class Casting