1

I'm experimenting with Java Login method. Can someone explain to me why i get this NullPointerException . . .

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Remote implements ActionListener {
    Action  action;
    Gui     gui;
    String  output;
    Boolean result;

public Remote(Gui g, Action a) {
    action = a;
    gui = g;
    actionListenerMeth(this);
}

@Override
public void actionPerformed(ActionEvent arg0) {
    try {
        String a = gui.username_tf.getText();
        char[] c = gui.password_tf.getPassword();
        String b = new String(c);
        result = action.login(a, b);
    } catch (Exception ee) {
        ee.printStackTrace();
    }
    if (result == true) { //<--this is where Eclipse shows me the error ...
        output = "You are Successfuly Loged In!";
    } else {
        output = "Username or Password is Wrong!";
    }
    gui.result_lb.setText(output);
}

public void actionListenerMeth(ActionListener ae) {
    gui.login_bt.addActionListener(ae);
}

}

Here is the console log:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at nova.Remote.actionPerformed(Remote.java:29) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at

........etc

And here is my .login method in Action class:

import java.sql.*;

public class Action {
String url = "jdbc:mysql://localhost/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String password = "password";
Boolean result;
///
public Boolean login(String x, String y)
{
    String user_var = x;
    String pass_var = y;
    ///
    try {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url + dbName, username, password);
        Statement st = conn.createStatement();
        ResultSet res = st.executeQuery("SELECT * FROM java where username='"+user_var+"' and password='"+pass_var+"' ");

        while(res.next())
        {
            String user = res.getString("username");
            String pass = res.getString("password");

            if((user_var.equals(user)) && (pass_var.equals(pass))){
                result = true;
            }else{
                result = false;
            }
        }
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (result);
}

}

Besan Vadim
  • 441
  • 4
  • 8

3 Answers3

1

Looks like action.login(a, b); returns null.

So in your case the Boolean object null should be compared with a primitive false. So null will be converted to a primitive boolean using Boolean.booleanValue(), wich gives a NullPointerException.

So you have to change:

if (result == true) 

To

if (result != null && result == true) 
Jens
  • 67,715
  • 15
  • 98
  • 113
  • 1
    True, but presumably the OP doesn't think `login` should be returning null. – Radiodef Apr 24 '15 at 07:57
  • The question is `Can someone explain to me why i get this NullPointerException` the the answer is not wrong – Jens Apr 24 '15 at 07:59
1

You're declaring result as a Boolean object, not a boolean primitive. When if (result == true) executes, the VM attempts to convert result to a boolean primitive using a method call on result, which is null.

Solution: make result a boolean. This will implicitly be initialised as false.

Incidentally, you don't need to say if (result == true), you can just say if (result).

Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • 2
    *"Solution: make result a boolean."* No... this will just cause the exception to throw on a different line. – Radiodef Apr 24 '15 at 07:59
  • There are a few issues with this code, but the specific question asked is related to converting a null object to a primitive. – Steve Chaloner Apr 24 '15 at 08:01
  • 2
    It's not a *solution*. The problem is `login` returns `Boolean` which is null. – Radiodef Apr 24 '15 at 08:02
  • Yes it is, because even if `gui.username_tf.getText()` throws an exception, `result` will still be a valid primitive with a value of false (or, since it's an instance variable, whatever it was the last time - it should actually be a local variable). The, the test `if (result)` will evaluate to false instead of throwing an NPE. – Steve Chaloner Apr 24 '15 at 08:05
  • But also, yes - action.login should return a primitive or a non-null Boolean. Like I said, this code has other issues. – Steve Chaloner Apr 24 '15 at 08:06
1

You need to learn Autoboxing and Unboxing before you using box type Boolean. The error comes box type Boolean equals with unbox type boolean value true or false.If you use box type, you code should be

Boolean result;    
if (result != null) 

or if you use unbox type ,your code should be

boolean result;
if (result == true) {//TODO}

actually ubox boolean no need == . You can declare likes this

if (result){//DO SOMETHING}
Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
  • You're right, thanks! Just had to change from **Boolean** to **boolean** and `if(result){//...}` was enough. I'm new and i'm learning :) – Besan Vadim Apr 24 '15 at 22:58