jeudi 11 juin 2015

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

here is the JSONparser:

public ArrayList<HistoryPayslipsInquiry> parseHistoryPayslipsInquiry(JSONObject object)

 {
        ArrayList<HistoryPayslipsInquiry> arrayList=newArrayList<HistoryPayslipsInquiry>();

try {

  JSONArray jsonArray=object.getJSONArray("Value");

JSONObject jsonObj;

  for(int i=0;i<jsonArray.length();i++)
            {
                jsonObj=jsonArray.getJSONObject(i);
                arrayList.add(new HistoryPayslipsInquiry(jsonObj.getInt("History_Payslip_Month"), jsonObj.getInt("History_Payslip_Year")
                                                        ,jsonObj.getInt("Emp_Code"),jsonObj.getString("Emp_Name"),jsonObj.getString("Allowance_Deduction_Flag"),
                        jsonObj.getInt("Allowance_Code"),jsonObj.getString("Allowance_Name"),jsonObj.getInt("Amount")));

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.d("JSONParser => parseHistoryPayslipsInquiry", e.getMessage());
        }
        return arrayList;
    }

DBHandler:

query = "CREATE TABLE " + TABLE_HistoryPayslips + "(" + COLUMN_HistoryPayslips_YEAR + " INTEGER, " + COLUMN_HistoryPayslips_MONTH  +
                " INTEGER, " + COLUMN_USER_Login + " TEXT, " + COLUMN_EMP_NAME + " TEXT, " + COLUMN_ALLOWANCE_DEDUCTION_FLAG + " TEXT, " +
                COLUMN_ALLOWANCE_CODE + " INT, " + COLUMN_ALLOWANCE_NAME + " TEXT, " +COLUMN_AMOUNT + " NUMERIC " + ");";
        db.execSQL(query);

        Log.i("Cool","History Payslips TAble Created");

 public void addHistoryPayslipInquiry(HistoryPayslipsInquiry HistoryPayslipsInquiryObj) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_HistoryPayslips_YEAR,HistoryPayslipsInquiryObj.getHistoryPayslips_YEAR());
        values.put(COLUMN_HistoryPayslips_MONTH, HistoryPayslipsInquiryObj.getHistoryPayslips_MONTH());
        values.put(COLUMN_USER_Login, Integer.toString(HistoryPayslipsInquiryObj.getEMP_CODE()));
        values.put(COLUMN_EMP_NAME, HistoryPayslipsInquiryObj.getEMP_NAME());
        values.put(COLUMN_ALLOWANCE_DEDUCTION_FLAG, HistoryPayslipsInquiryObj.getALLOWANCE_DEDUCTION_FLAG());
        values.put(COLUMN_ALLOWANCE_CODE, HistoryPayslipsInquiryObj.getALLOWANCE_CODE());
        values.put(COLUMN_ALLOWANCE_NAME, HistoryPayslipsInquiryObj.getALLOWANCE_NAME());
        values.put(COLUMN_AMOUNT, HistoryPayslipsInquiryObj.getAMOUNT());

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_HistoryPayslips, null, values);
        db.close();
    }

and login activity whre it is invoked:

protected class AsyncHistoryPayslipsInquiry extends
            AsyncTask<String, JSONObject, ArrayList<HistoryPayslipsInquiry>> {
        ArrayList<HistoryPayslipsInquiry> HistoryPayslipsInquiry = null;

        @Override
        protected ArrayList<HistoryPayslipsInquiry> doInBackground(String... params) {
            // TODO Auto-generated method stub

            RestAPI api = new RestAPI();
            try {

                JSONObject jsonObj = api.HistoryPayslipsInquiry(params[0]);

                JSONParser parser = new JSONParser();

                HistoryPayslipsInquiry = parser.parseHistoryPayslipsInquiry(jsonObj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncHistoryPayslipsInquiry", e.getMessage());

            }

            return HistoryPayslipsInquiry;
        }

        @Override
        protected void onPostExecute(ArrayList<HistoryPayslipsInquiry> result) {
            // TODO Auto-generated method stub
            //LeaveType  objLeaveType new LeaveType(0,"a");
                     for (int i = 0; i < result.size(); i++) {
                // data.add(result.get(i).getNo() + " " + result.get(i).getName());
                db.addHistoryPayslipInquiry(result.get(i));

                // data.add(result.get(i).getNo() + " " + result.get(i).getName());
            }

            // adapter.notifyDataSetChanged();

            // Toast.makeText(getApplicationContext(),"Loading Completed",Toast.LENGTH_SHORT).show();
        }

    }

new AsyncHistoryPayslipsInquiry().execute();

My table gets created however I am unable to populate data into it. I ran through many of the solutions but i coulnt understand them. also i am a newbie in Android. please anyone help

Aucun commentaire:

Enregistrer un commentaire