This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the error?" Watch "What is the error?" New topic
Author

What is the error?

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167


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.


Varuna Seneviratna
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


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

Joined: Mar 22, 2008
Posts: 265

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.


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?
 
Similar Threads
Doubt in return value
SCJP Brainteaser (1)
SCJP Difficulty
help with easy code
compiler error: cannot find symbol