0

I'm using JDBC for a school project in Android (I know it's not a good thing) and I can't understand why the createStatement return me null. I'm executing the jdbc code in an AsyncTask:

private class Inserisci extends AsyncTask<Void, Void, ResultSet> {

    @Override
    protected ResultSet doInBackground(Void... params) {

        String comando = "SELECT nome FROM prodotti";
        Connection c = null;
        try {
            c = DriverManager.getConnection("jdbc:mysql://192.168.137.1:3306/eshop", "root", "");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Statement s = null;
        try {
            s = c.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        comando = "SELECT nome FROM prodotti";
        try {
            return s.executeQuery(comando);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(ResultSet resultSet) {
        try {
            showMessage(resultSet.getString(1));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

192.168.137.1 is the IP of my computer running Xampp (Apache and MySQL turned on), the port is 3306 and username and password are the default ones. I'm running a simple query to print the first result but I can't get it. There is internet permission in the manifest. ShowMessage method is just a toast message. Any help? Thank you

Here's the full error log

05-15 21:01:55.925 11105-11185/com.quintabi.facchini.foobarmanager E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                     Process: com.quintabi.facchini.foobarmanager, PID: 11105
                                                                                     java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                         at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                         at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                         at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                         at java.lang.Thread.run(Thread.java:761)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
                                                                                         at com.quintabi.facchini.foobarmanager.MainActivity$Inserisci.doInBackground(MainActivity.java:128)
                                                                                         at com.quintabi.facchini.foobarmanager.MainActivity$Inserisci.doInBackground(MainActivity.java:114)
                                                                                         at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                         at java.lang.Thread.run(Thread.java:761) 
user207421
  • 305,947
  • 44
  • 307
  • 483
Stefano
  • 327
  • 1
  • 4
  • 17

1 Answers1

0

Wrap the code inside the same try catch clausule, it looks like you are not beign able to create the Connection.

Try this:

private class Inserisci extends AsyncTask<Void, Void, ResultSet> {

@Override
protected ResultSet doInBackground(Void... params) {

    String comando = "SELECT nome FROM prodotti";
    Connection c = null;
    try {
        c = DriverManager.getConnection("jdbc:mysql://192.168.137.1:3306/eshop", "root", "");
        Statement s = null;
        s = c.createStatement();
        comando = "SELECT nome FROM prodotti";
        return s.executeQuery(comando); 
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(ResultSet resultSet) {
    try {
        showMessage(resultSet.getString(1));
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}
Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20
  • can't really get why, but now the nullpointer exception is on the ResultSet: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.sql.ResultSet.getString(int)' on a null object reference at com.quintabi.facchini.foobarmanager.MainActivity$Inserisci.onPostExecute(MainActivity.java:136) seems that it can't fetch data... thanks for now – Stefano May 15 '17 at 19:20
  • That's because your ResultSet is null, maybe because your query is also null. Try to debug the values on your code, check the "c" value when you try to call the method "createStatement()", and the same with the "s" value when you try to call the "executeQuery(query)" method. – Jonathan Aste May 15 '17 at 19:22
  • That's why you should put all the code inside the same try block, that way you will be sure that the code can be executed without any exception – Jonathan Aste May 15 '17 at 19:25
  • Not recommended. There is a leaked `Statement` here, and, worse, a leaked `Connection`. You really cannot write a method that returns a `ReaultSet`. You need to refactor your code. – user207421 May 15 '17 at 21:03