MySQL SELECT/INSERT and Could not pass event

Discussion in 'Plugin Development' started by TheReelaatiiv, Feb 4, 2012.

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

    TheReelaatiiv

    Hello,

    i am currently writing my own plugin. The error message:

    Code:
    19:39:35 [SEVERE] Could not pass event org.bukkit.event.entity.EntityDamageEvent to ScoreSys
    java.lang.NullPointerException
            at org.thereelaatiiv.scoresys.ScoreSysListener.createUser(ScoreSysListener.java:75)
            at org.thereelaatiiv.scoresys.ScoreSysListener.onEntityDamage(ScoreSysListener.java:38)
            at org.bukkit.plugin.java.JavaPluginLoader$66.execute(JavaPluginLoader.java:729)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
            at net.minecraft.server.Entity.burn(Entity.java:703)
            at net.minecraft.server.Entity.move(Entity.java:624)
            at net.minecraft.server.EntityLiving.a(EntityLiving.java:793)
            at net.minecraft.server.EntityHuman.a(EntityHuman.java:1102)
            at net.minecraft.server.EntityLiving.d(EntityLiving.java:973)
            at net.minecraft.server.EntityHuman.d(EntityHuman.java:311)
            at net.minecraft.server.EntityLiving.y_(EntityLiving.java:339)
            at net.minecraft.server.EntityHuman.y_(EntityHuman.java:159)
            at net.minecraft.server.EntityPlayer.a(EntityPlayer.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:316)
            at net.minecraft.server.Packet10Flying.handle(SourceFile:126)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:536)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    The source code from ScoreSysListener.java is:
    Code:
    package org.thereelaatiiv.scoresys;
     
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
     
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.thereelaatiiv.scoresys.ScoreSys;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
     
    @SuppressWarnings({ "deprecation", "unused" })
    public class ScoreSysListener extends EntityListener {
    publicstaticScoreSysplugin;
    privateConnectioncon;
    privateBooleanppccrt = false;
    public ScoreSysListener(ScoreSys mp) {
    plugin = mp;
    try {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/minecraft", "minecraft", "***");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public void onEntityDamage(EntityDamageEvent e){
    if(ppccrt == false) {
    createUser("test");
    }
    int thedamage = e.getDamage();
    String reason = e.getCause().toString();
    if(reason == "ENTITY_ATTACK") {
    if(e.getEntity() instanceof Player) {
    Player receiver = (Player) e.getEntity();
    receiver.sendMessage(receiver.getName().toLowerCase() + " " + thedamage);
    }
    if(((EntityDamageByEntityEvent) e).getDamager() instanceof Player) {
    Player sender = (Player)((EntityDamageByEntityEvent) e).getDamager();
    sender.sendMessage(sender.getName().toLowerCase() + " " + thedamage);
    }
    }
    else if (e.getCause() == DamageCause.PROJECTILE) {
    if(e.getEntity() instanceof Player) {
    Player receiver = (Player) e.getEntity();
    receiver.sendMessage(receiver.getName().toLowerCase() + " " + thedamage);
    }
    EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
    Projectile arrow = (Projectile) event.getDamager();
    if (arrow.getShooter() instanceof Player) {
    Player sender = (Player)(arrow.getShooter());
    sender.sendMessage(sender.getName().toLowerCase() + " " + thedamage);
    }
    }
    else if(reason == "CONTACT" || reason == "SUFFOCATION" || reason == "FALL" || reason == "FIRE" || reason == "FIRE_TICK" || reason == "LAVA" || reason == "DROWNING" || reason == "BLOCK_EXPLOSION" || reason == "ENTITY_EXPLOSION" || reason == "VOID" || reason == "LIGHTNING" || reason == "SUICIDE" || reason == "STARVATION" || reason == "POISON" || reason == "MAGIC" || reason == "CUSTOM") {
    if(e.getEntity() instanceof Player) {
    Player receiver = (Player) e.getEntity();
    receiver.sendMessage(receiver.getName().toLowerCase() + " " + reason + " " + thedamage);
    }
    }
    }
    public void createUser(String Username) {
    PreparedStatement sampleQueryStatement_select;
    System.out.println("TESTv1");
    try {
    sampleQueryStatement_select = con.prepareStatement("SELECT `id` FROM `scoresys` WHERE `username` = '" + Username + "'");
    sampleQueryStatement_select.executeUpdate();
    if(sampleQueryStatement_select.getMetaData().getColumnCount() == 0) {
    PreparedStatement sampleQueryStatement_insert;
    sampleQueryStatement_insert = con.prepareStatement("INSERT INTO `scoresys` SET `username` = '" + Username + ", `score` = 0");
    sampleQueryStatement_insert.executeUpdate();
    sampleQueryStatement_insert.close();
    System.out.println("Created user " + Username);
    ppccrt = true;
    }
    sampleQueryStatement_select.close();
     
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    where
    Code:
    sampleQueryStatement_select = con.prepareStatement("SELECT `id` FROM `scoresys` WHERE `username` = '" + Username + "'");
    is the line from the error message.

    Please help me :-( (PS: I am a noob at writing java plugins... I am currently learning it, so please explain more what to do!)
     
  2. Offline

    KingWar92

    Can you give us voïd "con.prepareStatement" ?
     
  3. Offline

    TheReelaatiiv

    It's part from import java.sql.PreparedStatement;

    I also have changed my code:
    Code:
    package org.thereelaatiiv.scoresys;
     
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityListener;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.thereelaatiiv.scoresys.ScoreSys;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    @SuppressWarnings({ "deprecation", "unused" })
    public class ScoreSysListener extends EntityListener {
    publicstaticScoreSysplugin;
    publicConnectioncon;
    publicBooleanppccrt = false;
     
    public ScoreSysListener(ScoreSys mp) {
    plugin = mp;
    try {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/minecraft?user=minecraft&password=***");
    System.out.println(":::" + con.getMetaData().getURL());
    } catch (SQLException e) {
    System.out.println("SQLException: " + e.getMessage());
    System.out.println("SQLState: " + e.getSQLState());
    System.out.println("VendorError: " + e.getErrorCode());
    }
    }
    public void onEntityDamage(EntityDamageEvent e){
    if(ppccrt == false) {
    createUser("test");
    }
    int thedamage = e.getDamage();
    String reason = e.getCause().toString();
    if(reason == "ENTITY_ATTACK") {
    if(e.getEntity() instanceof Player) {
    Player receiver = (Player) e.getEntity();
    receiver.sendMessage(receiver.getName().toLowerCase() + " " + thedamage);
    }
    if(((EntityDamageByEntityEvent) e).getDamager() instanceof Player) {
    Player sender = (Player)((EntityDamageByEntityEvent) e).getDamager();
    sender.sendMessage(sender.getName().toLowerCase() + " " + thedamage);
    }
    }
    else if (e.getCause() == DamageCause.PROJECTILE) {
    if(e.getEntity() instanceof Player) {
    Player receiver = (Player) e.getEntity();
    receiver.sendMessage(receiver.getName().toLowerCase() + " " + thedamage);
    }
    EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
    Projectile arrow = (Projectile) event.getDamager();
    if (arrow.getShooter() instanceof Player) {
    Player sender = (Player)(arrow.getShooter());
    sender.sendMessage(sender.getName().toLowerCase() + " " + thedamage);
    }
    }
    else if(reason == "CONTACT" || reason == "SUFFOCATION" || reason == "FALL" || reason == "FIRE" || reason == "FIRE_TICK" || reason == "LAVA" || reason == "DROWNING" || reason == "BLOCK_EXPLOSION" || reason == "ENTITY_EXPLOSION" || reason == "VOID" || reason == "LIGHTNING" || reason == "SUICIDE" || reason == "STARVATION" || reason == "POISON" || reason == "MAGIC" || reason == "CUSTOM") {
    if(e.getEntity() instanceof Player) {
    Player receiver = (Player) e.getEntity();
    receiver.sendMessage(receiver.getName().toLowerCase() + " " + reason + " " + thedamage);
    }
    }
    }
    public void createUser(String Username) {
    Statement stmt = null;
    ResultSet rs = null;
    int selectcount = -1;
    try {
     
    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT `id` FROM `scoresys` WHERE `username` = '" + Username + "'");
    rs = stmt.getResultSet();
    if(rs != null) {
    try {
    selectcount = rs.getMetaData().getColumnCount();
    rs.close();
    } catch (SQLException ers) {
    // Ignore
    }
    }
    if(selectcount == 0) {
    rs = stmt.executeQuery("INSERT INTO `scoresys` SET `username` = '" + Username + "', `score` = 0");
    }
    if(rs != null) {
    try {
    rs.close();
    } catch (SQLException ers) {
    // Ignore
    }
    }
    if(stmt != null) {
    try {
    stmt.close();
    } catch (SQLException ers) {
    // Ignore
    }
    }
    } catch (SQLException e) {
    System.out.println("SQLException: " + e.getMessage());
    System.out.println("SQLState: " + e.getSQLState());
    System.out.println("VendorError: " + e.getErrorCode());
    }
    }
     
    }
    The error message is:
    Code:
    16:36:59 [SEVERE] Could not pass event org.bukkit.event.entity.EntityDamageEvent to ScoreSys
    java.lang.NullPointerException
            at org.thereelaatiiv.scoresys.ScoreSysListener.createUser(ScoreSysListener.java:77)
            at org.thereelaatiiv.scoresys.ScoreSysListener.onEntityDamage(ScoreSysListener.java:40)
            at org.bukkit.plugin.java.JavaPluginLoader$66.execute(JavaPluginLoader.java:729)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:453)
            at net.minecraft.server.Entity.am(Entity.java:272)
            at net.minecraft.server.EntityLiving.am(EntityLiving.java:172)
            at net.minecraft.server.Entity.y_(Entity.java:195)
            at net.minecraft.server.EntityLiving.y_(EntityLiving.java:327)
            at net.minecraft.server.EntityHuman.y_(EntityHuman.java:159)
            at net.minecraft.server.EntityPlayer.a(EntityPlayer.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:316)
            at net.minecraft.server.Packet10Flying.handle(SourceFile:126)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:537)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    
    The error line from above is:
    Code:
    Statement stmt1 = (Statement) con.createStatement();
     
  4. Offline

    TheReelaatiiv

    I've done it: I've created the same plugin in a completely new style (no deprecations, etc.).
    If anybody needs the source, i'll post it here/on pastebin.com.
     
  5. Offline

    Marcin Majewski

    I have the same problem. Can you help?
     
  6. Offline

    KingWar92

    A lot of thing can trough this exception !
     
  7. Offline

    TnT

    Moved to correct forum.
     
Thread Status:
Not open for further replies.

Share This Page