This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exercise of he book Head First Java

 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 }.
 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
can you give me a clue with the main how you would programm this ?
 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
can you give me a clue with the main how you would programm this ?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just learning how to write java code, throw away netbeans entirely and use the command line to compile.
 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think that netbeans is the problem. I will try Dr Java
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Command Line, not Dr Java, please.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic