• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Head First early chapters..  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So hello, everyone. First of all sorry for my english. I began Java only a few days ago and already have some problems.




This is my code. I understand the code mostly, however i can't compile it.  I get the following error message:

SimpleDotComTestDrive.java:26: error: non-static variable this cannot be referenced from a static context
SimpleDotCom theDotCom = new SimpleDotCom();

I would like to know why does the compiler tells me this error message. I have some other questions as well. Why is the locations array for the compiler the same as the locs array; the userGuess as the stringGuess. I tried to replace them and it worked fine (in a previous version).
 
Marshal
Posts: 60130
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please indent the code correctly. Make sure there are no }s above each other (e.g. lines 16‑17). Make sure that everything after a { moves to the right. Line 3 is not to the right of line 2. Line 8 is to the left of line 7. Make sure to use the same number of spaces throughout, which you should use a decent text editor to achieve, not MS NotePad. My favourite for Windows® is called NotePad++. If you look at line 55, it is 4 spaces right of line 54, and line 48 is 3 spaces right of line 46, and line 7 is 2 spaces right of line 6. Make sure everything after a } moves to the left. I don't like formatting like } catch (XYZException ex) {
Now, you will be wondering why I am going on about formatting? Isn't the code more important? The formatting makes the structure of the code visible. I cannot see where one part ends and the next begins. It is quite possible that you have counted { and } wrongly. I think you should write backwards when coding: look at this old post. The indentation allows you to see the structure of the code (again), and bad indentation can hide serious problems with the code. It can even hide when you have one class inside another, and I suspect that might be the cause of your problem.
 
M. Gumblert
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer, I will rewrite the code and post it throughout the day again if it doesnt work. have a nice day!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!