0

[enter image description here][1]Im trying to code a minecraft bukkit plugin and i wanted to use mysql. I set up mysql and sqlgetter with no problem. In my other class, i wanted to call sqlgetter function so i put constructor but whenever i call function by constructor, it returns null. I tried everything that eclipse advised. But im kinda stuck. I got no error while starting up my server but as i said, when i use command sqlgetter constructor returns null.

OTHER CLASS TO CALL FUNCTION

package net.linex.komutlar;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import net.linex.mysql.SQLGetter;

public class TarafSecim implements CommandExecutor{



    private SQLGetter sqlgetter;
    
    public TarafSecim(SQLGetter sqlgetter) {
        this.sqlgetter = sqlgetter;
    }


    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
        Player p = (Player) sender;
        
        if(label.equalsIgnoreCase("side")) {
            
            if(args[0] == null) {
                p.sendMessage("You can choose Red or Blue sides");
            }
        
            
            if(args[0].equalsIgnoreCase("red")) {
                if(!sqlgetter.tarafvar(p.getUniqueId())) {
                    sqlgetter.tarafEkle(p.getUniqueId(), "kirmizi");
                }else {
                    p.sendMessage("You are already in a side.");
                }
            }  
            if(args[0].equalsIgnoreCase("blue")) {
                if(!sqlgetter.tarafvar(p.getUniqueId())) {
                    sqlgetter.tarafEkle(p.getUniqueId(), "mavi");
                }else {
                    p.sendMessage("You are already in a side.");
                }
            }
        }
        
        
        
        return false;
    }
}

SQLGETTER CLASS

public class SQLGetter {
    
    private Main plugin;
    
    public SQLGetter(Main plugin) {
        this.plugin = plugin;
    }

    

    public void createTable() {
        PreparedStatement ps;
        
        try {
            ps = plugin.mysql.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS taraf" + "(NAME VARCHAR(100),UUID VARCHAR(100), TARAF VARCHAR(100), PRIMARY KEY(NAME))");
            ps.executeUpdate();
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    public void createPlayer(Player p) {
        PreparedStatement ps;
        try {
            UUID uuid = p.getUniqueId();
            ps = plugin.mysql.getConnection().prepareStatement("SELECT * FROM taraf WHERE UUID=?");
            ps.setString(1, uuid.toString());
            ResultSet results = ps.executeQuery();
            results.next();
            
            if(!exist(uuid)) {
                PreparedStatement ps2 = plugin.mysql.getConnection().prepareStatement("INSERT IGNORE INFO taraf "
                        + "(NAME,UUID) VALUES(?,?)");
                ps2.setString(1, p.getName());
                ps2.setString(2, p.getUniqueId().toString());
                ps2.executeUpdate();
                return;
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }

EDIT:

public class Main extends JavaPlugin{

    public MySQL mysql;
    public SQLGetter sqlgetter;
    
    
    
//BAŞLANGIÇ 
    public void onEnable() {
        Logger log = Bukkit.getLogger();
        log.info("TheLine plugin is active");
        this.mysql = new MySQL();
        this.sqlgetter = new SQLGetter(this);
        try {
            mysql.connect();
        } catch (ClassNotFoundException | SQLException e) {
            //e.printStackTrace();
            log.info("Database is not activeted");
        }
        
        if(mysql.isConnected()) {
            log.info("database is activated");
            sqlgetter.createTable();
        }

I wrote that in main class before. Is that a reason to return null? [1]: https://i.stack.imgur.com/WVKZz.png

  • 2
    How did you construct `TarafSecim` and `SQLGetter`? – Rogue Mar 02 '23 at 15:28
  • 1
    Whichever code creates an instance of `TarafSecim` also needs to create an instance of `SQLGetter`. It sounds like your code calls `new TarafSecim(null)` somewhere instead. – f1sh Mar 02 '23 at 15:33
  • I looked that link but im not quite sure it is the answer. Could you please be more clear with first question "How did you construct tarafSecim and SQLGetter" – professorchemist Mar 02 '23 at 15:45
  • Im sorry for my lack of knowledge. Im newbie to object oriented programming – professorchemist Mar 02 '23 at 15:50
  • Your pastes do not contain the relevant code, hence, nobody can answer your question. The problem is that somebody calls `onCommand` on an instance of `TarafSecim`, and that instance has a `null` ref for its `SQLGetter sqlgetter;` field. Hence, you or some other code constructed that instance by calling `new TarafSecim(x)` where x is `null`. THAT code is broken, but it's not in your paste, so we can't help. – rzwitserloot Mar 02 '23 at 15:59
  • Actually it was all of it. There are other classes but not relevant. But let me check if i miss some – professorchemist Mar 02 '23 at 16:05

0 Answers0