空对象引用上的 Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.cr
Posted
技术标签:
【中文标题】空对象引用上的 Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.createStatement()【英文标题】:Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.createStatement() on a null object reference 【发布时间】:2021-12-24 03:21:43 【问题描述】:我正在尝试创建一个连接到 Microsoft SQL Server 的应用程序,并将一些数据添加到一个表(供应商)中,但它一直拒绝我与服务器的连接。我只为这个数据库创建了一个用户和一个密码。我正在使用 MacBook,而服务器在另一台 Windows PC 上,但我认为这不是它无法正常工作的原因。对于ip,我使用了我在cmd中编写ipconfig时找到的IPv4。
这是代码:
public class connectClass
private Connection connection;
public Connection connectClass()
String ip = "won't put it here for obvious reasons";
String user = "android";
String password = "android";
String port = "1433";
String database = "dbExemplu";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String sirUrl = "jdbc:jtds:sqlserver://" + ip + ":" + port + ";" + "databasename=" + database
+ ";user=" + user + ";password=" + password + ";";
connection = DriverManager.getConnection(sirUrl);
catch (ClassNotFoundException e)
Log.e("Error", e.getException() + " " + e.getMessage());
catch (SQLException e)
Log.e("Error", e.getMessage());
return connection;
public class MainActivity extends AppCompatActivity
private Button btn1;
private EditText txtCode, txtName, txtPhone;
private Vendor vendor;
private Collections cols = new Collections();
private Connection connection;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCode = findViewById(R.id.codeVendor);
txtName = findViewById(R.id.nameVendor);
txtPhone = findViewById(R.id.phoneVendor);
btn1 = findViewById(R.id.saveVendor);
connectClass connect = new connectClass();
connection = connect.connectClass();
btn1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
vendor = new Vendor();
vendor(txtCode.getText().toString());
vendor.setName(txtName.getText().toString());
vendor.setPhone(txtPhone.getText().toString());
cols.vendors.add(vendor);
addVendor(vendor.getCode().toString(), vendor.getName().toString(), vendor.getPhone().toString);
);
void addVendor(String code, String name, String phone)
Statement st;
String s;
try
st = connection.createStatement();
s = "insert into Furnizori values ('" + code + "'" + ",'" + name + "','" + phone + "')";
st.executeUpdate(s);
catch (Exception e)
Log.e("Error", e.getMessage());
【问题讨论】:
【参考方案1】:我想通了。我的 SQL Server 身份验证设置为仅 Windows 身份验证,我必须将其设置为 SQL Server 身份验证。
【讨论】:
以上是关于空对象引用上的 Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.cr的主要内容,如果未能解决你的问题,请参考以下文章
更改组件可见性时 Android Studio 空对象引用错误
用于登录Android Studio的实时firebase数据库上的空指针异常[重复]
Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”
.getImage() 上的空对象引用 android [重复]
空对象引用上的 void android.view.View.dispatchWindowVisibilityChanged(int)'
NullPointerException:空对象引用上的 android.support.v4.app.FragmentHostCallback.getHandler()