java


NullPointerException: Connection.createStatement() on a null object reference


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

Related Links

Syntax for image button
JOOQ getting an auto generated column value upon inserting it using the record, not returning
AWS API Gateway endpoint works fine in postman, returns 400 Bad Request in Android App
How can I make it so when the user inputs “n” times the program will stop?
How to implement a java method to set a user name
Is there a way to prevent web dynos from going to sleep?
How to open each email in gmail?
Error when execute the DFSReadWriteTest.scala using Apache Spark and Scala
Smartcard-io - I get an SCARD_F_COMM_ERROR when listing the connected cards several times
Best approach for designing similar classes while working with JPA and JSF
LDAP Password reset does not respect the pwdhistory attribute
Application was crashing after successful Login through android emulator
How to compile thymeleaf/jsp in java code?
After opening activity i won't replace the layout with fragment but i got exception
Shuffling grouped list with ordering such that element of same group does not appear one after another in java
What is the method “.getChildren()” used for in Java? [duplicate]

Categories

HOME
compiler-construction
pandas
image
bluetooth
gitlab
vbscript
angular-material
hp-exstream
relay
filtering
yarn
autotools
fancybox-3
spring-xd
handsontable
spring-tool-suite
leiningen
visual-studio-2005
jsprit
serverless-framework
worldwind
connection-string
object-detection
sylius
extjs5
opennlp
javacv
internet-explorer-9
nhapi
junit5
vsts-build-task
hybridauth
adobe-premiere
automake
unspecified
cookiecutter-django
ibpy
isbn
acoustics
xml-documentation
logparser
fancybox-2
csound
typed-lambda-calculus
np-complete
ifs
pdfclown
lync-client-sdk
crosswalk-runtime
jquery-filter
python-stackless
nessus
ios4
revolution-r
tcpserver
ios8-today-widget
intel-fortran
lib.web.mvc
sdhc
endeca-workbench
cannon.js
ios9.1
method-parameters
composite
p2
tablelayout
asp.net-dynamic-data
file-copying
braille
undefined-reference
didselectrowatindexpath
geos
ms-project-server-2010
easy-install
jplaton
flashvars
path-separator
rdoc
dsn
automount
cascalog
netbeans-6.9
pendrive
subgurim-maps
celltable
propagation
paster
dentrix
ncqrs
nintendo-ds
uimenucontroller
wsdl.exe
signal-handling
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App