Get GUI from other class

Discussion in 'Spigot Plugin Development' started by ChikenGamer, Jun 22, 2015.

  1. Hi i try to get a GUI from another class of my plugin.
    [16:07:38 ERROR]: Could not pass event InventoryClickEvent to AdvancedHubPlus v1
    va:310) ~[spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(
    :1603) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$
    3) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at java.util.concurrent.Executors$ Source) [
            at Source) [?:1.8.0_31]
            at net.minecraft.server.v1_8_R3.SystemUtils.a( [spig
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(
    18) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(
    67) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(
    57) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
    :560) [spigot1_8.jar:git-Spigot-044d928-e8c6403]
            at Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
            at com.jimdo.choscar.Principal.GADGETS.GUI.InventoryClickEvent(
    135) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
    va:306) ~[spigot1_8.jar:git-Spigot-044d928-e8c6403]
            ... 15 more
    public class AdvancedHubPlusMain extends JavaPlugin implements Listener{
        public static Logger l = Logger.getLogger("Minecraft");
        PluginManager pm = Bukkit.getServer().getPluginManager();
         private GUI GUI;
         private Hats Hats;
        public void onEnable(){
            //pm.registerEvents(this, this);
            //pm.registerEvents(new Hats(this), this);
            pm.registerEvents(new Listeners(this), this);
            GUI = new GUI(this);
            Hats = new Hats(this);
         l.log(Level.INFO, "=========[AdvancedHubPlus]=========");
         l.log(Level.INFO, " > Status: Enabled");
         l.log(Level.INFO, " > Version: " + getDescription().getVersion());
         l.log(Level.INFO, " > Author: " + getDescription().getAuthors());
         l.log(Level.INFO, " > Name: " + getDescription().getName());
         l.log(Level.INFO, "=======================");
        public void onDisable(){
        l.log(Level.INFO, "=========[AdvancedHubPlus]=========");
        l.log(Level.INFO, " > Status: Disabled");
        l.log(Level.INFO, " > Version: " + getDescription().getVersion());
        l.log(Level.INFO, " > Author: " + getDescription().getAuthors());
        l.log(Level.INFO, " > Name: " + getDescription().getName());
        l.log(Level.INFO, "=======================");
    public class GUI implements Listener{
        private Hats hats;
        private Inventory invGUI;
        private ItemStack a, b, c, d, e, f, g;
        public GUI(Plugin p) {//Code, events registered}
        private ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {//Code }
            private ItemStack Skull(String skullOwner, String displayName, int quantity) {//Code }
            public void GUIM(Player p) {
            @EventHandler (priority = EventPriority.HIGH)
            public void InventoryClickEvent(InventoryClickEvent ev) {
                Player player = (Player)ev.getWhoClicked();
                PlayerInventory i = player.getInventory();
                    if (!ev.getInventory().getName().contains(invGUI.getName())) return;
                    if (ev.getCurrentItem() == null) return;
                    if (ev.getCurrentItem().getItemMeta() == null) return;
            if (ev.getCurrentItem().getItemMeta().getDisplayName().contains("Hats")) {
                    player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 0.0F);
                    hats.hats1(player); //Line of the mistake
        public void onPlayerInteract(PlayerInteractEvent e) {//Code }
            public void onPlayerJoin(final PlayerJoinEvent pje) {//Code }

    public class Hats implements Listener{
    private GUI GUI;
        private final Inventory invH, invH2, invH3, invH4, invH5;
        private final ItemStack a, b, c, d, e, f, g, h, ii, j, k, l, m, n, o, pp, q, r, s, t, u, al, ar, ad, ex,
                                a1, b1, c1, d1, e1, f1, g1, h1, ii1, j1, k1, l1, m1, n1, o1, pp1, q1, r1, s1, t1, u1,
                                a2, b2, c2, d2, e2, f2, g2, h2, ii2, j2, k2, l2, m2, n2, o2, pp2, q2, r2, s2, t2, u2,
                                a3, b3, c3, d3, e3, f3, g3, h3, ii3, j3, k3, l3, m3, n3, o3, pp3, q3, r3, s3, t3, u3,
                                a4, b4, c4, d4, e4, f4, g4, h4, ii4, j4, k4, l4, m4, n4, o4, pp4, q4, r4, s4, t4, u4;

        public Hats(Plugin p) {//Code, events registered}
            private ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {//Code}
            private ItemStack Skull(String skullOwner, String displayName, int quantity) {//Code}
            public void hats1(Player p) {
            public void hats2(Player p) {
            public void hats3(Player p) {
            public void hats4(Player p) {
            public void hats5(Player p) {

    Note: All the "GUI" work well separately

  2. what is on this line com.jimdo.choscar.Principal.GADGETS.GUI.InventoryClickEvent(

    also you really shouldn't have your method called InventoryClickEvent it should be more something like onInventoryClickEvent
  3. line 135: hats.hats1(player);//Line of the mistake
  4. I calle the method:
    public void hats1(Player p){
    From the GUI1, and the error is it
  5. InventoryClickEvent or onInventoryClickEvent
    No matter
  6. from what i can see Hats never was initialized
  7. just the matter of staying in line with what is considered the standard for writing OOP
  8. How I can initialized?
    I use "private Hats hats;" to get the method
  9. the same way you did it in Main. And if your not sure what i mean by that then I would suggest you head to the Java Basics Tutorials because if you don't know what I mean then you don't really know OOP basics
  10. @ChikenGamer also add a null check to you InventoryClickEvent, if they click outside the inventory you will get a NullPointerException.
  11. But where in the code I do? if I use "GUI = new GUI (this);" I get an error
  12. I have the null checks
  13. I haven't looked at the code, but try and make it a
    Code (Text):
    instead of a
    Code (Text):
    public void
    and then do like
    Code (Text):
    main.{Your GUI Name}(p);
  14. This goes back to my previous post, you need to learn more about what your actually doing before taking this on. At this point you are not even showing a clear understanding how constructors or objects work.
    • Agree Agree x 1
  15. I have no idea how to do it, but thank you, keep trying
  16. Like fearmig said,
    I'd suggest looking up some basic tutorials about Java OOP
  17. player.openInventory(class.Namegui)
  18. Just make a method returning your GUI Inventory:

    Code (Text):
    public Inventory getGUI(){
    return (guiname) (inventory);