I can´t change the config with my command

I tryed to change someting in my config with a command but it only does an Error.(Sry but i m a noob ;D )

MainClass:

CommandSpec joinCommandSpec = CommandSpec.builder()
.description(Text.of(“JoinMessage”))
.permission(“testplugin.command.joinmessage”)
.arguments(GenericArguments.remainingJoinedStrings(Text.of(“message”)))
.executor(new JoinMessage(config))
.build();
game.getCommandManager().register(this, joinCommandSpec,“joinMessage”);

Executer:

public class JoinMessage implements CommandExecutor {

public CommentedConfigurationNode config;
public JoinMessage(CommentedConfigurationNode config) {
    this.config = config ;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    config.getNode("message").setValue(args);
    src.sendMessage(Text.of("Die Nachricht wurde auf " + args + " gesetzt."));
    return CommandResult.success();
}

}

You need to save your config after you changed the value if you want to get it in the file.

thx but it still does not work

Can I see your code?

MainClass:
package io.github.dommihd.testplugin;

import java.nio.file.Path;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import com.google.inject.Inject;

import io.github.dommihd.testplugin.Config.ConfigManager;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;

@Plugin(id = “testplugin”, name = “Test Plugin”, version = “1.0”)

public class TestPlugin {

public static Game game;
public static TestPlugin test;

public static TestPlugin getTestPlugin()
{
    return test;
}

@Inject
private Logger logger;

public Logger getLogger()
{
    return logger;
}


@Inject
@DefaultConfig(sharedRoot = true)
private Path defaultConfig;

@Inject
@DefaultConfig(sharedRoot = true)
private ConfigurationLoader<CommentedConfigurationNode> configLoader;

public ConfigManager configManager;

@Listener
public void onServerInit(GameInitializationEvent event)
{
    // Grab Instance of Config Manager
    configManager = ConfigManager.getInstance();

    // Setup the config, passing in the default config format builder
    configManager.setup(defaultConfig, configLoader, getLogger(),
            new ConfigManager.DefaultConfigBuilder() {
        
                @Override
                public void build(CommentedConfigurationNode config) {

                    config.getNode("message").setComment("Join message").setValue("Hallo");

                }
            });


    // Get the config file!

    CommentedConfigurationNode config = configManager.getConfig();
    
    
    getLogger().info("Plugin loading...");
    test = this;
    game = Sponge.getGame();

    CommandSpec testCommandSpec = CommandSpec.builder()
            .description(Text.of("Test Command"))
            .permission("testplugin.command.test")
            .executor(new TestCommand())
            .build();
    game.getCommandManager().register(this, testCommandSpec,"test");
    
    CommandSpec joinCommandSpec = CommandSpec.builder()
            .description(Text.of("JoinMessage"))
            .permission("testplugin.command.joinmessage")
            .arguments(GenericArguments.string(Text.of("message")))
            .executor(new JoinMessage(config))
            .build();
    game.getCommandManager().register(this, joinCommandSpec,"joinMessage");


}
@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event)
{
    CommentedConfigurationNode config = configManager.getConfig();
    Player p = event.getTargetEntity();
    p.sendMessage(Text.of(config.getNode("message").getValue()+" "+p.getName()));

}


}

Executor:

package io.github.dommihd.testplugin;

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.text.Text;

import io.github.dommihd.testplugin.Config.ConfigManager;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;

public class JoinMessage implements CommandExecutor {
public ConfigManager configManager;
public CommentedConfigurationNode config;
public JoinMessage(CommentedConfigurationNode config) {
this.config = config ;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
config.getNode(“message”).setValue(args);
configManager.saveConfig();
src.sendMessage(Text.of(“Die Nachricht wurde auf " + args + " gesetzt.”));
return CommandResult.success();
}

}

You should read this https://docs.spongepowered.org/en/plugin/configuration/loaders.html#loading-and-saving

In your execute method, replace .setValue(args) with .setValue(args.getOne(“message”).get())

Now i got a null pointer exception:
[21:24:27] [Server thread/ERROR] [Sponge]: Error occurred while executing command ‘joinmessage hi’ for source EntityPlayerMP[‘Dommi’/676, l=‘world’, x=292,00, y=137,50, z=280,00]: null
java.lang.NullPointerException
at io.github.dommihd.testplugin.JoinMessage.execute(JoinMessage.java:23) ~[JoinMessage.class:?]
at org.spongepowered.api.command.spec.CommandSpec.process(CommandSpec.java:332) ~[CommandSpec.class:1.8-1577-3.0.0-BETA-1000]
at org.spongepowered.api.command.dispatcher.SimpleDispatcher.process(SimpleDispatcher.java:331) ~[SimpleDispatcher.class:1.8-1577-3.0.0-BETA-1000]
at org.spongepowered.common.command.SpongeCommandManager.process(SpongeCommandManager.java:252) [SpongeCommandManager.class:1.8-1577-3.0.0-BETA-1000]
at net.minecraft.command.ServerCommandManager.func_71556_a(SourceFile:83) [cl.class:?]
at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:812) [rj.class:?]
at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:791) [rj.class:?]
at net.minecraft.network.play.client.C01PacketChatMessage.func_180757_a(SourceFile:37) [lu.class:?]
at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:9) [lu.class:?]
at net.minecraft.network.PacketThreadUtil$1.onProcessPacket(SourceFile:110) [ih.class:?]
at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [ih.class:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_73]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_73]
at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHandler.java:714) [FMLCommonHandler.class:?]
at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:656) [MinecraftServer.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:364) [po.class:?]
at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:598) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:478) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]

Using a CommandCallable could give you an array of args which fixes this issue.

You’re getting a null pointer exception because the field configManager is never initialized to a value