Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Void type not allowed

 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Not sure why I am getting a void type not allowed error here. All I want to do is print off a line pulling into from my other class.
 
Kristin Stromberg
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again. What is the return type of your numberOfMovies() method? My guess is it's void, when it should be a String. Hope this helps...
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I don't want anything to return, I just want it to print out.
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just looked back at my code for my class MovieCollection and numberOfMovies is a void type.
 
Kristin Stromberg
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is you're calling the numberOfMovies() method in your print statement, and unless the numberOfMovies() method returns a string, you'll get the error you're seeing. It's okay if your runTest() method returns void.

Alternatively, maybe you don't have a numberOfMovies() method at all but a numberOfMovies instance variable instead? In that case, you just need to remove the parentheses like this:
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I double checked my methods and it is definately a method with a void data type.

Is there anyway I can call a method that has void return?

Here is the method I am trying to call...
 
Kristin Stromberg
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, you just call the method but don't try to embed the call in a print statememnt. The method handles the printing for you. So instead of this



you would do this:

 
Arul Prasad
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kristin


The problem is you're calling the numberOfMovies() method in your print statement, and unless the numberOfMovies() method returns a string, you'll get the error you're seeing. It's okay if your runTest() method returns void.


for ur information numerOfMovies() can return any data type not only String
it can return Float,Integer...etc, Object or any primitive data type
println() method will work since this method inturn will call the Object.toString()

 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa, As suggested by Kristin - following line of code will print required message:

[/qb]<hr></blockquote>

You can only call a method within the "println" method if it is returning anything. The value being returned is then concatenated to the rest of the string (if any) in the "println" method.

For example, if your numberOfMovies() method was returning the count, your original line of code would have copiled with no errors.

Saeed
[ August 10, 2005: Message edited by: Saeed Amer ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like the following should do:

System.out.print("The MovieCollection has ");
movieCollection.numberOfMovies();
System.out.println(" movies.");
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your code, the best approach would be



Since your code really is updating the number of movies, you might want to change your method name to updateMovieCollection() to cause less confusion.
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone, Thank you!

As suggested, I will use Kristin's advice. Thanks Kristin!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic