Material NPE

Discussion in 'Plugin Development' started by HeavyMine13, Sep 29, 2014.

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

    HeavyMine13

    Hey! I am trying to set a sign ling to a materials name, and I am getting the name from an input ID.

    It is throwing NPE here:
    Code:java
    1. e.setLine(1, mer.toString());


    Code:java
    1. Material mer = null;
    2. try {
    3. int id = Integer.parseInt(e.getLine(1));
    4. mer = Material.getMaterial(id);
    5. } catch (NumberFormatException err) {
    6. mer = Material.matchMaterial(e.getLine(1));
    7. }
     
  2. Offline

    xize

    HeavyMine13
    add some checks in your second snippet and put a return; if its still null after the try and catch, because I suspect you are using the mer Material outside the try and catch futher?
     
  3. Offline

    HeavyMine13

    after try and catch, I have other methods and my first snippet
     
  4. Offline

    fireblast709

    HeavyMine13
    • matchMaterial(String) does the id check already
    • matchMaterial(String) will return null if the material doesn't exist
     
  5. Offline

    HeavyMine13

    I just made this and still shoots the error bellow:
    Code:java
    1. if (Material.matchMaterial(e.getLine(1)) != null|| Material.matchMaterial(e.getLine(1)) != Material.AIR) {
    2. Material mer = Material.matchMaterial(e.getLine(1));
    3. e.setLine(1, "Lal");
    4. }



    Error:
     
  6. Offline

    fireblast709

    HeavyMine13 what you basically did was the following: if text is not a material OR if not air. So if it's null, it's not air, which means that the condition is true (and the if statement will execute). Change || to &&.
     
Thread Status:
Not open for further replies.

Share This Page