-1

I'm new to java programming and I'm making a project which implies the use of java and mysql and I cannot manage to fix a NullPointerException in this piece of code, can anyone help me?

here's the full code, the connection part is working fine, its tested

package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import database.mainConnection;

public class userUtils extends mainConnection {

    public static void main(String[] args ) throws Exception {
        userLogin("mattia",hash("password");

    }


    /*public static Boolean registerUser(String name, String surname, String username, String email, String password, String Affiliation, String dateOfBirth) throws Exception {
        Connection con = getConnection();
        // 0000-00-00 format date

        PreparedStatement query = con.prepareStatement("INSERT INTO users (UserID, Username, Email, Password, Name, Surname, BirthDate, Affiliation) VALUES (?,?,?,?,?,?,?,?)");

        query.executeUpdate();
        return false;
    }*/

    public static Boolean userLogin(String username, String password) throws Exception {
        Boolean bool = null;
        try{
            Connection con = getConnection();
            PreparedStatement query = con.prepareStatement("SELECT * FROM users WHERE Username=? AND Password=?");
            query.setString(1, username);
            query.setString(2, password);

            ResultSet result = query.executeQuery();

            if(result.next()) {
                bool = true;
            }

            } catch(Exception e) {
                e.printStackTrace();
                }
        return bool;
    }
}

Also it doesn't give any results back, but i guess its due to the nullpointerexception

the error points at

con.prepareStatement("SELECT * FROM users WHERE Username=? AND Password=?"); and at a printlnine where i invoke the method
bummi
  • 27,123
  • 14
  • 62
  • 101
  • Where's the nullpointer? My guess is that `con` is `null` because `getConnection()` returns null – Mark Nov 27 '18 at 08:21
  • 6
    It's a pity that the exact location of the error is a secret. – glglgl Nov 27 '18 at 08:22
  • 1
    share your error stack trace. – Pooja Aggarwal Nov 27 '18 at 08:22
  • 3
    unfortunately, this is what you get when beginning programmers choose not to learn the basics. You should change: System.out.println(e); to e.printStackTrace(); , show that StackTrace in your question, and show in the code what line it points to. – Stultuske Nov 27 '18 at 08:23
  • why does your method signature contains "throws Exception", while it is impossible for your method to throw an Exception? – Stultuske Nov 27 '18 at 08:24
  • it is, but i made it return a null because eclipse asks to do so, i picked up the suggestion. i cant locate the nullpointer because it just appears in the catch, thats the big issue, i cant even locate the error – MATTIA PACCAMICCIO Nov 27 '18 at 08:24
  • @MATTIAPACCAMICCIO read my first comment, follow those steps, and you should be able to tell on exactly which line it occurs – Stultuske Nov 27 '18 at 08:25
  • Rest of the code looks fine this line should be causing Connection con = getConnection(); can u check do u get the connection object – Varun Nov 27 '18 at 08:26
  • java.lang.NullPointerException at database.userUtils.userLogin(userUtils.java:32) at database.userUtils.main(userUtils.java:13) – MATTIA PACCAMICCIO Nov 27 '18 at 08:26
  • which line is line 32? – Scary Wombat Nov 27 '18 at 08:27
  • we don't know what line that is, because we don't have your code. All I can see from this bit, is that you don't follow naming conventions – Stultuske Nov 27 '18 at 08:27
  • Post the code to getConnection, it is very likely it returns null – Joakim Danielson Nov 27 '18 at 08:40

2 Answers2

0

Instead of null assign false to your bool variable, try to use boolean a primitive datatype for return type of class and bool instead of wrapper class Boolean.

      Boolean bool = false;
arpita
  • 11
  • 2
-1
Boolean bool = null;

or

Connection con = getConnection();

should be the problem.

If the result returns nothing, bool will be null and returned by function.

And check for return of getConnection() is not null.

Giorgio
  • 1,973
  • 4
  • 36
  • 51
  • Boolean bool = null; can not possibly be the problem here. The Exception is thrown within that method (in the try block), not from the method calling this method. – Stultuske Nov 27 '18 at 08:44
  • Yes, you are right but according to what he said about his java knowledge and guess about exception, i thought he may have misunderstood the problem. He said, "here's the full code, the connection part is working fine, its tested" and "Also it doesn't give any results back, but i guess its due to the nullpointerexception". This is why i thought this way. – Mehmet Özdemir Nov 27 '18 at 10:46