简体   繁体   English

如何解决Android Studio中的NullPointerException错误?

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

I am trying to connect my android app to MS SQL Server 2008. I am using Android Studio. 我正在尝试将我的Android应用程序连接到MS SQL Server2008。我正在使用Android Studio。 This is my MainActivity.java file: 这是我的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());
            }

        }
    });

}
}

But I'm getting the following error: 但我收到以下错误:

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)

The error points to the below code 错误指向以下代码

st = connect.createStatement();

How to I solve this? 我该如何解决?

I've included the jtds-1.2.7 jar file and I've properly set up the manifest file. 我已经包含了jtds-1.2.7 jar文件,并且已经正确设置了清单文件。

I am a beginner in Android Development. 我是Android开发的初学者。 Please help me to fix this issue. 请帮助我解决此问题。 Thanks! 谢谢!

URL format for jTDS , is: jTDS的URL格式为

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

In your string there are 2 extra ; 你的琴弦中有2个多余的; and a missing / : 和一个丢失的/

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

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

I guess with this: 我猜是这样的:

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

You will fix your issue... if not, please share your ConnectionURL string ( System.out or debugging) and I will doublecheck the result. 您将解决您的问题...如果没有,请共享您的ConnectionURL字符串( System.out或调试),我将再次检查结果。

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

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