0
創建數據庫和表傳遞successfulyl。數據庫處理器類的 我的onCreate():創建方法ORRLite例外集
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)
{
try
{
TableUtils.createTable(connectionSource, Category.class);
TableUtils.createTable(connectionSource, Level.class);
DataParsing a = new DataParsing();
a.fillCategories();
Log.i(DatabaseHandler.class.getName(), "created new entries in onCreate: ");
}
catch (SQLException e){
Log.e(TAG, "error creating DB " + DATABASE_NAME);
throw new RuntimeException(e);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我從網絡服務中獲取數據JSON和與GSON解析它。數據在fillcategories()方法中成功填充類數據。然後,我希望把所有的數據到數據庫ORMLite在saveContacts()方法:
public class DataParsing {
private static String KEY_STATUS = "status";
private static String KEY_MESSAGE = "message";
private static String KEY_CATEGORIES = "categories";
private static String KEY_DATA = "data";
private DatabaseHandler databaseHandler;
private Context _context;
public void fillCategories() throws JSONException, SQLException
{ UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser("[email protected]", "123456");
JSONObject data = json.getJSONObject(KEY_DATA);
// getting categories
JSONArray categories = new JSONArray();
categories = data.getJSONArray(KEY_CATEGORIES);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(categories.toString()).getAsJsonArray();
Type listType = new TypeToken<List<Category>>() {}.getType();
List<Category> tasks = new ArrayList<Category>();
tasks = gson.fromJson(array.toString(), listType);
saveContacts(tasks);
}
public void saveContacts(List<Category> contacts) throws SQLException
{
OrmLiteSqliteOpenHelper dbHelper= DatabaseHandler.getInstance(_context);
Dao<Category, Integer> daoContact=dbHelper.getDao(Category.class);
QueryBuilder<Category, Integer> queryBuilder = daoContact.queryBuilder();
Log.i("dao",queryBuilder.selectColumns("title").prepare().toString());
for (Category contact : contacts) {
Log.i("dao",contact.toString());
HelperFactory.GetHelper().getCategoryDao().create(contact);
}
}
}
,並獲得集上...創建(接觸)行異常:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.library.DataParsing.saveContacts(DataParsing.java:61)
at com.library.DataParsing.fillCategories(DataParsing.java:45)
at com.library.DatabaseHandler.onCreate(DatabaseHandler.java:85)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:169)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:63)
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:51)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:202)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:155)
at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:113)
at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:237)
at com.assignmentexpert.LoginActivity$1.onClick(LoginActivity.java:97)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
爲什麼它發生了嗎?
這是DataParsing.java的第61行嗎? – assylias 2012-08-01 11:45:55
HelperFactory.GetHelper()。getCategoryDao()。create(contact);主要問題在這裏... – 2012-08-01 13:44:54
你應該在這裏接受你自己的答案。 – Gray 2012-09-26 20:36:04