Solved error when calling method from another class

Discussion in 'Plugin Development' started by gereid, Feb 3, 2013.

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

    gereid

    Hello, lets have a look on this code:
    Code:java
    1.  
    2. public void item() {
    3.  
    4. Location loc;
    5.  
    6. loc = new Location(
    7.  
    8. Bukkit.getServer().getWorld(plugin.getConfig().getString("Loc.world")),
    9.  
    10. plugin.getConfig().getDouble("Loc.x"),
    11.  
    12. plugin.getConfig().getDouble("Loc.y"),
    13.  
    14. plugin.getConfig().getDouble("Loc.z"));
    15.  
    16. Block b = World.getBlockAt(loc); // The method getBlockAt(Location) is undefined for the type World
    17. Sign s = (Sign) b;
    18. s.setLine(1, "Your e");
    19. }
    20.  


    it should get cordinates from config, and change text on sign, but unfortunatelly getBlockAt refuses to work... if anybody know why, i would be gratefull.

    Thanks in advance
     
  2. Because you are calling it as if it's a static method, when it is not.

    Perhaps you meant to do

    loc.getWorld().getBlockAt(loc)
     
  3. Offline

    chasechocolate

    World is not static (at least with what you are trying to do). Do what tehbeard said.
    Code:java
    1. Block block = loc.getWorld().getBlockAt(loc);
     
  4. Offline

    gereid

    i would never get to this point alone.. Thanks!

    anyway, i would like to run this piece of code as thread, so my sign is updated every few seconds, but it gives me error:
    00:38:25 [SEVERE] Exception in thread "pool-1-thread-189"
    00:38:26 [SEVERE] org.apache.commons.lang.UnhandledException: Plugin sgwall vr01
    generated an exception while executing task 427
    at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftAsyncTask.run(CraftAsyn
    cTask.java:56)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
    at pl.chinchill.SgWall.SignUpdater.item(SignUpdater.java:27)
    at pl.chinchill.SgWall.SgWall$1.run(SgWall.java:29)
    at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftTask.run(CraftTask.java
    :53)
    at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftAsyncTask.run(CraftAsyn
    cTask.java:53)
    ... 3 more
     
  5. Offline

    chasechocolate

    What is line 29 of the SgWall class? What code are you using to update the sign ever few seconds?
     
  6. Offline

    gereid

    Code:java
    1.  
    2. BukkitTask task = getServer().getScheduler().runTaskTimerAsynchronously(this , new Runnable() {
    3. @Override
    4. public void run() {
    5. SignUpdater updater = new SignUpdater(null);
    6. updater.item();
    7. }
    8. }, 60L, 200L);
    9.  
    10. }
    11.  
    12.  

    its updater.item();

    dont get me wrong - this is my first plugin, and im very new in java, so i have no idea if im doing things right.

    idea of what i want to do is:

    run task every 10s {

    loop // for every key of config do code {

    get location of sign x and sign y, and then copy contents from x to y

    }

    }

    idk if this is best way to achieve my plan, but i dont know how to acces another plugin's classes, and directly "steal" data used to create signs...
     
  7. Offline

    gomeow

    You are probably getting an NPE because you passed null into it...

    To do something for every key in the Group key:
    Code:
    Group:
      key1: 34
      key2: 56
      Key3: 37
    Code:java
    1. for(String key:getConfig().getConfigurationSection("Group").getKeys(false)) {
    2. int value = getConfig().getInt("Group."+key);
    3. //Do stuff here
    4. }
     
  8. Offline

    gereid

    Code:java
    1.  
    2.  
    3. public void item() {
    4.  
    5. Bukkit.broadcastMessage(ChatColor.GOLD+" test message");
    6.  
    7.  
    8. // Location loc;
    9. // loc = new Location(Bukkit.getServer().getWorld(plugin.getConfig().getString("signs.id1.location.world")),
    10. // plugin.getConfig().getInt("signs.id1.location.x"),
    11. // plugin.getConfig().getInt("signs.id1.location.y"),
    12. // plugin.getConfig().getInt("signs.id1.location.z"));
    13. // Block b = loc.getWorld().getBlockAt(loc);
    14. // Sign s = (Sign) b;
    15. // s.setLine(1, "test");
    16. }
    17.  


    not sure about NPE but this code works and is executed every 10 seconds, so i think, that there is something terrible wrong in public void item() method.. just no idea what :/
     
  9. Offline

    gomeow

    item() is a method, not a class...
     
  10. Offline

    gereid

    yeah, i just realized my mistake right before You posted ;)

    i experimented some more, and i got to this point

    Code:java
    1.  
    2. public void item() {
    3.  
    4. Bukkit.broadcastMessage(ChatColor.GOLD+" test message");
    5.  
    6. String x = plugin.getConfig().getString("signs.id1.location.world"); // line 28
    7.  
    8. plugin.getServer().broadcastMessage("world name from sign id1 is: "+x);
    9.  
    10. }
    11.  


    when calling item() from main, test message is sent, but next line throws error, Help needed :(

    running this code directly from onEnable - still same error, on line 28
    Code:java
    1.  
    2. SignUpdater updater = new SignUpdater(null);
    3. updater.item();
    4.  


    edit: i got it! it was as simple as putting SignUpdater updater = new SignUpdater(null); in on load with final type, and then calling updater.item() from thread - hope it wont cause me more problems ;)

    thanks for Your assistance!
     
Thread Status:
Not open for further replies.

Share This Page