Click here to Skip to main content
15,171,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to solve this issue since 2 days but I am not able to solve it. Basically I am want that if the result of
Java
pst = conn.prepareStatement("select * from employee where cnic = ?");
is returning empty set than
if(rs.next())
will execute and if it returning some data than program skip if and execute else part. I don't know where I am getting NullPointerException. I think I am getting exception after the execution of
pst = conn.prepareStatement("select * from employee where cnic = ?");
I don't how to solve it so please help me

Java
try {
                    while(!bool){
                        pst = conn.prepareStatement("select * from employee where cnic = ?");
                        System.out.print(s9);
                        pst.setString(1, s9);
                        rs = pst.executeQuery();
                        if(rs.next()){
                            pst = conn.prepareStatement("insert into employee(id,firstname,lastname,age,gender,job,salary,phone,cnic,email) values(?,?,?,?,?,?,?,?,?,?)");
                            pst.setString(1, s1);
                            pst.setString(2, s2);
                            pst.setString(3, s3);
                            pst.setString(4, s4);
                            pst.setString(5, s5);
                            pst.setString(6, s6);
                            pst.setString(7, s7);
                            pst.setString(8, s8);
                            pst.setString(9, s9);
                            pst.setString(10, s10);
                            JOptionPane.showMessageDialog(null, "Employee Added Successfully");
                            new EmployeeMenu().setVisible(true);
                            setVisible(false);
                            bool = true;
                        }else {
                            JOptionPane.showMessageDialog(null, "This CNIC Number is Already Registered");
                            s9 = JOptionPane.showInputDialog("Enter a correct CNIC Number:");                           
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(AddEmployee.class.getName()).log(Level.SEVERE, null, ex);
                }


What I have tried:

I tried many things and not able to solve the problem so please help me to solve this.
Posted
Updated 27-May-21 1:02am

Most likely the executeQuery does not return any records and thus rs is null.
Java
rs = pst.executeQuery();
if(rs.next()){

So calling next on a null object raises the exception you see.

Use your debugger to verify and see exactly what is happening.
   
Comments
Safin Mahesania 27-May-21 6:31am
   
@Richard MacCutchan But I want that if the result is empty than if runs and if result is not empty so what condition I should apply
Richard MacCutchan 27-May-21 7:01am
   
Like I said, you first need to use the debugger to check that this is the actual problem. If you confirm that the return value is null then you can test for that condition and take some action.
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
   
Comments
Richard MacCutchan 27-May-21 7:17am
   
I was trying to recover some data from my (paid for) cloud based backup system the other day and suddenly got a popup saying:
"Object reference not set to an instance of an object".

I had to laugh (briefly).
OriginalGriff 27-May-21 9:16am
   
I'd laughed - and then started to swear ... :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900