jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes What is the error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the error?" Watch "What is the error?" New topic

What is the error?

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 169

The error messages generated
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

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.

Varuna Seneviratna
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.,


Finally, note that the argument to setName() is supposed to be a single String, not an array.

[Jess in Action][AskingGoodQuestions]
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


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.

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
I agree. Here's the link: http://aspose.com/file-tools
subject: What is the error?
It's not a secret anymore!