I have an Android application I’m working on, I’ve got the code working I’m just trying to clean it up a bit by moving certain functions into separate files. Whenever i try to call one of the functions from a separate file, however, it crashes saying FATAL EXCEPTION: AsyncTask #5. I have made a constructor in the separate file and I thought I was doing it correctly but I guess not. Any help is appreciated.
Here is my code:
DatabaseManager database;
public class loginTask extends AsyncTask<String, String, String> {
//check if server is online
protected String doInBackground(String... params) {
Log.d("test", "1");
String username = params[0];
String password = params[1];
URI absolute = null;
Log.d("test", "2");
try {
Log.d("test", "3");
absolute = new URI("http://link.com/webservice/");
} catch (URISyntaxException e) {
Log.d("test", "4");
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("test", "5");
database.test();//crashes at this line
return postHttpResponse(absolute,username,password);
}
//set status bar to offline if flag is false
protected void onPostExecute(String jsonstring) {
try {
//get values from jsonobject
JSONObject jsonobj=new JSONObject(jsonstring);
checkResponse(jsonobj);
//errorMessage.setText(checkResponse(jsonobj));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DatabaseManager.java
private final String LOGIN_TAG = "Login";
public DatabaseManager(){
}
public void test(){
Log.d(LOGIN_TAG, "this is a test");
}
You never instantiate
DatabaseManager– so it staysnullthe entire time. Class scoped references staynulluntil they are given something to reference (either by pointing the new reference to an existing one or by making a new Object)Consider doing