简体   繁体   English

java.lang.NullPointerException **错误?

[英]java.lang.NullPointerException** error?

I am trying to add records to Sql Server in Java. 我试图将记录添加到Java中的Sql Server。 In this function, program gives an error like that: 在此函数中,程序给出如下错误:

Connected
1
2
Error java.lang.NullPointerException

It is the output.. 它是输出。

Function is below: 功能如下:

public class DB {

    Connection con = null;
    Statement stmt Nnull ;
    component cmp = new Component();

    public long pSave(Component cmp) {
        String i = cmp.getI();
        String s = cmp.getS();
        String a = cmp.getA();
        int t = cmp.getT();
        int c = cmp.getC();

        System.out.println("1");

        try {
            System.out.println("2");
            stmt = con.createStatement();

            System.out.println("3");
            String SQL =
                "INSERT INTO kisi (cl1,cl2,cl3,cl4,cl5) "
                + "VALUES(" + i + "," + s + "," + a + "," + c + "," + t + ")";

            System.out.println("4");
            stmt.executeUpdate(SQL);

            System.out.println("Success");

            return 1;
        } catch(Exception e) {
            System.out.println("Error " + e);
            return 0;
        }
    }

}
Connection con = null;
......
stmt = con.createStatement();
        ^

The connection is not initialized. 连接未初始化。 You have to connect to DB in order to retrieve data. 您必须连接到DB才能检索数据。

NullPointerException means that you're trying to call a method on a null object. NullPointerException意味着您正在尝试在空对象上调用方法。 That null object is your con that you never initialize. 空对象是您永远不会初始化的缺点。 You should add these two lines to initialize it: 您应该添加以下两行来对其进行初始化:

Driver d = (Driver)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433", "userName", "password");

You need to create a Connection object first. 您需要首先创建一个Connection对象。 You have: Connection con = null; 您有:Connection con = null;

So, you cant create a statement from a connection that is null. 因此,您不能从为空的连接创建语句。

你没有初始化连接con你是当你试图初始化声明,呼吁.. stmt = con.createstatement

A little bit of debugging may help. 一点调试可能会有所帮助。

You may notice your output says Connected 1 2 Error java.lang.NullPointerException . 您可能会注意到输出显示Connected 1 2 Error java.lang.NullPointerException The 1 and 2 suggest that you reached the first and second println statements and the Error NPE suggests that a line of code after those failed. 12建议您到达第一个和第二个println语句,而Error NPE建议在这些语句之后出现一行代码。 The third println is never hit, which means your NPE happened between 2 and 3. There exists a single statement between these two, and that is where you are getting your NPE. 第三个println永远不会命中,这意味着您的NPE发生在2到3之间。在这两个之间存在一个声明,这就是您获取NPE的地方。

So the question now is, what could be causing the NPE? 所以现在的问题是,什么会导致NPE? I will leave that as an exercise for the reader. 我将其留给读者练习。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM