0

I'm new here and also more or less a beginner in programming Java. This is also my first Android project. At the moment I'm trying to do login and account registration with SQL.

At first the code:

Database class:

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;
    }
}

LoginActivity class:

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);
            }
        }
    }
}

Main class:

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");
    }
}

Now my problem: If I call the alreadyExisting() method in Database from main class everything is fine. But as soon as alreadyExisting() is called by pressing the login button in the app it throws the following error:

    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)

Why does this error only occur if I'm using the app?

I hope somebody can help me.

Thanks in advance

Fearix
  • 1
  • 1
  • Have you checked in a debugger to see if the app is able to successfully connect to the database, and whether the database is set up properly in the environment that the app is running in? (I'd check first whether the connection to the database is happening correctly; this is just a hunch, but I strongly suspect that it might be failing at that point). – EJoshuaS - Stand with Ukraine Jan 09 '20 at 19:52
  • Quick point: when you post a question on SO, can you be sure to stick around for awhile to reply to comments? Otherwise it makes it harder for people to help. – EJoshuaS - Stand with Ukraine Jan 09 '20 at 22:39

1 Answers1

0

Think I got the issue. Looks like Android can only connect to SQLite not to MySQL. Have to run for example a node.js or php script in between. Nevertheless thanks for your answers.

Fearix
  • 1
  • 1