Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation error

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Client.java: error: 'void' type not allowed here
System.out.println(" "+((MenuComponent)iterator.next()).print());
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR:Because of polymorphism, that will work irrespective of the types in the List.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you will have lots more compiler errors unless your spelling is better than mine
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic