Reputation: 785
I have problem with AsyncTask in Android. I try to insert data from accelerometr into database, when user push Start. Normally, when user push the button Start, application hanged after 5 second. So I try put this function in AsyncTask, but i have problem with compilation:
01-21 17:44:58.515: E/AndroidRuntime(1131): FATAL EXCEPTION: AsyncTask #1
01-21 17:44:58.515: E/AndroidRuntime(1131): java.lang.RuntimeException: An error occured while executing doInBackground()
01-21 17:44:58.515: E/AndroidRuntime(1131): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-21 17:44:58.515: E/AndroidRuntime(1131): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.lang.Thread.run(Thread.java:841)
01-21 17:44:58.515: E/AndroidRuntime(1131): Caused by: java.lang.NullPointerException
01-21 17:44:58.515: E/AndroidRuntime(1131): at pl.pawelfrydrych.flyingball.MyTask.insertData(MyTask.java:37)
01-21 17:44:58.515: E/AndroidRuntime(1131): at pl.pawelfrydrych.flyingball.MyTask.doInBackground(MyTask.java:18)
01-21 17:44:58.515: E/AndroidRuntime(1131): at pl.pawelfrydrych.flyingball.MyTask.doInBackground(MyTask.java:1)
01-21 17:44:58.515: E/AndroidRuntime(1131): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-21 17:44:58.515: E/AndroidRuntime(1131): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-21 17:44:58.515: E/AndroidRuntime(1131): ... 4 more
MyTask class:
import java.util.Date;
import android.content.ContentValues;
import android.os.AsyncTask;
import android.util.Log;
public class MyTask extends AsyncTask<Void, Void, Void> {
private MainActivity main;
public Database myDBAdapter;
private int abc;
@Override
protected Void doInBackground(Void... arg0) {
try {
//18 line// insertData();
} catch (InterruptedException e) {
Log.d("appname","błąd w doInBackground");
e.printStackTrace();
}
return null;
}
public void insertData() throws InterruptedException{
ContentValues values = new ContentValues();
Date date = new Date();
while(true){
long millis = System.currentTimeMillis();
abc++;
values.put(Database.ID, 1+abc);
values.put(Database.LEFT_POSITION, main.xPosition);
values.put(Database.RIGHT_POSITION, main.yPosition);
values.put(Database.GPS, main.GPSposition);
values.put(Database.TIME, date.toString());
if(myDBAdapter.db != null){
myDBAdapter.db.insert("baza", null, values);
Thread.sleep(1000 - millis % 1000);
}else{
Log.d(Database.DB_NAME,"db is null");
}
}
}
}
MainActivity class:
public void onClick(View v) {
switch(v.getId()){
case R.id.bStart:
myDBAdapter = new Database(this).open();
new MyTask().execute();
case R.id.bStop:
myDBAdapter.close();
break;
}
}
Upvotes: 0
Views: 227
Reputation: 44571
It looks like you get a NPE
here
if(myDBAdapter.db != null){
in insertData()
because you never initialize myDBAdapter
. You declare it in your task
public Database myDBAdapter;
but you never initialize it before trying to use it.
It actually looks like you have an Activity
variable and an AsyncTask
variable so when you declare it in your task it is a local variable which isn't initialized. You can re-initialize that variable but if they are the same then it would be better to delete it and make sure it is a member variable if the task is an inner class of the Activity
. If its a separate file then you will need to initialize it or pass a reference to a constructor of the AsyncTask
class.
Upvotes: 2
Reputation: 3203
It doesn't look like you are initializing private MainActivity main;
with anything. You try to use it before it is initialized.
Upvotes: 0