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 to check in a file into TFS using Java SDK
DynamoDB Stream- AWS Lambda Java API - output format not JSON
Scala RegEx String extractors behaving inconsistently
How to bounce an image between the boundaries
log to separate files using logback and apache camel routes
jersey error handling + generics response object
Sorting three arrays using third array as pivot
Save content of ArrayList to external storage
Fetch primary key generated by sequence using jdbctemplate / jdbc
Where can i find the maven dependencies for Webshpere eXtreme Scale?
onQueryTextSubmit brings to relaunching MainActivity
Dynamically create multiple ImageViews
ProgressBar working on emulator and not on phones
osgi.dev Property in eclipse 4.5
Apache FOP - Table component getting misaligned from second page onwards
How to measure the performance speed of one method in java [duplicate]

Categories

HOME
client
webpack
proxy
reflection
alfresco
filtering
yarn
sqlite3
jgroups
serverspec
datastax-java-driver
bs4
medical
google-translate
dax
correlation
google-cloud-spanner
task
systemc
finite-automata
karma-jasmine
plunker
zapier
riot.js
opencover
jquery-ajaxq
cloudhub
google-search-api
chromium-embedded
realex-payments-api
elasticsearch-ruby
gitignore
google-cloud-nl
react-chartjs
siesta-swift
copying
openoffice.org
wallpaper
android-ble
wixsharp
http-digest
io-redirection
swift3.0.2
withings
cookiecutter-django
tasker
android-tabhost
grid.mvc
core-plot
mime
wso2carbon
qwt
directory-structure
background-service
httplistener
rotativa
connect-by
react-native-listview
prettytensor
livequery
ptrace
two-factor-authentication
associative-array
natvis
execute
sonarqube5.1.2
c++03
javax.mail
geonetwork
website-monitoring
qpainter
network-interface
ceil
tld
starcluster
dealloc
balanced-payments
multiprocessor
jsctypes
commoncrypto
html4
referrer
angularjs-controller
simba
funscript
word-processor
newtonscript
twrequest
nsdatecomponents
nsobject
f#-powerpack
telerik-scheduler
remember-me
h.323
createwindow
web-application-design
suppress
perfect-hash
web-architecture
iweb
text-coloring
defensive-programming





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