NoteBlockAPI NullPointerException

Discussion in 'Plugin Development' started by MrDaniel, Sep 1, 2019.

  1. Offline

    MrDaniel

    Dear alive people,

    I am making a plugin, which has a custom entity named bechdoldt, now I have a problem, whenever I use the bechdoldt variable, it throws a nullpointer exception, for example when I check if it is null
    Code:
    boolean hmm = Bechdoldt == null;
    main.getLogger().info(Boolean.toString(hmm));
    or when I am using the api and I am using the entity to play music
    Code:
    esp.setEntity(Bechdoldt);
    or when I try to get the location and use it for the api
    Code:
    esp.setTargetLocation(Bechdoldt.getLocation());

    help?

    full code
    Code:
    public Plugin plugin;
        public Main main;
        public WitherSkeleton Bechdoldt;
        public BossBar bar;
        public String name = ChatColor.translateAlternateColorCodes('§', "§f[§cDICTATOR§f] §9Bechdoldt");
        public int task;
        public EntitySongPlayer esp;
       
       
        public MsBechdoldt(Location arg0, Plugin arg1, Main arg2) {
            summonBechdoldt(arg0);
            plugin = arg1;
            main = arg2;
           
            esp = new EntitySongPlayer(main.getBechdoldtMusic());
        }
       
       
        public void summonBechdoldt(Location loc) {
            Bechdoldt = (WitherSkeleton) loc.getWorld().spawnEntity(loc, EntityType.WITHER_SKELETON);
            bar = Bukkit.createBossBar("§f[§cDICTATOR§f] §9Bechdoldt", BarColor.PURPLE, BarStyle.SEGMENTED_20);
            Bechdoldt.setCanPickupItems(false);
            Bechdoldt.setCustomName(name);
            Bechdoldt.setCustomNameVisible(true);
            Bechdoldt.setMaxHealth(100);
            Bechdoldt.setHealth(100);
            boolean hmm = Bechdoldt == null;
            main.getLogger().info(Boolean.toString(hmm));
            esp.setEntity(Bechdoldt);
            esp.setDistance(100);
            for (Player p : Bukkit.getOnlinePlayers()) {
                esp.addPlayer(p);
            }
            esp.setPlaying(true);
        }
     
  2. Offline

    KarimAKL

    @MrDaniel Checking if the variable is null shouldn't throw an exception. Try checking if it's null before using any of it's methods, and then remember to return if it's null.
     
  3. Offline

    MrDaniel

    Ok, I did that, but now it's saying that there is a nullpointerexception on this line of code:

    Code:
    esp.setEntity(Bechdoldt);
     
  4. Offline

    KarimAKL

    @MrDaniel Did you do something like this?
    Code:Java
    1. if (esp == null) System.out.println("esp is null");
    2. if (Bechdoldt == null) System.out.println("Bechdoldt is null");
    3. return;

    And what was the result?
     

Share This Page