Expecto
Expecto

Reputation: 521

Can't be resolved to a type

I have a service interface that reads thus

package services;

import domain.Items;

public interface IItemsService extends IService {

public final String NAME = "IItemsService";


/** Places items into the database */
public void storeItem(Items items);




/** Retrieves items from the database
 * 
 * @param category
 * @param amount
 * @param color
 * @param type
 * @return
 * @throws ClassNotFoundException 
 */
public Items getItems (String category, float amount, String color, String type) throws ItemNotFoundException, ClassNotFoundException;

}

And a factory that looks like this...

package services;
public class Factory {



public Factory(){}



@SuppressWarnings("unchecked")
public IService getService(String name) throws ServiceLoadException {
    try {
        Class c = Class.forName(getImplName(serviceName));
        return (IService)c.newInstance();
    } catch (Exception e) {
        throw new ServiceLoadException(serviceName + "not loaded");
    }
}



private String getImplName (String name) throws Exception {
    java.util.Properties props = new java.util.Properties();
        java.io.FileInputStream fis = new java.io.FileInputStream("properties.txt");
            props.load(fis);
                fis.close();
                return props.getProperty(serviceName);
}
}

This should be really simple - but I keep getting two errors - IService cannot be resolved to a type (on both the factory and the service) and also an serviceName cannot be resolved into a variable error in the factory. I know I am missing something simple...

Upvotes: 1

Views: 5262

Answers (2)

wchargin
wchargin

Reputation: 16047

Regarding the type error: if IService is not in the package services, then IItemService will not have access to it and you will need to import some.package.name.IService.

The second answer is simple: in the getService and getImplName methods, the parameter is named name. In the method bodies, you're referring to it as serviceName. You should probably change name to serviceName in each of them.

If, on the other hand, you're trying to access an instance variable, note that in Java you have to declare all instance variables before you can access (read from/write to) them. You would need to modify the class as such:

public class Factory {

    private String serviceName;

    public Factory () {}

    // other code
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503290

Well the second error is simple: there isn't a variable called serviceName. If you think there is, point to its declaration. I suspect you meant to use the parameter - in which case just change the parameter name to match the name you're trying to use:

public IService getService(String serviceName) throws ServiceLoadException 

...

private String getImplName (String serviceName) throws Exception

This was a pretty simple error - you should think about what bit of your diagnosis let you down in working it out for yourself. (You should also indent your code more sensibly, btw.)

As for IService being missing - we have no idea where IService is meant to be declared, which makes it very hard to help you on this front...

Upvotes: 1

Related Questions