sanjeev kaneria
sanjeev kaneria

Reputation: 67

java.lang.NullPointerException in search query

import java.sql.*;
import java.io.*;

public class User {
    String s1;
    int age;
    String s2;
    String s3;

    /**
     * @param args
     * @return
     */
    public void AddUser() throws SQLException {

        try {
            // TODO Auto-generated method stub

            BufferedReader str = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("Enter your name");
            s1 = str.readLine();
            System.out.println("Enter your age");
            age = Integer.parseInt(str.readLine());
            System.out.println("sex");
            s2 = str.readLine();
            System.out.println("DOB");
            s3 = str.readLine();
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:20june");
            String query = "insert into UserDetails values(?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, s1);
            ps.setInt(2, age);
            ps.setString(4, s2);
            ps.setString(3, s3);
            ps.executeUpdate();
            System.out.println("Record Inserted");
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void SearchUser() throws SQLException, IOException {
        String str1;
        try {
            BufferedReader str2 = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("Enter your search");
            str1 = str2.readLine();
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:20june");
            String query1 = "SELECT * FROM UserDetails WHERE UserName like'str1%'";
            PreparedStatement ps = con.prepareStatement(query1);
            ps.setString(1, str1);
            ps.executeUpdate();
            ps.close();
            con.close();
        }

        catch (NullPointerException e)

        {
            System.out.println(e);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public static void main(String[] args) throws IOException, SQLException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        User u1 = new User();

        {
            System.out.println("-------Menu-----------");
            System.out.println("1.Add User Details ");
            System.out.println("2.Search user");
            System.out.println("Enter your choice");
            int m = Integer.parseInt(br.readLine());
            switch (m) {
            case 1:
                u1.AddUser();
                break;
            case 2:
                u1.SearchUser();
                break;
            default:
                System.out.println("Invalid choice");
                break;
            }
        }
    }
}

Upvotes: 0

Views: 2488

Answers (3)

camickr
camickr

Reputation: 324118

Look at the stack trace to find the line that is causing the NPE. Then determine which variable is null and fix the code. We can't help you because we can't we can't execute the code because we don't have access to your database.

If the problem is that you don't know what line is causing the problem then print out the stack trace using:

e.getStackTrace();

when you catch the NPE.

I don't know much about SQL, but I use code like the following to specify parameters:

String sql = "INSERT INTO Page (Name, Title) VALUES (?, ?)";

PreparedStatement stmt = connection.prepareStatement(sql);

stmt.setString( 1, "Name1" );
stmt.setString( 2, "Title1" );
stmt.executeUpdate();

The idea is to keep the SQL simple so you con't have to worry about the delimiters.

Upvotes: 2

Marcelo
Marcelo

Reputation: 11308

You are not defining the parameter correctly in the query, use:

String query1 = "SELECT * FROM UserDetails WHERE UserName like ?";
PreparedStatement ps = con.prepareStatement(query1);
ps.setString(1, str1 + "%");

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

This search query is wrong:

String query1 = "SELECT * FROM UserDetails WHERE UserName like'str1%'";

Rather, do this:

String query1 = "SELECT * FROM UserDetails WHERE UserName like'%?%'";

Upvotes: 0

Related Questions