【问题标题】:Error using JDBC "java.lang.NullPointerException" [duplicate]使用 JDBC “java.lang.NullPointerException”时出错 [重复]
【发布时间】:2015-08-23 23:40:16
【问题描述】:

我收到错误

java.lang.NullPointerException:尝试调用接口方法 'java.sql.Statement java.sql.Connection.createStatement()' 为空 对象引用 com.example.jacquesarroyo.onlinelibrary.suggestions$3.onClick(suggestions.java:81)

这是我使用的代码:

add = (Button) findViewById(R.id.btnadd);
errorlbl = (TextView) findViewById(R.id.lblerror);
title = (EditText) findViewById(R.id.txttitle);
author = (EditText) findViewById(R.id.txtauthor);
year = (EditText) findViewById(R.id.txtyear);
location = (EditText) findViewById(R.id.txtlocation);

ipaddress = "10.0.0.5";   
db = "SampleDatabase"; 
username = "admin"
password = "admin";
connect = ConnectionHelper(username, password, db, ipaddress);

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                st = connect.createStatement();
                preparedStatement = connect
                        .prepareStatement("Insert into Books(Title,Author,Year) values ('"
                                + title.getText().toString()
                                + "','"
                                + author.getText().toString()
                                + "','"
                                + year.getText().toString() + "')");
                preparedStatement.executeUpdate();
                errorlbl.setText("Data Added successfully");
            } catch (SQLException e) {
                errorlbl.setText(e.getMessage().toString());
            }
        }
    });

@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
                                    String database, String server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                + "databaseName=" + database + ";user=" + user
                + ";password=" + password + ";" + "ssl=false";
        connection = DriverManager.getConnection(ConnectionURL);


    } catch (SQLException se) {
        Log.e("ERROR", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERROR", e.getMessage());
    } catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }
    return connection;
}

第 81 行是:st = connect.createStatement();

【问题讨论】:

  • 由于没有createStatement() 调用或onClick() 方法,在您问题的代码中,我们无法真正帮助您。您可以考虑发布错误中引用的代码。
  • 错误出现在文件“suggestions.java”的第 81 行。哪些是第 81 行?
  • 第 81 行在:st = connect.createStatement();
  • 放上其余的代码。谢谢
  • 您必须从 ConnectionHelper 方法的日志中获取异常,因为它似乎返回了 null 引用。也许您使用的 URL 不正确。此外,Java 中的方法和变量名称应以小写字母开头(private Connection connectionHelper(...)String connectionURL 等)。

标签: java android sql-server jdbc


【解决方案1】:

NullPointerException 表示您尝试使用的变量未初始化或为null

调用前:

connect.createStatement();

你必须先初始化connect对象:

Connection connect = ConnectionHelper(user,password,database,server);

记住变量的范围。

【讨论】:

    猜你喜欢
    • 2018-11-28
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多