I am using sqlite select in asynctask, and the catch error is "NullPointerException". But I cannot figure out why. This is my second table and I completely followed the format of my first table. The first table works, but this one doesn't. What if there are more than one result meet the select condition? How can I make sure each return value could connect to the following xml code? Asynctask class code:
protected class tryconnect extends AsyncTask<Void,Integer,Void>{
EditText city=(EditText)findViewById(R.id.city_address);
String mycity=city.getText().toString();
EditText street=(EditText)findViewById(R.id.street_address);
String mystreet=street.getText().toString();
EditText postcode=(EditText)findViewById(R.id.postcode_address);
String mypostcode=postcode.getText().toString();
EditText number=(EditText)findViewById(R.id.number_address);
String mynumber=number.getText().toString();
String ReservedState="Free";
@Override
protected Void doInBackground(Void... arg0) {
try{
String result=dbHelper1.select1(ReservedState, mycity, mystreet, mypostcode);
}catch(Exception e){
e.printStackTrace();
}
// TODO Auto-generated method stub
String baseURL="https://fontys.nedapparking.com/so.xml?Ln=adminfontys&Pw=f0nty589&Cm=1&Fm=A&PrkLt=1&Bay=";
//c is the retrieving result from reservation table
String a="1";
StringBuilder URL=new StringBuilder(baseURL);
URL.append(a);
String fullURL=URL.toString();
try{
URL website=new URL(fullURL);
//getting xmlreader to parse data
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
HandlingXMLStuff doingWork=new HandlingXMLStuff();
xr.setContentHandler(doingWork);
try{
xr.parse(new InputSource(website.openStream()));
}catch(Exception e){
e.printStackTrace();
}
information=doingWork.getInformation();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Message.message(FragmentHandler.this,information);
}
}
select code in another class:`public String select1 (String ReservedState, String mycity, String mystreet, String mypostcode){
ReservedState="Free";
SQLiteDatabase Database1=dbHelper1.getWritableDatabase();
String[] columns={DbHelper1.KEY_ID1,DbHelper1.KEY_RESERVEDSTATE, DbHelper1.KEY_GEOGRAPHICALLOCATION,DbHelper1.KEY_CITY,DbHelper1.KEY_POSTCODE,DbHelper1.KEY_STREET};
Cursor cursor=Database1.query(DbHelper1.TABLE_NAME1,columns, DbHelper1.KEY_RESERVEDSTATE+"='"+ReservedState+"' AND "+DbHelper1.KEY_CITY+"='"+mycity+"' AND "+DbHelper1.KEY_POSTCODE+"='"+mypostcode+"' AND "+DbHelper1.KEY_STREET+"='"+mystreet+"'",null,null,null,null);
StringBuffer buffer=new StringBuffer();
while(cursor.moveToNext()){
int index1=cursor.getColumnIndex(DbHelper1.KEY_ID1);
int index2=cursor.getColumnIndex(DbHelper1.KEY_RESERVEDSTATE);
int index3=cursor.getColumnIndex(DbHelper1.KEY_GEOGRAPHICALLOCATION);
int index4=cursor.getColumnIndex(DbHelper1.KEY_CITY);
int index5=cursor.getColumnIndex(DbHelper1.KEY_POSTCODE);
int index6=cursor.getColumnIndex(DbHelper1.KEY_STREET);
String id=cursor.getString(index1);
buffer.append(id);
}
return buffer.toString();
}`
And my sqliteassethelper class is as following:
static class DbHelper1 extends SQLiteAssetHelper{
public static final String KEY_ID1="ID";
public static final String KEY_RESERVEDSTATE="ReservedState";
public static final String KEY_GEOGRAPHICALLOCATION="GeographicalLocation";
public static final String KEY_CITY="City";
public static final String KEY_POSTCODE="Postcode";
public static final String KEY_STREET="Street";
private static String DATABASE_NAME="clientdatabase.db";
private static final String TABLE_NAME1="ReservedStatus";
private static final int DATABASE_VERSION1=1;
public static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME1;
private Context context;
private String db_path;
public DbHelper1(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION1);
//this.DATABASE_NAME=DATABASE_NAME;
//this.context=context;
//db_path="/data/data/"+context.getPackageName()+"/databases/";
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//delete table
db.execSQL(DROP_TABLE);
onCreate(db);
}