Creating a chat channel

Discussion in 'Plugin Development' started by mydeblob, Jan 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    mydeblob

    I'm trying to create a chat channel but have came across a NPe which I can't figure out what's causing it.
    This is my code for the full event
    Code:java
    1. private CommandHandler ch;
    2. @EventHandler
    3. public void chatChannel(AsyncPlayerChatEvent event){
    4. Player p = event.getPlayer();
    5. if(ch.inClanChat.contains(p.getName())){
    6. event.setCancelled(true);
    7. ClanManager.getInstance().getPlayerClan(p).msg(ClanManager.getInstance().getPlayerClan(p), "THIS IS CLAN CHAT" + event.getMessage());
    8. }
    9. }

    ArrayList I'm checking if player is in:
    Code:java
    1. ArrayList<String> inAllyChat = new ArrayList<String>();

    It's throwing the following NPE
    Code:
    Caused by: java.lang.NullPointerException
        at com.mydeblob.Events.chatChannel(Events.java:36) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_09]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_09]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_09]
        at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0_09]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 31 more
    on this line
    Code:java
    1. if(ch.inClanChat.contains(p.getName()))

    The weird part is, this NPE happens every time I talk even if I'm not in the arraylist inClanChat.
    Any help is appreciated
     
  2. Offline

    Maurdekye

    mydeblob You don't seem to have initialized your CommandHandler.
     
  3. Offline

    mydeblob

    Maurdekye
    Sorry, I forgot to post that code. I have this super in my event class
    Code:java
    1. public Events(ClansMain plugin, CommandHandler ch){
    2. this.plugin = plugin;
    3. this.ch = ch;
    4. }
     
  4. Offline

    mydeblob

  5. Offline

    mydeblob

    KraZ__
    This is pretty much what I did, but I didn't use methods.

    KraZ__
    I didn't need to use the chat channel much, only in one command. So I didn't make any methods. I will switch to methods, even though it shouldn't make a difference besides cleaning up my code. Maybe as I switch to methods I will find an error in my code, so Thank you. I will tell my result soon

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 11, 2022
Thread Status:
Not open for further replies.

Share This Page