Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Incompatible types?

Thread view: 
ste1986 - 08 Jan 2006 19:43 GMT
im getting the error message "Incompatible types - found Volunteer but
expected java.lang.String" why is this. the error occurs in the
assignPerson method below.

public class Person
{
    // instance variables
    public String name;
    public String address;
    public int telephone;
    public boolean withVolunteer;
    public String volunteer;
   
    public Person (String name, String address, int telephone)
    {
       this.name = name;
       this.address = address;
       this.telephone = telephone;
    }

    /**
    * Method to get the persons name.
    * @return     name
    */
    public String getName()
    {
        return name;
    }
   
    /**
    * Method to get a persons information.
    *
    * @return     name, address, telephone
    */
    public String getDescription()
    {
       return name + "\t" + address + "\t" + telephone;
    }
   
    public void assignPerson(Volunteer volunteer)
    {
       withVolunteer = true;
       this.volunteer = volunteer; //ERROR
    }
   
       
       
}
Juha Laiho - 08 Jan 2006 19:56 GMT
"ste1986" <steiddon_1@hotmail.com> said:
>im getting the error message "Incompatible types - found Volunteer but
>expected java.lang.String" why is this. the error occurs in the
[quoted text clipped - 3 lines]
>{
>    // instance variables
...
>    public String volunteer;

Here you define that the Person class has a field with name volunteer
(and of type String).

...
>    public void assignPerson(Volunteer volunteer)
>    {
>       withVolunteer = true;
>       this.volunteer = volunteer; //ERROR
>    }

And here you have a method with parameter of type Volunteer (named
volunteer), which you're trying to assign to the field of type String.

Apparently Volunteer is not a subclass of String (and how could it, as
java.lang.String is "final" - i.e. subclassing of it is not allowed.

To fix, either change also the field to type Volunteer, or the method
parameter to type String (whichever suits better to your intended uses).
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Thomas Hawtin - 08 Jan 2006 20:09 GMT
> im getting the error message "Incompatible types - found Volunteer but
> expected java.lang.String" why is this. the error occurs in the
[quoted text clipped - 10 lines]
>        this.volunteer = volunteer; //ERROR
>     }

Because you have assigned an expression of type Volunteer (the local
variable, volunteer) to a variable of type String (the member variable,
volunteer).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.