Cannot add an object to ArrayList

Discussion in 'Plugin Development' started by _TheTechGuy_, Sep 5, 2021.

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

    _TheTechGuy_

    Hi!

    I am developing a plugin, that would allow players to create polls.

    This is the code for the method, that I call when I receive a command.
    Code:java
    1.  
    2. public void createPoll(String pollid, CommandSender sender){
    3. if (!pollid.equalsIgnoreCase(null)){
    4. Poll poll = new Poll(pollid,"none", new ArrayList<>());main.pollList.add(poll);sender.sendMessage(ChatColor.GREEN + "Succesfully created poll '" + pollid + "' use /addtitle <poll> to add a title to your poll.");}
    5. }
    6.  


    However, not does it not add the Poll object to the list, but it gives me NullPointerException. But I cannot find the object which is null.

    Here is the code for all of the commands.

    Code:java
    1.  
    2. public class CreatePoll implements CommandExecutor {
    3.  
    4. private Main main;Poll selectedPoll; public void addTitle(String pollID, CommandSender sender, String title){
    5. if (pollID.equalsIgnoreCase(null)){
    6. for (Poll currentPoll: main.pollList) {
    7. if (currentPoll.getId().equalsIgnoreCase(pollID)){
    8. selectedPoll = currentPoll; break;}
    9. }
    10. if (title.equalsIgnoreCase(null)){
    11. selectedPoll.setTitle(title);sender.sendMessage("Changed title for " + selectedPoll.getId() + " to " + selectedPoll.getTitle());}
    12. }
    13. }
    14. public void addVariant(String pollID, CommandSender sender, String variant){
    15. if (pollID.equalsIgnoreCase(null)){
    16. for (Poll poll: main.pollList) {
    17. if (poll.getId() == pollID){
    18. selectedPoll = poll; break;}
    19. }
    20. if (variant.equalsIgnoreCase(null)){
    21. selectedPoll.getVariants().add(variant);sender.sendMessage("Added " + variant + " as a variant for poll " + selectedPoll.getId());}
    22. }
    23. }
    24. public void createPoll(String pollid, CommandSender sender){
    25. if (!pollid.equalsIgnoreCase(null)){
    26. Poll poll = new Poll(pollid,"none", new ArrayList<>());main.pollList.add(poll);sender.sendMessage(ChatColor.GREEN + "Succesfully created poll '" + pollid + "' use /addtitle <poll> to add a title to your poll.");}
    27. }
    28.  
    29. @Override
    30. public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
    31. if (command.getName().equalsIgnoreCase("createpoll")){
    32. createPoll(args[0], sender);}else if (command.getName().equalsIgnoreCase("addtitle")){
    33. addTitle(args[0], sender, args[1]);}else if (command.getName().equalsIgnoreCase("addvariant")){
    34. addVariant(args[0], sender, args[1]);}else if (command.getName().equalsIgnoreCase("startpoll")){
    35. if (!args[0].equalsIgnoreCase(null)){
    36. Bukkit.broadcastMessage("---------------------------------------------------------");Bukkit.broadcastMessage("Poll: " + main.pollList.get(main.pollList.indexOf(args[0])).getTitle() + "\n"); for (String variant: main.pollList.get(main.pollList.indexOf(args[0])).getVariants()) {
    37. Bukkit.broadcastMessage(variant + "\n");}
    38. Bukkit.broadcastMessage("---------------------------------------------------------");}
    39. }else if (command.getName().equalsIgnoreCase("debugvoting")){
    40. sender.sendMessage("Polls: " + main.pollList.toString());}
    41. return true;}
    42. }
    43.  
     
    Last edited by a moderator: Sep 6, 2021
  2. Offline

    KarimAKL

    @_TheTechGuy_ At what line does the problem occur? Also, please format your code; it takes effort to see what is going on.
     
  3. Offline

    Strahan

    I don't see a class constructor wherein you initialize main. I'd also recommend you consider guard clauses, it'd clean that up significantly.

    Yea, that was headache inducing. When I see people post unreadable stuff like that, I go to https://codebeautify.org/javaviewer and it sorts it out :)
     
    Dai_Kunai and KarimAKL like this.
  4. Offline

    davidclue

    @_TheTechGuy_ You have to define what the list will hold when you instantiate it
    Code:
    new ArrayList<Object>()
     
  5. Offline

    KarimAKL

    That works wonders! Thanks for sharing. :)
     
    Strahan likes this.
Thread Status:
Not open for further replies.

Share This Page