java - 使用 JDBC 时出错 "java.lang.NullPointerException"

标签 java android sql-server jdbc

我收到错误

java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference at 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();

最佳答案

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

调用之前:

connect.createStatement();

您必须首先通过以下方式初始化connect对象:

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

记住变量的范围。

关于java - 使用 JDBC 时出错 "java.lang.NullPointerException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32172636/

相关文章:

sql-server - 有没有办法通过 sql server 复制复制一些数据而不是数据库中的所有数据?

java - 如何在 Java 中识别和删除从我们的 webapp 发起的 Threads/ThreadLocals?

java - 使用 Maven 只重建必要的 jar

java - 如何使我的 ListView 行具有不同的高度

android - "zipAlignExe' 不存在。”在 Android Studio 上运行我的项目时出错

sql-server - 如何更改 SQL Server 中手动执行查询的超时?

c# - ADO.net数据表是变化时间

java - Java 如何防止使用 Random 类创建重复的数字?

java - 使用 If 语句中定义的变量?

android - 新的 SDK 中没有请求publish_actions