This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation error" Watch "Compilation error" New topic
Author

Compilation error

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


Client.java: error: 'void' type not allowed here
System.out.println(" "+((MenuComponent)iterator.next()).print());

My one regret in life is that I am not someone else.
- Woody Allen
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
The print method returns “void”. And you are passing void to the + operator.

Did you want to pass a String? In which case you should’t pass the print() method. You should specify that String in the toString() method and miss out the print() call.
[You can also miss out the nasty cast, and use polymorphism.]

You ought to declare the Iterator as Iterator<MenuComponent>, then you won’t need casts at all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
OR:Because of polymorphism, that will work irrespective of the types in the List.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
And you will have lots more compiler errors unless your spelling is better than mine
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

meeta gaur wrote:Client.java: error: 'void' type not allowed here
System.out.println(" "+((MenuComponent)iterator.next()).print());

Meeta,

Simply posting a piece of code with an error message is useless.

What is your question?

Do you not understand the error message? Because if not, you really need to go back to the tutorials and re-read about methods and return types.

And if it's something else, you need to TellTheDetails.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Thank you Campbell
 
jQuery in Action, 2nd edition
 
subject: Compilation error