0
btnLogin.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            // validation of login panel
            if(txtUser.getText().equalsIgnoreCase("") && txtPassword.getText().equalsIgnoreCase(""))
            {
                //JOptionPane.showMessageDialog(null, "in");
                lblErrUser.setVisible(true);
                lblErrPassword.setVisible(true);
            }
            else if (txtUser.getText().equalsIgnoreCase("") || txtPassword.getText().equalsIgnoreCase(""))
            {
                if(txtUser.getText().equalsIgnoreCase(""))
                    lblErrUser.setVisible(true);
                else
                    lblErrPassword.setVisible(true);
            }
            else
            {
                // checking with database the credentials
                try {

                    System.out.println(txtUser.getText().toString() + " : " + txtPassword.getText().toString());
                    resultuser = qp.checkUser(txtUser.getText().toString(),txtPassword.getText().toString()); 
                    if(resultuser == 0)
                    {
                        JFrame newFrame = new homeScreen();
                        newFrame.setVisible(true);
                        newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Wrong Credentials !!!");

                } 
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("ERROR n login panel");
                }

            }
        }
    });

qp is an object to class queryprocessing, pls fing below code:

    public class queryProcessing {

Statement stmt = null;
ResultSet rs = null;
Connection con = null;

public queryProcessing() {
    // TODO Auto-generated constructor stub
}

public int checkUser(String user, String pswd)
{
    System.out.print("In checking");
    String userdb = null;
    String pswddb = null;
    // TODO Auto-generated method stub
    String sql = ("SELECT * FROM login");
    try {
        stmt = (Statement) con.createStatement();
        rs = stmt.executeQuery(sql);

        while(rs.next()) 
        { 
            userdb = rs.getString("user"); 
            pswddb = rs.getString("password");
            System.out.print(userdb + " : " + pswddb);
            if(userdb.equalsIgnoreCase(user) && pswddb.equalsIgnoreCase(pswd))
                return 0;
            else
                return 1;
        }
    } 
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 1;
    }
    return 1;
}}     

ERROR:

a : a
java.lang.NullPointerException
ERROR n login panel
at loginPanel$2.actionPerformed(loginPanel.java:115)
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 java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)     

spend two days on this error. I just want to get return value for the below line where qp is an object of other class file refering to the method returning some value to a variable in another class file. But i am getting above error, no matter what datatype i change.

Pls help ???

resultuser = qp.checkUser(txtUser.getText().toString(),txtPassword.getText().toString());

Suraj G
  • 551
  • 1
  • 7
  • 15

1 Answers1

0

My first suggestion is to print qp to be sure that is not null.

System.out.println(txtUser.getText().toString() + " : " + txtPassword.getText().toString());
resultuser = qp.checkUser(txtUser.getText().toString(),txtPassword.getText().toString());

Your System.out.println(); ensures that the user and password is not null. Try to add qp to the system.out and check the result

King Midas
  • 1,442
  • 4
  • 29
  • 50
  • i have added more code for your understanding – Suraj G Feb 13 '18 at 09:20
  • I cannot execute your code. Please add as I have suggested the value of `qp` in the `System.out.println()` I have copied in my answer. I think that you will see a `null` value there. Something like `System.out.println(qp + "-->" + txtUser.getText().toString() + " : " + txtPassword.getText().toString());` – King Midas Feb 13 '18 at 09:23
  • hey @JorgeHortelano you might be right but problem is where you are suggesting me to print is before result. and i think , somehow i am getting new error at stmt = (Statement) con.createStatement(); .. now am confuse – Suraj G Feb 13 '18 at 09:45
  • If you are confused about the behaviour of your code, it is time for debugging. If you are confused with your own code, think how can it be for us. – King Midas Feb 13 '18 at 09:51
  • found the solution .. when i say confuse it was not for code it was for my further steps, like to go with your solution or shall i go with mine.. i have done lot R & D and searched more than 20 answers to get it done...anyways thanks for your help.. problem was not with qp it was with other class code.. i was using different code for db connection and other for query processing..so i now merged two class and BINGOOOO .. thanks again..if anyone need help regarding same..let me know ;) – Suraj G Feb 13 '18 at 10:09