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

How can I use a variable for the “expected” attribute of junit #Test?
Analyzing heap dum, Map#size() is not a function?
How to switch (migrate) to Google Cloud SQL from DataStore?
What image processing API should I use for a coloring book [closed]
Game of Life, Calculate neighbors Java
I encrypt data with 32 byte length using AES algorithm the result(cipher text) is 64 byte length,Is this valid?
Receive volume key press events when phone is in deep sleep
velocity template resource not found exception
Accessing properties based on context of WAR (Multiple WARs in single EAR)
Difference in Timestamp in PHP and Java
why does documentUrl return null
UnsupportedOperationException in using a javafx SimpleMapProperty
how to add Json header in json array parsed from Java Object
Enums in Processing 2.0
Execute a .app file from within a java program
How to read an input file char by char using a Scanner?

Categories

HOME
pdf
yahoo-oauth
microservices
sqlite3
windows-server
mouse
in-app-purchase
upload
elasticsearch-hadoop
izpack
google-cloud-spanner
gz
static-libraries
designer
caml
django-simple-history
progressive-web-apps
undefined
contextmenu
alpine
google-pagespeed
jprofiler
object-detection
ehcache
sox
mustache.php
devops
dxf
su
pingfederate
traffic
fluentvalidation
strncpy
subdomains
tasklet
srcset
sql-server-2012-express
ecto
bosh
copying
caret
y86
mixture-model
temporary-files
squib
eclipse-scout
node-gyp
eigenvalue
s
slick-3.0
angular2-meteor
btrace
executenonquery
outlook-2013
rainbowtable
theming
promela
jlink
typescript1.8
domain-model
elgg
brightcove
linode
objective-c-swift-bridge
jquery-filter
instant
rotativa
file-writing
packagist
dataview
system.management
netmq
microbenchmark
myo
remobjects
c3
cctv
asp.net-web-api-odata
wordpress-theme-customize
zend-route
heisenbug
reactfx
undefined-reference
cdc
ril
gridcontrol
plasma
specification-pattern
google-email-migration
dotnethighcharts
bigcouch
php-parser
mercurial-server
gcj
android-sdk-2.1
google-friend-connect
wise
thunderbird-lightning
brewmp
virtual-functions
eqatec
temporal-database
w3wp
data-acquisition
ajax-forms

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