Arjen
Arjen

Reputation: 3

Why do I get, and how do I solve this "String to object of type <objecttype>" error

I am (being an absolute beginner), trying to create a simple tool, that creates some objects and links them. The objects are: Customers Licenses (2 types, extends class)

The idea is to use (one of) the customer company name when creating a license, so the license is linked to a customer. I use ArrayLists to store the data.

I tried to use the getter for Customer cCompany, but when I try to actually create a new license object, I get errors about incompatible types (String to object of type customer)

How can I fix that error?

Any help is highly appreciated, but please explain well, me being an absolute beginner. I probably overcomplicate stuff....

Some code extracts:

From Main:

public class Main {

    public static void main(String[] args) {
        //Create customers
        List <Customer> customers = new ArrayList <> (10);
        customers.add(new Customer("TestCompany","John Doe",1234567890,"[email protected]"));


....

//Create Elvis licenses (based on superclass License)
List <ElvisLicense> ellicenses = new ArrayList <> (10);
ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));

Class: Customer:

class Customer {
    String cCompany;
    private String cName;
    private int cPhone;
    private String cEmail;

    public Customer( String cCompany, String cName,int cPhone, String cEmail)
    {
    this.cCompany = cCompany;
    this.cName = cName;
    this.cPhone = cPhone;
    this.cEmail = cEmail;
    }

    //This getter should be used to link the license to the customer (Done in License.java)
    public String getcCompany() {
        return cCompany;
    }

Class License (Superclass)

class License {
// Used no modifier to set access for Class/Package and Subclass inside the package
Customer licenseCompany;
String lVendor;
int lContractNumber;
String lCertificateNumber;
String lProductName;
String lLicenseKey;
int lNumberOfSeats;


    public License(Customer cCompany, String lVendor, int lContractNumber, String lCertificateNumber, 
            String lProductName, String lLicenseKey, int lNumberOfSeats)
    {
    licenseCompany = cCompany;
    this.lVendor = lVendor;
    this.lVendor = lVendor;
    this.lContractNumber = lContractNumber;
    this.lCertificateNumber = lCertificateNumber;
    this.lProductName = lProductName;
    this.lLicenseKey = lLicenseKey;
    this.lNumberOfSeats = lNumberOfSeats;    
    }


    public Customer getLicenseCompany() {
        return licenseCompany;
    }

    public void setLicenseCompany(Customer licenseCompany) {
        this.licenseCompany = licenseCompany;
    }


//preparations to allow for example printing the content of an arraylist element
    @Override
    public String toString(){
    return "Customer name " + getLicenseCompany()  + "\n" + "Vendor name " + getlVendor()  + "\n" + "Contract number: " + getlContractNumber() + "\n"
               + "Certificate number: " + getlCertificateNumber() + "\n" + 
                "Product name " + getlProductName()  + "\n" + "Licence key: " + getlLicenseKey() + "\n"
               + "Number of seats: " + getlNumberOfSeats();
}


}

And the extended class:

public class ElvisLicense extends License{

private boolean elIsBundle;
private boolean elIsSubscription;


public ElvisLicense(
        Customer licenseCompany,
        String lVendor,
        int lContractNumber,
        String lCertificateNumber, 
        String lProductName,
        String lLicenseKey,
        int lNumberOfSeats,
        boolean elIsBundle,
        boolean elIsSubscription
        )

    {
    super(
            licenseCompany,
            lVendor,
            lContractNumber,
            lCertificateNumber,
            lProductName,
            lLicenseKey,
            lNumberOfSeats);

    this.elIsBundle = elIsBundle;
    this.elIsSubscription = elIsSubscription;
    }  


.....



@Override
public String toString(){
    return "Customer name " + licenseCompany  + "\n" 
            + "Vendor name " + lVendor  + "\n" 
            + "Contract number: " + lContractNumber + "\n"
            + "Certificate number: " + lCertificateNumber + "\n" 
            + "Product name " + lProductName  + "\n" 
            + "Licence key: " + lLicenseKey + "\n"
            + "Number of seats: " + lNumberOfSeats + "\n"
            + "Number of seats: " + elIsBundle + "\n" 
            + "Number of seats: " + elIsSubscription;
}

}

I expect that the Customername is used when creating a new license.

Upvotes: 0

Views: 70

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51892

What you need to do is to use one of the Customer objects you have already created when creating the ElvisLicense object. To more easily find that customer by name I suggest you store them in a map instead of a list with the name as a key.

Map<String, Customer> customerMap = new HashMap<>();
Customer customer = new Customer("TestCompany","John Doe",1234567890,"[email protected]"));
customerMap.put(customer.getcCompany(), customer);

so when creating the license you look up the customer

List <ElvisLicense> ellicenses = new ArrayList <> (10);
Customer customer = customerMap.get("TestCompany");
if (customer != null) {
    ElvisLicense license = new ElvisLicense(customer,"VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));
    ellicenses.add(license);
} else {
   //If the customer isn't found you need some kind of error handling, better than below :)
   System.out.println("Can't create a license, no customer found");
}

Upvotes: 0

Qingfei Yuan
Qingfei Yuan

Reputation: 1212

Below line is wrong.

ellicenses.add(new ElvisLicense("TestCompany","VendorA",1234,"1234-A","Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));

As license need customer object an parameter. Instead, you should create customer object first.

ellicenses.add(new ElvisLicense(new Customer("TestCompany","VendorA",1234,"1234-A"),"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true));

for reusing that customer list to avoid create company.

for(Customer customer : customers){
   // here you need some way to offer other parameters except customer parameter.
   License license = new new ElvisLicense(customer,"Solutions Server gold","1234-dtbk-87654-nlof",10, true , true);
   ellicenses.add(license);
}

Upvotes: 1

Related Questions