Why is getState() returning null?
Hi Community,
I tried to persist some configuration values. I followed the instructions of the documentation http://confluence.jetbrains.com/display/IDEADEV/Persisting+State+of+Components
I created a Service like this:
@State(name = "ConfigurationServiceImpl",
storages = {
@Storage(id = "main", file = StoragePathMacros.APP_CONFIG + "tdaconfig.xml")
}
)
public class ConfigurationServiceImpl implements ConfigurationService, PersistentStateComponent<ConfigurationServiceImpl.State> {
class State {
public Set<String> excludedClassNames = new HashSet<String>();
}
public State configurationState;
@Override
public void addExcludedClassName(String excludedClassName) {
getState().excludedClassNames.add(excludedClassName);
}
@Override
public void clearExcludedClassNames() {
getState().excludedClassNames.clear();
}
@Override
public boolean isClassExcluded(TDAClass tdaClass) {
for (String excludedClassName : getState().excludedClassNames) {
if (tdaClass.getName().equals(excludedClassName)) {
return true;
}
}
return false;
}
@Override
public Set<String> getExcludedClassNames() {
return getState().excludedClassNames;
}
@Nullable
@Override
public State getState() {
return configurationState;
}
@Override
public void loadState(ConfigurationServiceImpl.State state) {
configurationState = state;
}
}
The problem that I have is if I try to use the getExcludedClassNames() Method I get a NullPointerException. I tried to use the ConfigurationService with the stored data in my ExcludedClasses configurable.
<applicationService serviceInterface="analyzer.service.ConfigurationService"
serviceImplementation="analyzer.service.impl.ConfigurationServiceImpl">
</applicationService>
<applicationConfigurable instance="analyzer.configuration.ExcludedClasses"/>
I don't see what I'm doing wrong.
I Think the loadState Method is never called, but I don't know why.
Markus
Please sign in to leave a comment.
It's returning null because there's nothing else it could return. You aren't initializing the state in the constructor (which is what you need to do), and the loadState() method is not called because there is no persisted state.
So I have to initialize the State in the constructor like this?
If I do this I get another error:
org.picocontainer.defaults.UnsatisfiableDependenciesException:analyzer.service.impl.ConfigurationServiceImpl has unsatisfied dependency: class analyzer.service.impl.ConfigurationServiceImpl$State among unsatisfiable dependencies: [[class analyzer.service.impl.ConfigurationServiceImpl$State]] where AreaPicoContainer.MyPicoContainer[null] was the leaf container being asked for dependencies.
No, you need to initialize it by creating a new State() instance. There is no one except you who knows what the initial state of your component shoud be; there is no way for the platfrom to provide this data to your component.