Solved Problem with List<String>

Discussion in 'Plugin Development' started by ArthurMaker, Jan 11, 2014.

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



    I'm doing a list with spawns but I got errors when I add more than 1 spawn...
    I use this:
    1. private List<String> spawns = null;
    2. public void addSpawn(String location){
    3. Bukkit.broadcastMessage("0");
    4. if(this.spawns != null){
    5. Bukkit.broadcastMessage("1");
    6. List<String> spawnList = this.spawns;
    7. Bukkit.broadcastMessage("2");
    8. spawnList.add(location);
    9. Bukkit.broadcastMessage("3");
    10. this.spawns = spawnList;
    11. Bukkit.broadcastMessage("4");
    12. }else{
    13. Bukkit.broadcastMessage("1.1");
    14. this.spawns = Arrays.asList(new String[]{location});
    15. Bukkit.broadcastMessage("2.1");
    16. }
    17. }

    I did some debug messages, and I saw it goes until 2. The "spawnList.add(location);" doesn't work and I don't understand why.
    Can anyone help me? :s
  2. Offline


    Does it throw an exception or is it just doing nothing?
  3. Offline


    Because you can do anything with null object?
    1. private List<String> spawns =null;
    change to
    1. private List<String> spawns = new ArrayList<>();
  4. Offline


    It throws an exception.
  5. You were catching an exception, and the exception has been thrown right at spawnList.add(location).

    Could you change the catch clause in the other part of your code?

    }catch(Exception e){}
    }catch(SpecificException e){}
    EDIT: nvm, didnt see your last post.

    Is the exception IllegalStateException?
  6. Offline


    Gosh, I forgot the "new ArrayList<String>()".
    I'll try it out, ninja.
  7. I think Arrays.asList returns an unmodifiable list.
  8. Offline


    Nope, it doesn't work too with "new ArrayList<String>()"...
    Not working...
  9. Offline


    ArthurMaker Because your storing a location in a string?
  10. Offline


    Nope, I convert the location to a string before execute the addSpawn().
  11. Offline


    1. List<String> locations = new ArrayList<String>();
    2. public void addLocation(String location) {
    3. locations.add(location);
    4. }

    That should work :p
  12. Offline


    Nope, doesn't work... e-e
  13. Offline


    if it does not work you obviously did something wrong.
    post "fixed" code and stacktrace.
  14. Did you see the edit?

    Whats the exception?
  15. Offline


    }catch (Exception e){

    Nope, nothing wrong.
    The code in the post is which almost worked. I mean, it added 1 string to the list, but just this. The others code doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
  16. Offline


    Don't do that

    In general, exceptions aren't a "break" in code, they are just another utility. When a specific exception is thrown, it is thrown so that you can handle the exception the correct way.

    There are also Runtime exceptions that, by design, should not be caught (NullPointer, OutOfMemory). RuntimeExceptions represent exceptions that result from an error in the code itself.

    If you choose to catch all exceptions and then, say, exit the program. That might be a usable case for this. Otherwise I would recommend against it.

    This post has some other general information about the practice:

    As stated before, post your full code, and relevant stack trace.
  17. Offline


    Solved! I just edited some things in the code and now it works perfectly.
    Thank you guys, for your help!
  18. Offline


    Remove this:

    1. @SuppressWarnings("null")

    And you'll likely find your error. You should not be suppressing warnings.
Thread Status:
Not open for further replies.

Share This Page