Okay so ill tell you my idea before showing code:
I want to do a recruit a friend where you go to our website, type in your username, the person that recruited you username and you hit submit. When you do that it stores the person that was recruited ip and username into a database and in another it stores the credit for the person that recruited that person. Then you, as the person getting credit, logs in on the server and types a command like ::claimreward and then it goes out to the MYSQL database and checks for their credit then credits them.
I got the website working perfectly, however when I try and do my java code i get a NullPointerException....
Heres my code:
package com.rs.sql; import java.sql.*; import java.security.MessageDigest; public class Raf { public static Connection con = null; public static Statement stm; public static void createConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql://ip/data", "usern", "password"); stm = con.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public static ResultSet query(String s) throws SQLException { try { if (s.toLowerCase().startsWith("select")) { ResultSet rs = stm.executeQuery(s); return rs; } else { stm.executeUpdate(s); } return null; } catch (Exception e) { System.out.println("MySQL Error:"+s); e.printStackTrace(); } return null; } public static void destroyCon() { try { stm.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } public static boolean checkVotes(String playerName) { try { Statement statement = con.createStatement(); String query = "SELECT * FROM credit WHERE recruiter = '" + playerName.replaceAll("_", " ") + "'"; ResultSet results = statement.executeQuery(query); while(results.next()) { int recieved = results.getInt("amount"); if(recieved == 0) { return true; } } } catch(SQLException e) { e.printStackTrace(); } return false; } public static boolean voteGiven(String playerName) { try { query("UPDATE credit SET recieved = 1 WHERE recruiter = '" + playerName.replaceAll("_", " ") + "'"); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
When executing this code:
My cmd prompt says it crashes at:public static boolean checkVotes(String playerName) { try { Statement statement = con.createStatement(); String query = "SELECT * FROM credit WHERE recruiter = '" + playerName.replaceAll("_", " ") + "'"; ResultSet results = statement.executeQuery(query); while(results.next()) { int recieved = results.getInt("amount"); if(recieved == 0) { return true; } } } catch(SQLException e) { e.printStackTrace(); } return false; }
because of nullpointerexceptionStatement statement = con.createStatement();
However, I have another file that is doing pretty much the same thing and works!
Here it is:
And the above works 100%.... I am just copying and pasting the above and changing the database, user, pass, and other stuff within that, but idk why it's getting a nullpointerexception....package com.rs.sql; import java.sql.*; import java.security.MessageDigest; public class SQL { public static Connection con = null; public static Statement stm; public static void createConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql://ip/data", "user2", "password"); stm = con.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public static ResultSet query(String s) throws SQLException { try { if (s.toLowerCase().startsWith("select")) { ResultSet rs = stm.executeQuery(s); return rs; } else { stm.executeUpdate(s); } return null; } catch (Exception e) { System.out.println("MySQL Error:"+s); e.printStackTrace(); } return null; } public static void destroyCon() { try { stm.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } public static boolean checkVotes(String playerName) { try { Statement statement = con.createStatement(); String query = "SELECT * FROM votes WHERE username = '" + playerName.replaceAll("_", " ") + "'"; ResultSet results = statement.executeQuery(query); while(results.next()) { int recieved = results.getInt("recieved"); if(recieved == 0) { return true; } } } catch(SQLException e) { e.printStackTrace(); } return false; } public static boolean voteGiven(String playerName) { try { query("UPDATE votes SET recieved = 1 WHERE username = '" + playerName.replaceAll("_", " ") + "'"); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }