jeudi 3 mars 2016

Why will my database not open for read?

I am working with a WebService that downloads JSON data from a server. The JSON data comes in excellently however when I try to store that information into my local sqlite database it throws a NullPointerException. Below is my code:

@TargetApi(Build.VERSION_CODES.KITKAT)
public static String customers() {
    String resTxt = null;
    String webMethName="downloadCustomerInfo";
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    //Set envelope as dotNet
    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_Main);

    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
        // Get the response
        Object response = envelope.getResponse();
        String result = response.toString();

        JSONArray jarray =new JSONArray(result);

        //StringBuilder stringBuilder_name = new StringBuilder();
        //StringBuilder stringBuilder_account = new StringBuilder();


        MaintenanceAppDB sqlitedb = new MaintenanceAppDB(c);
        sqlitedb.openForWrite();
        try {
            for (int count = 0; count < jarray.length(); count++) {
                JSONObject obj = jarray.getJSONObject(count);
                customerName = obj.getString(TAG_NAME);
                customerAccount = obj.getString(TAG_NUMBER);
                //stringBuilder_name.append(customerName + ",");
                //stringBuilder_account.append(customerAccount + ",");
                ContentValues cv = new ContentValues();
                cv.put(MaintenanceAppDB.CUSTOMER_NAME, customerName);
                cv.put(MaintenanceAppDB.CUSTOMER_ACCOUNT, customerAccount);
                MaintenanceAppDB.maintenanceAppDatabase.insert(MaintenanceAppDB.TABLE_CUSTOMERS, null, cv);
            }
            sqlitedb.close();

            int counter = sqlitedb.getUserDataCount();

            if(counter == jarray.length()){
                resTxt = "Customer Information Downloaded Successfully!";
            }
            else {
                resTxt = "FAILED!!!";
            }
        }catch (JSONException e){
            e.printStackTrace();
        }



        //String names = stringBuilder_name.toString().trim();
        //String accounts = stringBuilder_account.toString().trim();
        //customerName = jarray.getJSONObject(0).getString(TAG_NAME);
        //customerAccount = jarray.getJSONObject(0).getString(TAG_NUMBER);

        // Assign it to resTxt variable static variable
        //resTxt = response.toString();


    } catch (Exception e) {
        //Print error
        e.printStackTrace();
        resTxt = e.toString();
        //Assign error message to resTxt
        //resTxt = "Error occured";
    }
    //Return resTxt to calling object
    return resTxt;
}

The error gets thrown at this line:

sqlitedb.openForWrite();

Any help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire