Getting MySQL credentials from config

Discussion in 'Plugin Development' started by Pitazzo, Aug 2, 2014.

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

    Pitazzo

    Hi guys,
    I'd like to read my MySQL credentials from the config file in order to create the Connection object. I wrote following code:

    Code:java
    1. public static Connection connection;
    2. public static Main plugin;
    3.  
    4. public MySQL(Main plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. public static synchronized void openConnection() {
    9. try {
    10. connection = DriverManager.getConnection("jdbc:mysql://"
    11. + plugin.getConfig().getString("MySQL.Host") + ":"
    12. + plugin.getConfig().getString("MySQL.Port") + "/"
    13. + plugin.getConfig().getString("MySQL.DB"), plugin
    14. .getConfig().getString("MySQL.User"), plugin.getConfig()
    15. .getString("MySQL.Password"));
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. }


    My config.yml looks so:

    Code:
    MySQL:
      Host: localhost
      Port: 3306
      DB: bukkit
      User: root
      Pass: xxxxx
      
    But whenever I try to use the openConnection method, I get a NullPoinetrExecption:

    Code:java
    1. 16:16:28 [SEVERE] java.lang.NullPointerException
    2. 16:16:28 [SEVERE] at es.programahermes.MySQL.openConnection(MySQL.java:22)
    3. 16:16:28 [SEVERE] at es.programahermes.MySQL.dbContanisPlayer(MySQL.java:43)
    4. 16:16:28 [SEVERE] at es.programahermes.Utilidades.PointsAdjust$1.run(PointsAdjust.java:21)
    5. 16:16:28 [SEVERE] at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftTask.run(CraftTask.java:53)
    6. 16:16:28 [SEVERE] at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    7. 16:16:28 [SEVERE] at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:524)
    8. 16:16:28 [SEVERE] at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    9. 16:16:28 [SEVERE] at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    10. 16:16:28 [SEVERE] at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    11. 16:16:28 [SEVERE] at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    12. 16:16:28 [INFO] Error al conectar con la base de datos. Revisar credenciales.
    13. 16:16:28 [WARNING] [HermesCore] Task #12 for HermesCore v0.2 generated an exception
    14.  


    Any help will be appreciated ;)
     
  2. Offline

    Gater12

    Pitazzo
    Config may be null on first start up if the config isn't generated before invoking openConnection.
     
  3. Offline

    fireblast709

    Pitazzo remove all the static keywords
     
  4. Offline

    Pitazzo

    Gater12
    I know it, but it'snt the problem; the config is generated.

    Thanks

    fireblast709 No way, I need to access the method from other classes

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

    fireblast709

    Pitazzo then learn how to do this without static, because you obviously have no idea how to properly implent it with (and static isn't the preferred way anyway) ;).

    Just use the constructor to pass along refences, and a non static getter function to retrieve the connection.
     
Thread Status:
Not open for further replies.

Share This Page