PersistentStateComponent no XML found

Answered

I followed the settings tutorial https://jetbrains.org/intellij/sdk/docs/tutorials/settings_tutorial.html

But when I run my plugin and go to the settings panel I get a NullPointerException in my Configuration class.

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.ui.JBColor;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Supports storing the application settings in a persistent way.
* The State and Storage annotations define the name of the data and the file name where
* these persistent application settings are stored.
*/
@State(
name = "PluginSettingsState",
storages = {@Storage("CDDPluginSettings.xml")}
)
public class PluginSettingsState implements PersistentStateComponent<PluginSettingsState> {

public JBColor highlighterColor = (JBColor) JBColor.MAGENTA;
public int cyclesLength = 5;

public static PluginSettingsState getInstance(){
return ServiceManager.getService(PluginSettingsState.class);
}

@Nullable
@Override
public PluginSettingsState getState() {
return this;
}

@Override
public void loadState(@NotNull PluginSettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
}
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
* Provides controller functionality for application settings.
*/
public class PluginConfigurationClass implements Configurable {

private PluginSettingsComponent mySettingsComponent;

// A default constructor with no arguments is required because this implementation
// is registered as an applicationConfigurable EP

@Nls(capitalization = Nls.Capitalization.Title)
@Override
public String getDisplayName() {
return "CDD Plugin Settings";
}

@Override
public JComponent getPreferredFocusedComponent() {
return mySettingsComponent.getPreferredFocusedComponent();
}

@Nullable
@Override
public JComponent createComponent() {
mySettingsComponent = new PluginSettingsComponent();
return mySettingsComponent.getPanel();
}

@Override
public boolean isModified() {
PluginSettingsState settings = PluginSettingsState.getInstance();
boolean modified = mySettingsComponent.getCyclesLength() != settings.cyclesLength;
modified |= !mySettingsComponent.getHighlighterColor().equals(settings.highlighterColor);
return modified;
}

@Override
public void apply() throws ConfigurationException {
PluginSettingsState settings = PluginSettingsState.getInstance();
settings.cyclesLength = mySettingsComponent.getCyclesLength();
settings.highlighterColor = mySettingsComponent.getHighlighterColor();
}

@Override
public void reset() {
PluginSettingsState settings = PluginSettingsState.getInstance();
mySettingsComponent.setCyclesLength(settings.cyclesLength);
mySettingsComponent.setHighlighterColor(settings.highlighterColor);
}

@Override
public void disposeUIResources() {
mySettingsComponent = null;
}
}

My settings object is null because there is no XML file to load from. So how do I create the xml file on startup? Or did I miss something following the tutorial?
Thanks in advance!

2 comments
Comment actions Permalink

Just to be on the safe side you did not forget to declare the <applicationService/> by any changes in the plugin.xml? I guess  ServiceManager.getService() would return null then.

Johan

 

<extensions defaultExtensionNs="com.intellij">
  <applicationService serviceImplementation="???your package???.PluginSettingsState"/> 
</extensions>
1
Comment actions Permalink

Thank you! I actually did forget to declare the service in the plugin.xml

0

Please sign in to leave a comment.