Wednesday, May 1, 2013

[android help] SQL Exception error when open connection using SQL Helper Android Eclipse


I am having terrible trouble with a database connection using eclipse for an android app


I had a working example of this but since i have adapted it i have been unable to open a database connection.


I have a main activity that opens a "getData" in an Async Task


This downloads a JOSN formatted document, parses and passes to "writeSync" class


This class is designed to open the database connection add the records and then close the connection.


I am receiving an error when i run the database.open method but am unable to trap the specific error.


I have checked that i am not passing Null values and that the Helper object is populated.


Here is my code if anyone can spot my problem.


I have also written a wrapper class to pass the context when the database is opening


MAIN ACTIVITY



package com.example.jsondb;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;

public class MainActivity extends Activity {
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getContext contextrequest = new getContext();

AsyncTask JSONData;
//Get JSON Data as a string
//Update display#
//SyncDataProducts();
JSONData = new GetData().execute("null");

String WriteDB = "";
//pass JSONData to database to be written
//update display

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


GETDATA



package com.example.jsondb;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.util.Log;

public class GetData extends AsyncTask {


public String getServerData(String strURL) {
InputStream is = null;

ArrayList nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("code","A"));

//get http data
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}


//convert response to string
String result = "";
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

result=sb.toString();

//send data to database
AsyncTask WriteData;
WriteData = new WriteSync().execute(result);

return result;

}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}




return result;





}

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return getServerData("http://500kgiveaway.co.uk/android/products.php");
}
}


WRITESYNC



package com.example.jsondb;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class WriteSync extends AsyncTask {
private Context adapterContext;
public String WriteSyncData(String result) {
String httpResponce = "";
String returnstring = "";
httpResponce = result;


adapterContext = getContext.getContextNow();

DataSource datasource;
datasource = new DataSource(adapterContext);

datasource.open();


try{
JSONArray jArray = new JSONArray(httpResponce);
for(int i=0;i JSONObject json_data = jArray.getJSONObject(i);

datasource.createProduct(json_data.getString("stockref"), json_data.getString("title"), json_data.getString("price"));

//Get an output to the screen
returnstring += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}


return returnstring;


}

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

return WriteSyncData(params[0]);
}
}


MYSQLHELPER



package com.example.jsondb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper{
//Product Table
public static final String TABLE_NAME = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_STOCKREF = "stockref";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_PRICE = "price";

private static final String DATABASE_NAME = "Logma.db";
private static final int DATABASE_VERSION = 2;

// Database creation sql statement

private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME
+ "(" + COLUMN_ID + " integer primary key autoincrement,"
+ COLUMN_STOCKREF + " text not null,"
+ COLUMN_TITLE + " text not null,"
+ COLUMN_PRICE + " text not null"
+ ");";


public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

}


DATA SOURCE



package com.example.jsondb;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns_products = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_STOCKREF,MySQLiteHelper.COLUMN_TITLE,MySQLiteHelper.COLUMN_PRICE};

public DataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public String createProduct(String strStockRef, String strTitle, String strPrice) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_STOCKREF, strStockRef);
values.put(MySQLiteHelper.COLUMN_TITLE, strTitle);
values.put(MySQLiteHelper.COLUMN_PRICE, strPrice);


long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null,
values);

return "Completed";
}

}


GETCONTEXT



package com.example.jsondb;

import android.content.Context;

public class getContext extends android.app.Application {

private static getContext instance;

public getContext() {
instance = this;
}

public static Context getContextNow() {
return instance;
}

}


Here is my stack trace



05-01 15:21:22.948: E/AndroidRuntime(24214): FATAL EXCEPTION: AsyncTask #2
05-01 15:21:22.948: E/AndroidRuntime(24214): java.lang.RuntimeException: An error occured while executing doInBackground()
05-01 15:21:22.948: E/AndroidRuntime(24214): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-01 15:21:22.948: E/AndroidRuntime(24214): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.lang.Thread.run(Thread.java:856)
05-01 15:21:22.948: E/AndroidRuntime(24214): Caused by: java.lang.NullPointerException
05-01 15:21:22.948: E/AndroidRuntime(24214): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:236)
05-01 15:21:22.948: E/AndroidRuntime(24214): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
05-01 15:21:22.948: E/AndroidRuntime(24214): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-01 15:21:22.948: E/AndroidRuntime(24214): at com.example.jsondb.DataSource.open(DataSource.java:19)
05-01 15:21:22.948: E/AndroidRuntime(24214): at com.example.jsondb.WriteSync.WriteSyncData(WriteSync.java:24)
05-01 15:21:22.948: E/AndroidRuntime(24214): at com.example.jsondb.WriteSync.doInBackground(WriteSync.java:51)
05-01 15:21:22.948: E/AndroidRuntime(24214): at com.example.jsondb.WriteSync.doInBackground(WriteSync.java:1)
05-01 15:21:22.948: E/AndroidRuntime(24214): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-01 15:21:22.948: E/AndroidRuntime(24214): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-01 15:21:22.948: E/AndroidRuntime(24214): ... 4 more


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...