gpt4 book ai didi

Java Android Studio SQL ResultSet 由应用程序执行时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:56:10 24 4
gpt4 key购买 nike

我是新来的,或多或少也是 Java 编程的初学者。这也是我的第一个Android项目。目前我正在尝试使用 SQL 进行登录和帐户注册。

首先是代码:

数据库类:

import java.sql.*;

public class Database {

private Connection connection;
private Statement statement;
private ResultSet resultSet;
private PreparedStatement preparedStatement;



public ResultSet connectQuery(String query) {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

connection = DriverManager.getConnection("jdbc:mysql://...", "...", "...");
statement = connection.createStatement();
resultSet = statement.executeQuery(query);

} catch (Exception e) {
e.printStackTrace();
}

return resultSet;
}


public boolean alreadyExisting(String username) {
try {
String query = "SELECT username FROM users WHERE username = '" + username + "'";
ResultSet result = connectQuery(query);

return result.next();

} catch(SQLException e) {
System.out.println(e);
}
return true;
}
}

登录 Activity 类:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;

import de.simbo.songrequest.MainActivity;
import de.simbo.songrequest.R;
import de.simbo.songrequest.database.Database;

public class LoginActivity extends AppCompatActivity {

Database database;
private ImageButton login;
private ImageButton create;
private EditText username;
private EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
accountTest = new AccountTest();
database = new Database();
username = findViewById(R.id.Username);
password = findViewById(R.id.Password);
login = findViewById(R.id.Login);
create = findViewById(R.id.Create);

Intent intent = new Intent(this, CreateAccountActivity.class);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn(username.getText().toString(), password.getText().toString());
}
});

}

public void signIn(String username, String password) {
if (database.alreadyExisting(username)) {
if (database.getPassword(username).equals(password)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
}
}

主类:

import de.simbo.songrequest.account.CreateAccountActivity;
import de.simbo.songrequest.database.Database;
import de.simbo.songrequest.queue.Queue;
import de.simbo.songrequest.song.Song;

public class Main {

static Database database;

public static void main(String args[]) {

database = new Database();
database.alreadyExisting("timbo");
}
}

现在我的问题是:如果我从主类调用数据库中的alreadyExisting()方法,一切都很好。但是,一旦通过按下应用程序中的登录按钮来调用alreadyExisting(),它就会抛出以下错误:

    java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.sql.ResultSet.next()' on a null object reference
at de.simbo.songrequest.database.Database.alreadyExisting(Database.java:114)
at de.simbo.songrequest.account.LoginActivity.signIn(LoginActivity.java:53)
at de.simbo.songrequest.account.LoginActivity$2.onClick(LoginActivity.java:46)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

为什么仅当我使用该应用程序时才会出现此错误?

希望有人能帮助我。

提前致谢

最佳答案

我想我遇到了这个问题。看起来Android只能连接到SQLite而不能连接到MySQL。必须在两者之间运行例如 node.js 或 php 脚本。不过还是感谢您的回答。

关于Java Android Studio SQL ResultSet 由应用程序执行时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59671120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com