Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the error?

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic