搜索查询中的java.lang.NullPointerException

时间:2011-06-22 05:16:55

标签: java sql

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;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

查看堆栈跟踪以找到导致NPE的行。然后确定哪个变量为null并修复代码。我们无法帮助您,因为我们无法执行代码,因为我们无法访问您的数据库。

如果问题是您不知道导致问题的是哪一行,则使用以下方法打印出堆栈跟踪:

e.getStackTrace();

当你赶上NPE时。

我对SQL知之甚少,但我使用以下代码来指定参数:

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

PreparedStatement stmt = connection.prepareStatement(sql);

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

这个想法是保持SQL简单,所以你不必担心分隔符。

答案 1 :(得分:0)

此搜索查询错误:

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

相反,这样做:

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

答案 2 :(得分:0)

您没有在查询中正确定义参数,请使用:

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