Mythruna
March 28, 2024, 02:13:20 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to the new forums. See "Announcements" for a note for new users.
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: This shouldn't give a NullPointerException  (Read 5668 times)
Michael
Donators
Hero Member
***
Posts: 2166



View Profile
« on: September 15, 2013, 09:12:56 AM »

I am receiving a NPE, and I made sure not a thing was null. Here is my stack trace:

Code:
11:08:08,790 ERROR [EventDispatcher] Error handling event:PlayerEvent[mythruna.s
erver.PlayerConnectionContext@516b7a] for type:PlayerJoined  in handler:me.shzyl
o.essentials.Essentials$2@148a67a
java.lang.NullPointerException
        at me.shzylo.essentials.commands.CmdSethome.registerCommand(CmdSethome.j
ava:54)
        at me.shzylo.essentials.commands.Command.registerCommands(Command.java:3
2)
        at me.shzylo.essentials.Essentials$2.newEvent(Essentials.java:42)
        at me.shzylo.essentials.Essentials$2.newEvent(Essentials.java:1)
        at mythruna.event.EventDispatcher.publishEvent(EventDispatcher.java:65)
        at mythruna.server.GameServer$MessageObserver.login(GameServer.java:831)

        at mythruna.server.GameServer$MessageObserver.messageReceived(GameServer
.java:841)
        at mythruna.server.GameServer$MessageObserver.messageReceived(GameServer
.java:625)
        at com.jme3.network.base.MessageListenerRegistry.messageReceived(Message
ListenerRegistry.java:74)
        at com.jme3.network.base.DefaultServer.dispatch(DefaultServer.java:283)
        at com.jme3.network.base.DefaultServer$Redispatch.messageReceived(Defaul
tServer.java:570)
        at com.jme3.network.base.DefaultServer$Redispatch.messageReceived(Defaul
tServer.java:566)
        at com.jme3.network.base.KernelAdapter.dispatch(KernelAdapter.java:184)
        at com.jme3.network.base.KernelAdapter.createAndDispatch(KernelAdapter.j
ava:238)
        at com.jme3.network.base.KernelAdapter.run(KernelAdapter.java:281)

me.shzylo.essentials.commands.CmdSethome.registerCommand(CmdSethome.java:54):
Code:
shell
.registerCommand( // THIS LINE
name,
cmd);

me.shzylo.essentials.commands.Command.registerCommands(Command.java:32):
Code:
new CmdSethome
(ess)
.registerCommand(); // THIS LINE

me.shzylo.essentials.Essentials$2.newEvent(Essentials.java:42):
Code:
command.registerCommands();

====

Full class codes:

Essentials.java:
Code:
package me.shzylo.essentials;

import java.io.File;

import me.shzylo.essentials.commands.Command;
import mythruna.event.EventDispatcher;
import mythruna.event.EventListener;
import mythruna.event.EventType;
import mythruna.event.PlayerEvent;
import mythruna.event.PlayerEvents;
import mythruna.server.event.ServerEvent;
import mythruna.server.event.ServerEvents;

public class Essentials {
public static final String NAME = "Essentials";
public static final File FOLDER = new File("scripts/" + NAME);
private static Command command;

@SuppressWarnings("rawtypes")
public static void onServerStart() {
EventDispatcher.getInstance().addListener(ServerEvents.serverStarted, new EventListener<ServerEvent>() {
public void newEvent(EventType<ServerEvent> type, ServerEvent e) {
if (!FOLDER.exists())
FOLDER.mkdir();

System.out.println("[Essentials] Loaded Successfully!");
}
});
}

public static void onPlayerJoin() {
command = new Command(new Essentials());
System.out.println("essentials debug 1");

EventDispatcher.getInstance().addListener(PlayerEvents.playerJoined, new EventListener<PlayerEvent>() {
public void newEvent(EventType<PlayerEvent> type, final PlayerEvent e) {
System.out.println("essentials debug 2");
command.setShell(e.getContext().getShell());
System.out.println("essentials debug 3");
command.setPlayerEvent(e);
System.out.println("essentials debug 4");
command.registerCommands();
System.out.println("essentials debug 5");
}
});
}
}

Command.java:
Code:
package me.shzylo.essentials.commands;

import me.shzylo.essentials.Essentials;
import mythruna.event.PlayerEvent;

import org.progeeks.tool.console.Shell;

public class Command {
protected Essentials ess;
protected Shell shell;
protected PlayerEvent event;

public Command(Essentials instance) {
System.out.println("command debug 1");
this.ess = instance;
}

public void setShell(Shell shell) {
System.out.println("command debug 2");
this.shell = shell;
}

public void setPlayerEvent(PlayerEvent e) {
System.out.println("command debug 3");
this.event = e;
}

public void registerCommands() {
System.out.println("command debug 4");
new CmdSethome
(ess)
.registerCommand();
}
}

CmdSethome.java:
Code:
package me.shzylo.essentials.commands;

import me.shzylo.essentials.Essentials;

import org.progeeks.cmd.Environment;
import org.progeeks.cmd.Result;
import org.progeeks.tool.console.ShellCommand;
import org.progeeks.tool.console.ShellEnvironment;

public class CmdSethome extends Command {

public CmdSethome(Essentials ess) {
super(ess);
System.out.println("cmdsethome debug 1");
}

public void registerCommand() {
System.out.println("cmdsethome debug 2");
String name = "sethome";
final String desc = "set your home at your current location.";
final String[] help = null;
System.out.println("cmdsethome debug 3");

ShellCommand cmd = new ShellCommand() {
private static final long serialVersionUID = 1L;

public Result execute(Environment env) {
return null;
}

public boolean isSimple() {
System.out.println("cmdsethome debug 4");
return false;
}

public String[] getHelp() {
System.out.println("cmdsethome debug 5");
return help;
}

public String getDescription() {
System.out.println("cmdsethome debug 6");
return desc;
}

public int execute(ShellEnvironment shellEnv, String str) {
System.out.println("cmdsethome debug 7");
executeCommand();
return 0;
}
};
System.out.println("cmdsethome debug 8");
shell
.registerCommand(
name,
cmd);
}

public void executeCommand() {
/*try {
EntityId playerEntity = event.getContext().getPlayer();
GameSystems systems = event.getContext().getSystems();
EntityData ed = systems.getEntityData();
Position pos = ed.getComponent(playerEntity, Position.class);
Vector3f location = pos.getLocation(); // null float playerX
float playerX = location.getX();
float playerY = location.getY();
float playerZ = location.getZ();
String playerName = event.getContext().getPlayerData().get("characterInfo.name");

System.out.println(playerName + "'s location is: " + playerX + ", " + playerY + ", " + playerZ);
} catch (Exception e) {
e.printStackTrace();
}*/

event.getContext().echo("setting home randomly");
}
}

As you can see, I did the debug test for it all, and when I did the debug this was the order, everything was set accordingly so nothing went null:
Quote
essentials debug 2
command debug 2
essentials debug 3
command debug 3
essentials debug 4
command debug 4
command debug 1
cmdsethome debug 1
cmdsethome debug 2
cmdsethome debug 3
cmdsethome debug 8

If you can just tell me what is causing this problem, I will feel so stupid if it was just a silly little mistake.
Logged
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #1 on: September 15, 2013, 11:00:01 AM »

shell is null.  NPEs are never thrown by accident so if you see one then something about the dereference is null. In this case, you are trying to call a method on a null object.

Your code is very strange also.  You have a Cmdsethome that you must instantiate before calling registerCommand... it then creates a completely different kind of command that it registers with the shell just to call back to itself.  Why not just create the right command in the first place?

At any rate, by that point you haven't set any of the things like shell, I guess.  So registration fails.  The code was really hard for me to follow so I imagine it's also really hard for you to fully follow what is going on.
Logged
Michael
Donators
Hero Member
***
Posts: 2166



View Profile
« Reply #2 on: September 15, 2013, 11:14:53 AM »

I got it to work, I went ahead and did as you said, which is directly register it. I was trying to implement it in a way so that I don't have to set the shell, and context each time, kind of make the code "neater" but in turn I just screwed myself over. I thought the code made some sense, just practicing and getting better at this point Smiley
Logged
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #3 on: September 15, 2013, 11:22:33 AM »

Well, every player has a different shell... so I'm not sure you save much trouble anyway.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.20 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!