簡體   English   中英

如何解決Android Studio中的NullPointerException錯誤?

[英]How to resolve NullPointerException error in android studio?

我正在嘗試將我的Android應用程序連接到MS SQL Server2008。我正在使用Android Studio。 這是我的MainActivity.java文件:

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends ActionBarActivity {

Button add;
TextView errorlbl;
EditText name, address, pincode;
Connection connect;
PreparedStatement preparedStatement;
Statement st;
String ipaddress, db, username, password;

@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 + ";";
        connection = DriverManager.getConnection(ConnectionURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return connection;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    add = (Button) findViewById(R.id.btnadd);

    errorlbl = (TextView) findViewById(R.id.lblerror);

    name = (EditText) findViewById(R.id.txtname);
    address = (EditText) findViewById(R.id.txtaddress);
    pincode = (EditText) findViewById(R.id.txtpincode);

    ipaddress = "192.168.0.9";
    db = "mydatabase";
    username = "myusername";
    password = "mypassword";
    connect = ConnectionHelper(username, password, db, ipaddress);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {


      connect = ConnectionHelper(username, password, db, ipaddress);
      st = connect.createStatement();

                preparedStatement = connect
                        .prepareStatement("insert into studentRecord(Name,Address,Pincode) values ('"
                                + name.getText().toString()
                                + "','"
                                + address.getText().toString()
                                + "','"
                                + pincode.getText().toString() + "')");
                preparedStatement.executeUpdate();
                errorlbl.setText("Data Added successfully");
            } catch (SQLException e) {
                errorlbl.setText(e.getMessage().toString());
            }

        }
    });

}
}

但我收到以下錯誤:

java.lang.NullPointerException: Attempt to invoke interface method   'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference at app.mysqlapp.MainActivity$1.onClick(MainActivity.java:77)

錯誤指向以下代碼

st = connect.createStatement();

我該如何解決?

我已經包含了jtds-1.2.7 jar文件,並且已經正確設置了清單文件。

我是Android開發的初學者。 請幫助我解決此問題。 謝謝!

jTDS的URL格式為

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

你的琴弦中有2個多余的; 和一個丟失的/

"jdbc:jtds:sqlserver://" + server + ";"     + "databaseName=" + database +
                                     ↑ here!   ↑ here!                                                                  

";user=" + user + ";password=" + password + ";";
                                             ↑ here!

我猜是這樣的:

"jdbc:jtds:sqlserver://" + server + "/databaseName=" + database + ";user=" + user + ";password=" + password;    

您將解決您的問題...如果沒有,請共享您的ConnectionURL字符串( System.out或調試),我將再次檢查結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM