Struggling with YAML in different classes

Discussion in 'Plugin Development' started by herghost, Nov 22, 2011.

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

    herghost

    Hi all

    I was wondering if someone could take the time to clear up a bit of lack of understanding on my part. I have followed this excellent tutorial by WalkerCrouse and I can now create a config file and add defaults etc and use it in my main class.

    My issue is that I have a call to a method in my onEnable which creates some MySQL tables. What I am trying to do is get the database username and password from my config.yml.

    This is where I fall down completely. I just dont understand how I am meant to retrieve the values into a string in a different class.

    My class is below:
    Code:
    package me.herghost.Fiery.functions;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    
    
    
    import me.herghost.Fiery.Fiery;
    
    
    
    public class sqlFunctions
    {
    
    	public sqlFunctions(Fiery instance)
    	{
    		this.plugin = instance;
    	}
    	 public Fiery plugin;
    	 String user = plugin.config.getString("settings.mysql.user");
    	 String pass = plugin.config.getString("settings.mysql.pass");
    	 String url = "jdbc:mysql://localhost:3306/Fiery";
    	public void create_table_users() throws SQLException
    	{
    		Connection conn = DriverManager.getConnection(url, user, pass);
    		PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS users (p_id int NOT NULL AUTO_INCREMENT KEY, p_name varchar(255), p_ip varchar(255))");
    		sampleQueryStatement.executeUpdate();
    		sampleQueryStatement.close();
    		conn.close();
    	}
    
    	public void create_table_userhomes() throws SQLException
    	{
    		Connection conn = DriverManager.getConnection(url, user, pass);
    		PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS userhomes (world varchar(128), p_name varchar(255) not null, home_x varchar(500), home_y varchar(500), home_z varchar(500), world1 varchar(128), home1_x varchar(500), home1_y varchar(500), home1_z varchar(500), world2 varchar(128), home2_x varchar(500), home2_y varchar(500), home2_z varchar(500), primary key(p_name))");
    		sampleQueryStatement.executeUpdate();
    		sampleQueryStatement.close();
    		conn.close();
    	}
    	}
    
    
    This results in a null pointer exception on the String user ........... line.

    I have also tried
    getConfig().getString(path) which results in the same error.

    I only started learning Java a month or so ago, so I am not going to pretend I understand it all, but I thought I had a grasp of it!

    If someone would be so kind to explain what I am missing or what I am fundamentally doing wrong then I would much appreciate it. I am an PHP developer by trade and so I think that sometimes my logic for Java is floored somewhat!

    Many Thanks
    Dave
     
    WalkerCrouse likes this.
  2. Offline

    Sagacious_Zed Bukkit Docs

    In your other class, you should create a private final field for your plugin. In the constructor you should have a parameter for your plugin and set it to the field. When you construct the second object in the main class, you should pass your main class as the argument.

    Whenever you want to retrieve your config object, invoke getConfig() on your plugin field in the second class.
     
  3. Offline

    herghost

    @Sagacious_Zed

    Firstly, thank you for responding :)

    Ehm, could you possible dumb it down a bit?!

    Code:
    //create a private final field for your plugin
    private final Fiery plugin
    
     //parameter for your plugin and set it to the field
    public Fiery(Fiery plugin)
    	{
    		this.plugin = plugin;
    	}
    ???
    
    //construct the second object in the main class, you should pass your main class as the argument.
    ??????
    
    
    //Whenever you want to retrieve your config object, invoke getConfig() on your plugin field in the second class.
    public Fiery plugin;
    	 String user = plugin.getConfig().getString("settings.mysql.user"); ??????
    
    I am calling my method from the main class like this:

    Code:
    try
    			{
    			   sqlFunctions method = new sqlFunctions(plugin);
    	        	method.create_table_users();
    	        	method.create_table_userhomes();
    	        	log.info("Fiery Database Tables OK!");
    	      	}
    
    Thanks again for your help, and please excuse my ignorance!
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Okay,

    So in your sqlFunctions class, you are doing it right, you have the plugin's main class as a field in your other class, sqlFunction.
    When you construct sqlFuctions you should pass it the current instance of your plugin's main class.
    so from your main class
    Code:
    sqlFunctions method = new sqlFunctions(this);
    The third thing is that you cannot use getConfig() with field initializers, unless the class is being constructed inside the main class onEnable method. Because, getConfig() depends on some values at runtime, which are not always available at object construction, always true for the main class, sometimes true for other classes depending when you construct it.
     
  5. Offline

    herghost

    I am now getting an error on my main Fiery.java.

    Would it help if I posted my complete code?

    My main class (Fiery.java)

    Code:
    package me.herghost.Fiery;
    
    import java.sql.SQLException;
    import java.util.logging.Logger;
    
    import me.herghost.Fiery.commands.banCommand;
    import me.herghost.Fiery.commands.giveCommand;
    import me.herghost.Fiery.commands.homeCommand;
    import me.herghost.Fiery.commands.itemCommand;
    import me.herghost.Fiery.commands.kickCommand;
    import me.herghost.Fiery.commands.sethomeCommand;
    import me.herghost.Fiery.commands.spawnCommand;
    import me.herghost.Fiery.commands.unbanCommand;
    import me.herghost.Fiery.functions.sqlFunctions;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Fiery extends JavaPlugin {
    	private final Fiery plugin;
    	public Fiery(Fiery plugin)
    	{
    		this.plugin = plugin;
    	}
    	Logger log = Logger.getLogger("Minecraft");
    
    	//The Listener
    	private final FieryPlayerListener playerListener = new FieryPlayerListener(this);
    	//Config File
    	public FileConfiguration config;
    	//OnEnable Methods
    	public void onEnable(){
    		//Send Message to Console
    		log.info("Fiery Plugin Enabled - Beta");
    		//Initialize config file
    		loadConfiguration();
    		//Create Tables
    		try
    			{
    			    sqlFunctions method = new sqlFunctions(this);
    	        	method.create_table_users();
    	        	method.create_table_userhomes();
    	        	log.info("Fiery Database Tables OK!");
    	      	}
    		catch
    			(SQLException e1)
    				{
    					log.info("Something Fucked Up");
    					e1.printStackTrace();
    	        	}
    		//register commands
    		this.getCommand("item").setExecutor(new itemCommand());
    		this.getCommand("give").setExecutor(new giveCommand());
    		this.getCommand("kick").setExecutor(new kickCommand());
    		this.getCommand("spawn").setExecutor(new spawnCommand());
    		this.getCommand("ban").setExecutor(new banCommand());
    		this.getCommand("unban").setExecutor(new unbanCommand());
    		this.getCommand("sethome").setExecutor(new sethomeCommand());
    		this.getCommand("home").setExecutor(new homeCommand());
    		//register listeners
    		PluginManager pm = this.getServer().getPluginManager();
    		pm.registerEvent(Event.Type.PLAYER_JOIN,playerListener, Event.Priority.Normal, this);
    	}
    	public void onDisable(){
    		log.info("Fiery Plugin Disabled - Goodbye!");
    	}
    	public void loadConfiguration()
    	{
    		config = getConfig();
    		getConfig().addDefault("settings.mysql.user", "mysql username");
    		getConfig().addDefault("settings.mysql.pass", "mysql password");
    		getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
    
    
    	public Fiery getPlugin() {
    		return plugin;
    	}
    
    }
    
    and sqlFunctions
    Code:
    package me.herghost.Fiery.functions;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    
    
    
    import me.herghost.Fiery.Fiery;
    
    
    
    public class sqlFunctions
    {
    	 String user = plugin.getString("settings.mysql.user");
    	 String pass = plugin.getString("settings.mysql.pass");
    	 String url = "jdbc:mysql://localhost:3306/Fiery";
    	public void create_table_users() throws SQLException
    	{
    		Connection conn = DriverManager.getConnection(url, user, pass);
    		PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS users (p_id int NOT NULL AUTO_INCREMENT KEY, p_name varchar(255), p_ip varchar(255))");
    		sampleQueryStatement.executeUpdate();
    		sampleQueryStatement.close();
    		conn.close();
    	}
    
    	public void create_table_userhomes() throws SQLException
    	{
    		Connection conn = DriverManager.getConnection(url, user, pass);
    		PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS userhomes (world varchar(128), p_name varchar(255) not null, home_x varchar(500), home_y varchar(500), home_z varchar(500), world1 varchar(128), home1_x varchar(500), home1_y varchar(500), home1_z varchar(500), world2 varchar(128), home2_x varchar(500), home2_y varchar(500), home2_z varchar(500), primary key(p_name))");
    		sampleQueryStatement.executeUpdate();
    		sampleQueryStatement.close();
    		conn.close();
    	}
    
    
    
    	}
    the error is for the sqlFunctions method = new sqlFunctions(this); line, and is 'the constructor of sqlFunctions(Fiery) is undefined.

    As for actually getting the value to the String, what should I be reading up on?

    Thanks again
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    remove from Fiery.java the following:
    Code:
    	public Fiery(Fiery plugin)
    	{
    		this.plugin = plugin;
    	}
    	Logger log = Logger.getLogger("Minecraft");
    declare a plugin variable in sqlFunction.java
    Code:
    private JavaPlugin plugin;
    define a constructor in sqlFunction.java
    Code:
    public sqlFunction(JavaPlugin plugin) {
        this.plugin = plugin;
    }
    As for actually reading the value of the string it is correct, as long as this object, sqlFunction, is created after your plugin's onLoad method is called internally by bukkit. Generally, inside onEnable or later.
     
  7. Offline

    herghost

    I am still getting the same error on the ' sqlFunctions method = new sqlFunctions(this);' line?
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    @herghost
    if you copy pasta my code, I missed an s on sqlFunction
     
  9. Offline

    herghost

    Nope, s is there

    also if I remove public Fiery(Fiery plugin)
    { this.plugin = plugin; }

    but leave Private final Field plugin; in Fiery.java then I get an error on public class Fiery extends JavaPlugin { for Fiery with 'the blank final field plugin has not been initialized'
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    you can remove the declared variable plugin in fiery too, i don't see why you need your plugin to reference itself in that way.

    And I don't know why it think a constructor has not been defined, if you have defined it. It is defiend right?
     
  11. Offline

    herghost

    Yup, this is driving me nuts! I half want to give up and put the create tables stuff in the main class, but I know there must be something I am over looking! I have it so Eclipse produces no errors, just still getting the same nullpointer error when the plugin is loaded.

    The code currently looks like this:
    Fiery.java (My Main Class)
    Code:
    package me.herghost.Fiery;
    
    import java.sql.SQLException;
    
    import java.util.logging.Logger;
    
    import me.herghost.Fiery.commands.banCommand;
    import me.herghost.Fiery.commands.giveCommand;
    import me.herghost.Fiery.commands.homeCommand;
    import me.herghost.Fiery.commands.itemCommand;
    import me.herghost.Fiery.commands.kickCommand;
    import me.herghost.Fiery.commands.sethomeCommand;
    import me.herghost.Fiery.commands.spawnCommand;
    import me.herghost.Fiery.commands.unbanCommand;
    import me.herghost.Fiery.functions.sqlFunctions;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Fiery extends JavaPlugin {
    	Logger log = Logger.getLogger("Minecraft");
    
    	//The Listener
    	private final FieryPlayerListener playerListener = new FieryPlayerListener(this);
    	//Config File
    	public FileConfiguration config;
    	//OnEnable Methods
    	public void onEnable(){
    		//Send Message to Console
    		log.info("Fiery Plugin Enabled - Beta");
    		//Initialize config file
    		loadConfiguration();
    		//Create Tables
    		try
    			{
    			    sqlFunctions method = new sqlFunctions();
    			    method.create_table_users();
    	        	method.create_table_userhomes();
    	        	log.info("Fiery Database Tables OK!");
    	      	 }
    		catch
    			(SQLException e1)
    				{
    					log.info("Something Fucked Up");
    					e1.printStackTrace();
    	        	}
    		//register commands
    		this.getCommand("item").setExecutor(new itemCommand());
    		this.getCommand("give").setExecutor(new giveCommand());
    		this.getCommand("kick").setExecutor(new kickCommand());
    		this.getCommand("spawn").setExecutor(new spawnCommand());
    		this.getCommand("ban").setExecutor(new banCommand());
    		this.getCommand("unban").setExecutor(new unbanCommand());
    		this.getCommand("sethome").setExecutor(new sethomeCommand());
    		this.getCommand("home").setExecutor(new homeCommand());
    		//register listeners
    		PluginManager pm = this.getServer().getPluginManager();
    		pm.registerEvent(Event.Type.PLAYER_JOIN,playerListener, Event.Priority.Normal, this);
    	}
    	public void onDisable(){
    		log.info("Fiery Plugin Disabled - Goodbye!");
    	}
    	public void loadConfiguration()
    	{
    		getConfig().addDefault("settings.mysql.user", "mysql username");
    		getConfig().addDefault("settings.mysql.pass", "mysql password");
    		getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
    }
    
    sqlFunctions.java
    Code:
    package me.herghost.Fiery.functions;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    
    
    public class sqlFunctions
    
    {
    	private JavaPlugin plugin;
    
    
    	public void sqlFunction(JavaPlugin plugin) {
    	    this.plugin = plugin;
    	}
    	    String user = plugin.getConfig().getString("settings.mysql.user");
    	    String pass = plugin.getConfig().getString("settings.mysql.user");
    	    String url = "jdbc:mysql://localhost:3306/Fiery";
    	public void create_table_users() throws SQLException
    	{
    		Connection conn = DriverManager.getConnection(url, user, pass);
    		PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS users (p_id int NOT NULL AUTO_INCREMENT KEY, p_name varchar(255), p_ip varchar(255))");
    		sampleQueryStatement.executeUpdate();
    		sampleQueryStatement.close();
    		conn.close();
    	}
    
    	public void create_table_userhomes() throws SQLException
    	{
    		Connection conn = DriverManager.getConnection(url, user, pass);
    		PreparedStatement sampleQueryStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS userhomes (world varchar(128), p_name varchar(255) not null, home_x varchar(500), home_y varchar(500), home_z varchar(500), world1 varchar(128), home1_x varchar(500), home1_y varchar(500), home1_z varchar(500), world2 varchar(128), home2_x varchar(500), home2_y varchar(500), home2_z varchar(500), primary key(p_name))");
    		sampleQueryStatement.executeUpdate();
    		sampleQueryStatement.close();
    		conn.close();
    	}
    
    
    	}
    
    I donate a beer to anyone who can show me the light :)
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    @herghost you should post your error from your log
     
  13. Offline

    herghost

    Code:
    2011-11-24 00:22:41 [INFO] PermissionsBukkit v1.2 is now enabled
    2011-11-24 00:22:41 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2011-11-24 00:22:41 [INFO] Reload complete.
    2011-11-24 00:26:10 [WARNING] Can't keep up! Did the system time change, or is the server overloaded?
    2011-11-24 18:11:46 [INFO] Fiery Plugin Disabled - Goodbye!
    2011-11-24 18:11:46 [INFO] PermissionsBukkit v1.2 is now disabled
    2011-11-24 18:11:46 [INFO] Fiery Plugin Enabled - Beta
    2011-11-24 18:11:46 [SEVERE] Error occurred while enabling Fiery v0.1 (Is it up to date?): null
    java.lang.NullPointerException
    	at me.herghost.Fiery.functions.sqlFunctions.<init>(sqlFunctions.java:26)
    	at me.herghost.Fiery.Fiery.onEnable(Fiery.java:48)
    	at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:183)
    	at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:957)
    	at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:280)
    	at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:176)
    	at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:159)
    	at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:422)
    	at org.bukkit.Bukkit.reload(Bukkit.java:186)
    	at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
    	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:364)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:360)
    	at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:553)
    	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:530)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    2011-11-24 18:11:46 [INFO] PermissionsBukkit v1.2 is now enabled
    2011-11-24 18:11:46 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2011-11-24 18:11:46 [INFO] Reload complete.
    
    
     
  14. Offline

    Sagacious_Zed Bukkit Docs

    @herghost what is at line 26 of sqlFunctions.java?
     
  15. Offline

    herghost

    String user = plugin.getConfig().getString("settings.mysql.user");
     
  16. Offline

    Sagacious_Zed Bukkit Docs

    Okay one of two things may be happening

    either you are using one of the bukkit builds that require a default config file to be present in the jar due to a bug
    or you can't have getConfig() calls in the class field initializers, move it into a method and call that method after you construct it.
     
  17. Offline

    herghost

    I have a config.yml in the jar so it must be option 2 :) What exactly I am moving into a method and should I call it from Fiery or sqlFunctions?
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    @herghost
    you should probably call it after constructing it in Fiery
     
  19. Offline

    herghost

    I think that about does it, my java knowledge is just not advanced enough to figure this out. I guess I will have to go away and learn some more. Bloody frustrating :)
     
  20. Offline

    DDoS

    Your sqlFunctions constructor is invalid. It should look like this.


    PHP:
    package me.herghost.Fiery.functions;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import org.bukkit.plugin.java.JavaPlugin;

    public class 
    sqlFunctions{

        private 
    Plugin plugin;
        private 
    String user;
        private 
    String pass;
        private 
    String url;

        public 
    sqlFunction(Plugin plugin) {
            
    this.plugin plugin;
            
    this.user plugin.getConfig().getString("settings.mysql.user");
            
    this.pass plugin.getConfig().getString("settings.mysql.user");
            
    this.url "jdbc:mysql://localhost:3306/Fiery";

        }

    //rest of your code here
    Constructors should not have a return type. Also, declare your 'user', 'pass' and 'url' variables at the top of your class, then set them in the constructor. Also, you can use the Plugin class instead of JavaPlugin.
     
    herghost likes this.
  21. Offline

    herghost

    @DDoS

    Many thanks for your input!

    I have as above, however the declarations are being ignored? Just returning as null?

    works perfectly :) Do you have a paypal button or anything so I can buy you a beer?
     
  22. Offline

    DDoS

    Thank you for the offer, but I don't drink. You don't need to buy me anything anyways, I help for free! :)
     
    herghost likes this.
  23. Offline

    herghost

    @DDoS

    Thats kind :) Coould I possibly bug you for some more help on this subject? I also need to get the config file in my listener as when a player logs in for the first time it logs there IP address. I have tried applying the above method but I get a plugin not found error in the console. I think this is because the listener is defined before the onEnable, as well as loading the config file but I don't know how to go about editing the code to work in it.

    Fiery.java
    Code:
    package me.herghost.Fiery;
    
    import java.sql.SQLException;
    
    import java.util.logging.Logger;
    
    import me.herghost.Fiery.commands.banCommand;
    import me.herghost.Fiery.commands.giveCommand;
    import me.herghost.Fiery.commands.homeCommand;
    import me.herghost.Fiery.commands.itemCommand;
    import me.herghost.Fiery.commands.kickCommand;
    import me.herghost.Fiery.commands.mopCommand;
    import me.herghost.Fiery.commands.rmopCommand;
    import me.herghost.Fiery.commands.sethomeCommand;
    import me.herghost.Fiery.commands.spawnCommand;
    import me.herghost.Fiery.commands.unbanCommand;
    import me.herghost.Fiery.functions.sqlFunctions;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Fiery extends JavaPlugin {
    	Logger log = Logger.getLogger("Minecraft");
    
    	//Config File
    	public FileConfiguration config;
    	//The Listener
    	private final FieryPlayerListener playerListener = new FieryPlayerListener(this);
    	//OnEnable Methods
    	public void onEnable(){
    		//Initialize config file
    		loadConfiguration();
    
    //more code
    
    //register listeners
    		PluginManager pm = this.getServer().getPluginManager();
    		pm.registerEvent(Event.Type.PLAYER_JOIN,playerListener, Event.Priority.Normal, this);
    }
    //end of onEnable
    
    and my listener
    Code:
    package me.herghost.Fiery;
    
    import java.net.InetSocketAddress;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.plugin.Plugin;
    
    public class FieryPlayerListener extends PlayerListener
    
    {
    	private Plugin plugin;
        private String user;
        private String pass;
        private String url;
    
    
    public FieryPlayerListener(Plugin plugin)
    {
    	this.plugin = plugin;
        this.user = plugin.getConfig().getString("settings.mysql.user");
        this.pass = plugin.getConfig().getString("settings.mysql.pass");
        this.url = "jdbc:mysql://localhost:3306/Fiery";	
    }
    	public void onPlayerJoin(PlayerJoinEvent event)
    	{
    		String thisplayer = event.getPlayer().getName();
    		InetSocketAddress playerip = event.getPlayer().getAddress();
    		try
    		{
    			Connection conn = DriverManager.getConnection(url, user, pass);
    			ResultSet rs =conn.createStatement().executeQuery("SELECT * FROM users WHERE p_name = '" + thisplayer + "'");
    			if (!rs.next())
    			{
    				PreparedStatement sampleQueryStatement = conn.prepareStatement("INSERT INTO users (p_name,p_ip) VALUES ('" + thisplayer + "','" + playerip + "')");
    		        sampleQueryStatement.executeUpdate();
    				sampleQueryStatement.close();
    				conn.close();
    			}
    	    }
    	catch
    		(SQLException e1)
    			{
    				e1.printStackTrace();
            	}
    	}
    }
    
    Thanks
     
  24. Offline

    DDoS

    Could you post the error log?
     
  25. Offline

    herghost

    Code:
    2011-11-27 11:54:12 [SEVERE] Could not load 'plugins\Fiery.jar' in folder 'plugins':
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:175)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:215)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:136)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:141)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:117)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:52)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:141)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:388)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:229)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:131)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:125)
        at me.herghost.Fiery.FieryPlayerListener.<init>(FieryPlayerListener.java:25)
        at me.herghost.Fiery.Fiery.<init>(Fiery.java:29)
        ... 13 more
    2011-11-27 11:54:12 [INFO] Preparing level "world"
    2011-11-27 11:54:12 [INFO] Default game type: 1
    2011-11-27 11:54:12 [INFO] Preparing start region for level 0 (Seed: -4058256270512872989)
    2011-11-27 11:54:13 [INFO] Preparing start region for level 1 (Seed: -4058256270512872989)
    2011-11-27 11:54:13 [INFO] Preparing start region for level 2 (Seed: -4058256270512872989)
    2011-11-27 11:54:13 [INFO] Preparing spawn area: 0%
    2011-11-27 11:54:14 [INFO] PermissionsBukkit v1.2 is now enabled
     
  26. Offline

    DDoS

    You can't call getConfig() during initialization (so you can't use it for your player listener constructor). Remove the calls to the config from the constructor, and instead use a method to grab the config data and set it, and call it during your onEnable().
     
  27. Offline

    herghost

    @DDoS thanks again :)

    I have created a method called addConfig like so:

    Code:
    public class addConfig {
    	    public Plugin plugin;
    	    public String user;
    	    public String pass;
    	    public String url;
    	public void addConfiga(Plugin plugin)
    	{
    		this.plugin = plugin;
    	    this.user = plugin.getConfig().getString("settings.mysql.user");
    	    this.pass = plugin.getConfig().getString("settings.mysql.pass");
    	    this.url = "jdbc:mysql://localhost:3306/Fiery";
    	}
    
    }
    
    I then have this in my onEnable
    Code:
    addConfig method = new addConfig();
    			method.addConfiga(this);
    
    and my Listener:
    Code:
    public class FieryPlayerListener extends PlayerListener
    
    {
    	public String user;
    	public String pass;
    	public String url;
    	public void onPlayerJoin(PlayerJoinEvent event)
    	{
    		String thisplayer = event.getPlayer().getName();
    		InetSocketAddress playerip = event.getPlayer().getAddress();
    		try
    		{
    			Connection conn = DriverManager.getConnection(url, user, pass);
    			ResultSet rs =conn.createStatement().executeQuery("SELECT * FROM users WHERE p_name = '" + thisplayer + "'");
    			if (!rs.next())
    			{
    				PreparedStatement sampleQueryStatement = conn.prepareStatement("INSERT INTO users (p_name,p_ip) VALUES ('" + thisplayer + "','" + playerip + "')");
    		        sampleQueryStatement.executeUpdate();
    				sampleQueryStatement.close();
    				conn.close();
    			}
    	    }
    		
    and finally - my error :)

    Code:
    2011-11-27 19:31:04 [INFO] herghost [/127.0.0.1:58100] logged in with entity id 723 at ([world] 217.125, 65.0, -182.5)
    2011-11-27 19:31:04 [SEVERE] java.sql.SQLException: The url cannot be null
    2011-11-27 19:31:04 [SEVERE] 	at java.sql.DriverManager.getConnection(Unknown Source)
    2011-11-27 19:31:04 [SEVERE] 	at java.sql.DriverManager.getConnection(Unknown Source)
    2011-11-27 19:31:04 [SEVERE] 	at me.herghost.Fiery.FieryPlayerListener.onPlayerJoin(FieryPlayerListener.java:35)
    2011-11-27 19:31:04 [SEVERE] 	at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:260)
    2011-11-27 19:31:04 [SEVERE] 	at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
    2011-11-27 19:31:04 [SEVERE] 	at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:339)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.ServerConfigurationManager.c(ServerConfigurationManager.java:129)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:113)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:80)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.Packet1Login.a(SourceFile:59)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:46)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.NetworkListenThread.a(SourceFile:94)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
    2011-11-27 19:31:04 [SEVERE] 	at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    
    The server boots without error, this happens when a player logs in. Does Java read in lines? eg, the user and pass are defined and the last thing it looks at is the url?
     
  28. Offline

    DDoS


    That's not a method you created, but a class. Please google "java how to create a method". Make sure your method is in the PlayerListener, and is public, and not static.
     
  29. Offline

    herghost

    Thanks, I have just procured a copy of beginning java. I suspected for some time that I didn't actually know what I was doing and this confirms it :) I will be back when I have learnt some basics

    @DDoS

    Ok I think I am getting closer to understanding :)

    I now have this in FieryPlayerListener
    Code:
    {
    	public void initDB()
    	{
        this.user = plugin.getConfig().getString("settings.mysql.user");
    	    this.pass = plugin.getConfig().getString("settings.mysql.pass");
    	    this.url = "jdbc:mysql://localhost:3306/Fiery";
    	}
        public Fiery plugin;
        public String user;
        public String pass;
        public String url;
    
    	public void onPlayerJoin(PlayerJoinEvent event)
    	{
    		String thisplayer = event.getPlayer().getName();
    		InetSocketAddress playerip = event.getPlayer().getAddress();
    		try
    		{
    //do my stuff
    
    and I am trying to call the method initDB() in my main class like so:
    Code:
    public void onEnable(){
    		//Initialize config file
    		loadConfig();
    		//Initialize details from config.yml
    		FieryPlayerListener.initDB();
    
    However I get the error message 'Cannot make a static reference to the non-static method initDB() from the type FieryPlayerListener' - How or why is this seen as a static reference? I have been avoiding using static!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  30. Offline

    DDoS

    First, add the argument "FileConfiguration config" to your "initDB" method :

    PHP:
    public void initDB(FileConfiguration config) {
        
    this.user config.getString("settings.mysql.user");
        
    this.pass config.getString("settings.mysql.pass");
    }
    Also, you don't need to set your URL in the initDB, you can set in the constructor, since it's value doesn't depend on the config. Next, since your method is not a class method, you have to call it from an instance. Use the instance you created in your main class: "playerListener". Also, pass the argument "config" to the initDB method, as it is now required.

    PHP:
    public void onEnable() {
        
    //Initialize config file
        
    loadConfig();
        
    //Initialize details from config.yml
        
    playerListener.initDB(config);
    }
     
Thread Status:
Not open for further replies.

Share This Page