Hello, I'm making a Guild Wars 2 app. In the code below I get the different WvWvW matches. The api returns them but when I call "DatabaseHelper.loadWorldNames(matches)". It gives me a nullpointer exeption. So I've been debugging and I saw that in the DatabaseHelper the program doesn't go to Application.getDatabaseHelper().getWorldDao(). The code is provided below.
Backgroundloader:
public class MatchLoader extends BackgroundTaskLoader<List<Match>>{
public MatchLoader(Context context)
{
super(context);
}
@[member='OverRide']
public List<Match> loadInBackground() {
try {
HttpsURLConnection connection =
(HttpsURLConnection) new URL("https://api.guildwars2.com/v1/wvw/matches.json").openConnection();
List<Match> matches = new Gson().fromJson(new InputStreamReader(connection.getInputStream()),
Response.class).wvw_matches;
DatabaseHelper.loadWorldNames(matches);
Collections.sort(matches, new Comparator<Match>() {
@[member='OverRide']
public int compare(Match match, Match match2) {
return match.wvw_match_id.compareTo(match2.wvw_match_id);
}
});
return matches;
} catch (Exception e) {
Log.e(Application.Constants.LOG_TAG, "Error loading matches", e);
return null;
}
}
private class Response {
public List<Match> wvw_matches;
}
}
DatabaseHelper:
public static void loadWorldNames(List<Match> matches) throws Exception {
if (Application.getDatabaseHelper().getWorldDao().queryForAll().size() == 0) {
HttpsURLConnection connection =
(HttpsURLConnection) new URL("https://api.guildwars2.com/v1/world_names.json").openConnection();
final List<world> worlds =
new Gson().fromJson(new InputStreamReader(connection.getInputStream()),
new TypeToken<List<world>>() {
}.getType());
Application.getDatabaseHelper().getEventNameDao().callBatchTasks(new Callable<Object>() {
@[member='OverRide']
public Object call() throws Exception {
for (world world : worlds) {
Application.getDatabaseHelper().getWorldDao().create(world);
}
return null;
}
});
}
for (Match match : matches) {
match.redWorld = Application.getDatabaseHelper().getWorldDao().queryForId(match.red_world_id);
match.greenWorld = Application.getDatabaseHelper().getWorldDao().queryForId(match.green_world_id);
match.blueWorld = Application.getDatabaseHelper().getWorldDao().queryForId(match.blue_world_id);
}
}
public Dao<world, Integer> getWorldDao() throws Exception {
if (worldDao == null) {
worldDao = DaoManager.createDao(getConnectionSource(), world.class);
}
return worldDao;
}
Anyone who can help?
sorry if I made any spelling errors, english isn't my native language.
Backgroundloader:
public class MatchLoader extends BackgroundTaskLoader<List<Match>>{
public MatchLoader(Context context)
{
super(context);
}
@[member='OverRide']
public List<Match> loadInBackground() {
try {
HttpsURLConnection connection =
(HttpsURLConnection) new URL("https://api.guildwars2.com/v1/wvw/matches.json").openConnection();
List<Match> matches = new Gson().fromJson(new InputStreamReader(connection.getInputStream()),
Response.class).wvw_matches;
DatabaseHelper.loadWorldNames(matches);
Collections.sort(matches, new Comparator<Match>() {
@[member='OverRide']
public int compare(Match match, Match match2) {
return match.wvw_match_id.compareTo(match2.wvw_match_id);
}
});
return matches;
} catch (Exception e) {
Log.e(Application.Constants.LOG_TAG, "Error loading matches", e);
return null;
}
}
private class Response {
public List<Match> wvw_matches;
}
}
DatabaseHelper:
public static void loadWorldNames(List<Match> matches) throws Exception {
if (Application.getDatabaseHelper().getWorldDao().queryForAll().size() == 0) {
HttpsURLConnection connection =
(HttpsURLConnection) new URL("https://api.guildwars2.com/v1/world_names.json").openConnection();
final List<world> worlds =
new Gson().fromJson(new InputStreamReader(connection.getInputStream()),
new TypeToken<List<world>>() {
}.getType());
Application.getDatabaseHelper().getEventNameDao().callBatchTasks(new Callable<Object>() {
@[member='OverRide']
public Object call() throws Exception {
for (world world : worlds) {
Application.getDatabaseHelper().getWorldDao().create(world);
}
return null;
}
});
}
for (Match match : matches) {
match.redWorld = Application.getDatabaseHelper().getWorldDao().queryForId(match.red_world_id);
match.greenWorld = Application.getDatabaseHelper().getWorldDao().queryForId(match.green_world_id);
match.blueWorld = Application.getDatabaseHelper().getWorldDao().queryForId(match.blue_world_id);
}
}
public Dao<world, Integer> getWorldDao() throws Exception {
if (worldDao == null) {
worldDao = DaoManager.createDao(getConnectionSource(), world.class);
}
return worldDao;
}
Anyone who can help?
sorry if I made any spelling errors, english isn't my native language.