データベースとテーブルの作成は成功しました。私の onCreate() の DatabaseHandler クラス:
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();
}
}
Web サービスからデータ JSON を取得し、GSON で解析します。データは、fillcategories() メソッドでクラス データを正常に埋めます。次に、saveContacts() メソッドですべてのデータを ORMLite データベースに入れたいと思います。
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("shurko@ukr.net", "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);
}
}
}
...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)
なぜそれが起こるのですか?