Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Mysql with a java server

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mysql with a java server

    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:
            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;
            }
    My cmd prompt says it crashes at:
    Statement statement = con.createStatement();
    because of nullpointerexception

    However, I have another file that is doing pretty much the same thing and works!
    Here it is:

    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;
            }
    }
    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....


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,980
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: Mysql with a java server

    i get a NullPointerException....
    There is a variable with a null value on line xxx. Look at line xxx in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line xxx and print out the values of all the variables on that line.

    Get the value of xxx from the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mysql with a java server

    Quote Originally Posted by Norm View Post
    There is a variable with a null value on line xxx. Look at line xxx in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line xxx and print out the values of all the variables on that line.

    Get the value of xxx from the error message.
    '

    My cmd prompt says it crashes at:
    Statement statement = con.createStatement();
    because of nullpointerexception

    That's the line... Idk what's the null value?
    The command prompt says its that line. It's like line 49 or something....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,980
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: Mysql with a java server

    What is the value in con?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mysql with a java server

    Quote Originally Posted by Norm View Post
    What is the value in con?
    con = DriverManager.getConnection("jdbc:mysql://ip/data", "usern", "password");

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,980
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: Mysql with a java server

    That is the statement that is supposed to put a value in con, but it does not show what is in con when the code executes.
    Add a println() statement just before the statement with the NPE that prints the values of all the variables used on the line where the NPE happens. That will be con if you are correct in finding the line with the NPE.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Synchronize MySql DB from local to Server
    By m.vinay09@gmail.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2013, 10:38 PM
  2. Receive Server Sent Event Notifications in JAVA SERVER PAGE (JSP)
    By amritasenthilkumar in forum Web Frameworks
    Replies: 1
    Last Post: August 17th, 2012, 02:30 PM
  3. Replies: 0
    Last Post: June 23rd, 2012, 12:57 AM
  4. Local Domain MySQL server, access denied
    By techwiz24 in forum JDBC & Databases
    Replies: 1
    Last Post: February 29th, 2012, 10:29 PM
  5. Mysql url pointer to server instead of localhost
    By kurt-hardy in forum JDBC & Databases
    Replies: 3
    Last Post: April 12th, 2011, 04:32 AM

Tags for this Thread