vendredi 11 mars 2016

Newly inserted item in sqlite throw null pointer exception?

Am having one doubt am fetching the records from server and saving it and displaying it with recyclerview when i fetch it and displaying it from sqlite it throws null pointer excpetion for first time don't know where am doing mistake:

RequestQueue queue = Volley.newRequestQueue(getBaseContext());

JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, params[0], new JSONObject(),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                String server_response = response.toString();
                try {
                    JSONObject json_object = new JSONObject(server_response);
                    JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));
                    for (int i = 0; i < json_array.length(); i++) {
                        Model_Account modelobjs = new Model_Account();
                        JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
                        modelobjs.setCompany_group(json_arrayJSONObject.getString("CompanyName"));
                        modelobjs.setState(json_arrayJSONObject.getString("Region"));
                        accountListAdapter.addModelClass(modelobjs);
                        account_sf_db.InsertorUpdate(modelobjs);


                    }
                }  catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();

            }
        });
queue.add(jsonObjRequest);

And this where am displaying listitem :

List<Model>lisobj=new Arraylist(
 listobj = account_sf_db.list();
            accountListAdapter = new AccountListAdapter(listobj, getApplicationContext());
            recyclerView.setAdapter(accountListAdapter);

as soon as i click recyclerview item it throws null pointer exception:

accountListAdapter.setOnItemClickListener(new AccountListAdapter.AccountList() {
                    @Override
                    public void Clickable(View v, int position) {

                        Bundle bundle = new Bundle();
                        Model_Account modelobj = (Model_Account) listobj.get(position);
                        int id = modelobj.getID();
                        bundle.putInt("ID", id);
                        Intent intent = new Intent(getApplicationContext(), DetailAccount.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });

Here is my Log:

 FATAL EXCEPTION: main
                                                                        Process: precision.servicefirst, PID: 31312
                                                                        java.lang.NullPointerException
                                                                            at precision.servicefirst.Accounts$1$3.Clickable(Accounts.java:111)
                                                                            at precision.servicefirst.AccountListAdapter$MyViewHolder$1.onClick(AccountListAdapter.java:48)
                                                                            at android.view.View.performClick(View.java:4444)
                                                                            at android.view.View$PerformClick.run(View.java:18457)
                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                            at dalvik.system.NativeStart.main(Native Method)

This is the method i call before inserting:

  public void addModelClass(Model_Account modelClass){
        dataSet.add(modelClass);
        notifyItemInserted(dataSet.size()-1);
    }

Aucun commentaire:

Enregistrer un commentaire