Solved NullPointerException, can't find where.

Discussion in 'BungeeCord Plugin Development' started by TheKillerMC-2216, Sep 30, 2019.

Thread Status:
Not open for further replies.
  1. Hi! I'm making a commend plugin, and the config wasn't updating, I wondered if it was a static access issue, made a getInstance() method, and when I ran the command again, the log split this out:


    Code (Text):
    21:29:34 [WARNING] Error in dispatching command
    java.lang.NullPointerException
        at com.shadowhawkcoding.commends.commands.CommendCommand.execute(CommendCommand.java:47)
        at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:189)
        at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:142)
        at net.md_5.bungee.connection.UpstreamBridge.handle(UpstreamBridge.java:145)
        at net.md_5.bungee.protocol.packet.Chat.handle(Chat.java:50)
        at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:104)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:297)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)
        at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:337)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1408)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:345)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930)
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:677)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:612)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:529)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:491)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:905)
        at java.lang.Thread.run(Unknown Source)

    This is the code:
    Code (Java):
    package com.shadowhawkcoding.commends;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.UUID;

    import com.shadowhawkcoding.commends.commands.CommendCommand;
    import com.shadowhawkcoding.commends.commands.CommendsCommand;

    import net.md_5.bungee.BungeeCord;
    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Plugin;
    import net.md_5.bungee.api.plugin.PluginManager;
    import net.md_5.bungee.config.Configuration;
    import net.md_5.bungee.config.ConfigurationProvider;
    import net.md_5.bungee.config.YamlConfiguration;

    public class Commends extends Plugin {
        public Configuration config;
        public CommandSender console = BungeeCord.getInstance().getConsole();
        public static Commends inst = new Commends();
        public static Commends getInstance() {
            return inst;
        }
        public boolean addCommend(UUID target, ProxiedPlayer sender, String reason) {
            Configuration c = config.getSection("users." + sender.getUniqueId().toString());
            int countLast24 = 0;
            for (String str : c.getStringList("commended")) {
                if ((Long.parseLong(str.split(";")[1]) + 86400000l) > System.currentTimeMillis()) {
                    countLast24++;
                }
            }
            if (countLast24 > config.getInt("settings.max-commends-per-24"))
                return false;
            c.getStringList("commended").add(target.toString() + ";" + System.currentTimeMillis() + ";" + reason);
            c = config.getSection("users." + target.toString());
            c.getStringList("commendations").add(sender.getUniqueId().toString() + ";" + System.currentTimeMillis() + ";" + reason);
            return true;
        }

        @Override
        public void onEnable() {
            File configFile = new File(getDataFolder(), "config.yml");
            if(!getDataFolder().exists())
                getDataFolder().mkdir();
            if (!configFile.exists()) {
                try {
                    configFile.createNewFile();
                    try (InputStream is = getResourceAsStream("config.yml");
                            OutputStream os = new FileOutputStream(configFile)) {
                     
                    }
                } catch (IOException e) {
                    throw new RuntimeException("Unable to create configuration file", e);
                }
            }
            try {
                config = ConfigurationProvider.getProvider(YamlConfiguration.class)
                        .load(new File(getDataFolder(), "config.yml"));
            } catch (IOException e) {
                console.sendMessage(new TextComponent(String.format(
                        "%sPunish > %s%sThe configuration file could not be found or loaded. The plugin will not load.",
                        ChatColor.BLUE, ChatColor.RED, ChatColor.BOLD)));
                e.printStackTrace();
                onDisable();
                return;
            }
         
            PluginManager pm = this.getProxy().getPluginManager();
            pm.registerCommand(this, new CommendCommand());
            pm.registerCommand(this, new CommendsCommand());
        }
    }
     

    And here is the one for the command:

    Code (Java):
    package com.shadowhawkcoding.commends.commands;

    import com.shadowhawkcoding.commends.Commends;

    import net.md_5.bungee.BungeeCord;
    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Command;

    public class CommendCommand extends Command {

        public CommendCommand() {
            super("commend");

        }

        @Override
        public void execute(CommandSender s, String[] args) {
            if (!(s instanceof ProxiedPlayer)) {
                s.sendMessage(TextComponent.fromLegacyText(
                        String.format("%sCommends > %sYou must be a player to do this!", ChatColor.BLUE, ChatColor.RED)));
                return;
            }
            ProxiedPlayer p = (ProxiedPlayer) s;
            if (!p.hasPermission("commends.use")) {
                p.sendMessage(TextComponent.fromLegacyText(String.format("%sPermissions > %sYou don't have permission to do this!",
                        ChatColor.BLUE, ChatColor.RED)));
                return;
            }  
            if (args.length < 2) {
                p.sendMessage(TextComponent.fromLegacyText(
                        String.format("%sCommends > %sUsage: /commend <player> <reason>", ChatColor.BLUE, ChatColor.GOLD)));
                return;
            }
            if (BungeeCord.getInstance().getPlayer(args[0]) == null) {
                p.sendMessage(TextComponent.fromLegacyText(
                        String.format("%sCommends > %sThat player does not exist.", ChatColor.BLUE, ChatColor.RED)));
                return;
            }
            ProxiedPlayer pl = BungeeCord.getInstance().getPlayer(args[0]);
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < args.length; i++) {
                sb.append(args[i]).append(" ");
            }
            if (!Commends.getInstance().addCommend(pl.getUniqueId(), p, sb.toString().trim())) {
                p.sendMessage(
                        TextComponent.fromLegacyText(
                                String.format(
                                        "%sCommends > %sYou can only send "
                                                + Commends.getInstance().config.getInt("settings.max-commends-per-24") + " per day.",
                                        ChatColor.BLUE, ChatColor.RED)));
                return;
            } else {
                Commends.getInstance().addCommend(pl.getUniqueId(), p, sb.toString().trim());
                p.sendMessage(TextComponent.fromLegacyText(String.format(
                        "%sCommends > %sSuccessfully commended %s%s%s %s%sfor %s%s%s%s%s!", ChatColor.BLUE, ChatColor.GREEN,
                        ChatColor.GOLD, ChatColor.BOLD, pl.getName(), ChatColor.RESET, ChatColor.GREEN, ChatColor.GOLD,
                        ChatColor.BOLD, sb.toString().trim(), ChatColor.RESET, ChatColor.GREEN)));
                if (!pl.hasPermission("commends.noshowcommends")) {
                    pl.sendMessage(TextComponent.fromLegacyText(String.format(
                            "%sCommends > %s%s%s %s%sjust commended you for %s%s%s%s%s!", ChatColor.BLUE, ChatColor.GOLD,
                            ChatColor.BOLD, p.getName(), ChatColor.RESET, ChatColor.GREEN, ChatColor.GOLD, ChatColor.BOLD,
                            sb.toString().trim(), ChatColor.RESET, ChatColor.GREEN)));
                }
             
            }

        }

    }
     

    Thanks!
     
    #1 TheKillerMC-2216, Sep 30, 2019
    Last edited: Sep 30, 2019
  2. Your error is here:
    Code (Text):
    java.lang.NullPointerException
        at com.shadowhawkcoding.commends.commands.CommendCommand.execute(CommendCommand.java:47)
    At CommendCommand.java:47
    So in the class CommendCommand, line 47

    That means (if I copied correctly), here:
    Code (Java):
    if (!Commends.getInstance().addCommend(pl.getUniqueId(), p, sb.toString().trim())) {
     
    #2 bayerntvplay, Oct 6, 2019
    Last edited: Oct 6, 2019
    • Like Like x 1
  3. Thanks! Didn’t realise that number meant the line number. I’ll have a look at it in a bit.
     
  4. No problem! :)

    Make sure to set the prefix of the thread to Solved if everything is working now.
    Click here to edit the title of the thread.
     
Thread Status:
Not open for further replies.