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

Environment properties not getting read in spring boot
Extract Substring from String java
error when merge using hibernate 3 and ojdbc 7 [duplicate]
Can I get the instance of the calling object in Java?
Spring Boot load beans from context xml in library
How to kill all active processes from Android phone with code in my app?
proper use reactor to async process
Can't get string comparison right
java.io.FileNotFoundException: I:\FTP 23-04-2017 (Acceso denegado) / (Access denied)
How to calculate multiple user input (IF statements ) to return a total
Is there any visual tool for writing Java classes in Bytecode for Eclipse or IntelliJ? [closed]
Unable to set SOAP Header while calling Web Service through Camel using dataFormat as POJO
JAR not using resources correctly (maven)
spring bean Autowired failed on redeploy
How do you parse a log file by identifying issues
parseInt() acting strangely after substring operation to remove characters

Categories

HOME
urbancode
gremlin
cmd
amazon-ecs
binary-tree
disassembler
retrofit
baqend
ezpublish
bs4
pheatmap
slick-slider
midi
modelica
django-simple-history
finite-automata
facebook-page
propel
ab-testing
introduction
java-3d
jasonette
web-sql
chromium-embedded
sqlcipher
smb
xpages-ssjs
numerical-methods
skia
preg-match
directx-10
nameservers
airconsole
http-referer
sequential
file-format
unobtrusive-validation
turbogears
total-commander
wptoolkit
cubic-spline
skeleton-css-boilerplate
jquery-nestable
rails-routing
dandelion
winscp-net
tropo
mplayer
darcs
crypt
akka-cluster
orthogonal
blacklist
google-cdn
medium.com
visual-c++-2008
radtreelist
angstrom-linux
netmq
phpcas
historian
lib.web.mvc
fouc
coveralls
browser-bugs
apache-commons-fileupload
processmodel
iiviewdeckcontroller
sonarqube5.1.2
kineticjs
fpml
id3v2
xcode-6.2
google-reader
p4java
wp-query
hippomocks
transcoding
srs
flexmojos
commoncrypto
apc
xamlparseexception
mhtml
jquery-knob
cisco-jtapi
word-processor
postgresql-performance
pendrive
asyncfileupload
pydot
datareader
fluent-interface
sortable-tables
sproutcore-2
swing-app-framework
eqatec
port-number
genealogy
document-conversion
uiq3

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