• 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

What is the error?

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The error messages generated
[ITALICS]
G:\JavaCertification\SCJP\SCJPExamGuide\PaulShanghara\RobotManager.java:16: non-static variable this cannot be referenced from a static context
robot = new Robot();
^
G:\JavaCertification\SCJP\SCJPExamGuide\PaulShanghara\RobotManager.java:17: cannot find symbol
symbol : method setName(java.lang.String)
location: class RobotManager
robot = setName(args);
^
2 errors

Tool completed with exit code 1

[/ITALICS]
[ DESCRIPTION ]
The error messages generated express about robot variable not defined within a non static context But I have defined it inside the main method.[I]This code is from chapter1 of the SCJP study guide of Paul Shanghara.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two completely separate errors here. The first one happens because your "Robot" class is nested inside your RobotManager class (pay close attention to your opening and closing braces to see this.) Move class Robot entirely outside of RobotManager's enclosing braces to fix the problem.

The second error is happening because your "main" method is calling a setName() method as if it were a member of RobotManager, but it's not. To call setName() from RobotManager, there must be an instance of class Robot, and you must use the "dot" operator to call the method on that instance -- i.e.,

robot.setName(args[0]);

Finally, note that the argument to setName() is supposed to be a single String, not an array.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Varuna,

Is there a specific reason you defined the Robot class as an inner class of RobotManager?

If that is the case, then you should review the Java Tutorial on nested classes to understand why you're getting the first error on compilation.

The second error is the result of incorrect syntax, as well.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic