Trying to assign an array a value

Discussion in 'Plugin Development' started by chris4315, Jul 21, 2013.

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

    chris4315

    I'm trying to create a list of announcements loaded from my SQL database and I have loaded them successfully without SQLExceptions, but I can't seem to add them to the String array announcements that I created. Here's my code:

    Code:java
    1.  
    2. package me.dev.mct.Announcer;
    3.  
    4. import java.sql.ResultSet;
    5. import java.sql.SQLException;
    6. import java.util.HashMap;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Announcer extends JavaPlugin {
    13.  
    14. public String prefix = ChatColor.GRAY + "[" + ChatColor.BLUE + "MC-Tron" + ChatColor.GRAY + "] " + ChatColor.BLUE + "";
    15. public String[] announcements;
    16.  
    17. public void onEnable() {
    18. getLogger().info("Announcer has been enabled and will connect shortly!");
    19. try {
    20. Database.start();
    21. Database.query("CREATE TABLE IF NOT EXISTS announcements (string varchar(255))");
    22. getLogger().info("Starting database... creating necessary tables...");
    23. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    24. public void run() {
    25. loadAnnouncements();
    26. }
    27. }, 0L, 12000L);
    28. } catch (SQLException e) {
    29. e.printStackTrace();
    30. }
    31. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    32. public void run() {
    33. startSpeaking();
    34. }
    35. },350L);
    36. }
    37.  
    38. public void onDisable() {
    39. getLogger().info("Announcer has been disabled!");
    40. try {
    41. Database.stop();
    42. } catch (SQLException e) {
    43. e.printStackTrace();
    44. }
    45. }
    46.  
    47. public void loadAnnouncements() {
    48. if (Database.openConnection()) {
    49. int loaded = 0;
    50. int queue = 1;
    51. try {
    52. rs = Database.query("SELECT * FROM announcements");
    53. while (rs.next()) {
    54. announcements[queue] = rs.getString("string");
    55. queue++;
    56. loaded++;
    57. }
    58. getLogger().info(loaded + " announcements were loaded from the database!");
    59. } catch (SQLException e) {
    60. e.printStackTrace();
    61. }
    62.  
    63. }
    64. else {
    65. getLogger().info("Announcer could not load MC-Tron's announcements because it couldn't connect to the database!");
    66. Bukkit.broadcast(ChatColor.RED + "Announcements failed to be loaded. No database connection.", "mct.admin");
    67. }
    68. }
    69.  
    70. public void startSpeaking() {
    71. if (announcements.length > 0) {
    72.  
    73. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    74. public void run() {
    75. int queue = 1;
    76. if (!(announcements.length < queue)) {
    77. Bukkit.broadcastMessage(prefix + announcements[queue]);
    78. queue+=1;
    79. }
    80. }
    81. }, 0L, 120L);
    82. }
    83. else {
    84. getLogger().info("Why are there no announcements to be read!");
    85. Bukkit.broadcast(ChatColor.RED + "There aren't any announcements to be read! Check the list to be sure.", "mct.admin");
    86. }
    87. }
    88.  
    89.  
    90.  
    91.  
    92. }
    93.  

    And there's a NullPointerException on lines 54 and 24. And another one on 71 and 32. I'm in a bit of a rush today :p so I may have forgotten the most silliest thing.

    Bump

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

    Rprrr

    chris4315
    Well... your result statement is null.. how can you expect to load the messages correctly from the database, then?
     
  3. Offline

    chris4315

    The messages are being loaded properly from the database. I debugged that and checked to be sure. I think the lines are messed up.
     
Thread Status:
Not open for further replies.

Share This Page