• 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

Exercise of he book Head First Java

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think that netbeans is the problem. I will try Dr Java
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Command Line, not Dr Java, please.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic