It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Exercise of he book Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exercise of he book Head First Java" Watch "Exercise of he book Head First Java" New topic
Author

Exercise of he book Head First Java

Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Please can someone help me

I try to compile the code with the netbeans compiler



I got an error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context
at javaapplication2.Main.main(Main.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

It's the code of the book of head first java. can someone tell me what I am doing wrong?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
The code you posted is not what is in Head First Java. You have the Book and BooksTestDrive classes as inner classes to Main, which is not how it is on pg. 63, and I'm not even sure what you've posted will compile because you are missing a }.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Hi Joe,
I saw I missed a }
on page 68 of the second edition is the solution of the problem.
Now I got another error can you help me?



the error is : java.lang.NoSuchMethodError: main
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I'm still confused as to why you have everything wrapped in a class called Main. It isn't needed, nor is it in the book. I'm not trying to be mean or anything, just trying to understand your thinking.

As for your error, from the command line run it as java BooksTestDrive. You should run the class that has the main method in it.
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Hi Joe,
can you give me a clue with the main how you would programm this ?
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Hi Joe,
can you give me a clue with the main how you would programm this ?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Normal javaranch policy is to not give out code, but since this is from the book you are using, here is the code from HFJ:



From there, I'd just compile it using javac <FileName>.java, then run it using java BooksTestDrive.
Timothy Onggowasito
Greenhorn

Joined: Apr 16, 2010
Posts: 11
I think it has something to do with netbean. They ussualy added the main class in the beginning as a template.
Try using Dr. Java as a compiler. It's much simpler to use.

Apparently, what you don't know can hurt you...
Tim Ong
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11164
    
  16

If you're just learning how to write java code, throw away netbeans entirely and use the command line to compile.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
I also think that netbeans is the problem. I will try Dr Java
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Command Line, not Dr Java, please.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exercise of he book Head First Java
 
Similar Threads
Java Arrays to display String
While loop - Head's First Java example
Problem with the main class!
question from HFJ
Error message