0

There are many Questions related to my problem but none of them actually solved my problem. I tried almost all of them.

I'm trying to change values in layout after getting data from network source, but i'm getting nullpointerexception everytime.

Error I'm getting

03-25 18:05:43.071: E/AndroidRuntime(4832): FATAL EXCEPTION: main
    03-25 18:05:43.071: E/AndroidRuntime(4832): java.lang.NullPointerException
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at com.wishberg.app.CommunityPageActivity$SendRequest.onPostExecute(CommunityPageActivity.java:158)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at com.wishberg.app.CommunityPageActivity$SendRequest.onPostExecute(CommunityPageActivity.java:1)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at android.os.AsyncTask.finish(AsyncTask.java:631)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at android.os.Looper.loop(Looper.java:137)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at java.lang.reflect.Method.invoke(Method.java:525)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    03-25 18:05:43.071: E/AndroidRuntime(4832):     at dalvik.system.NativeStart.main(Native Method)

Here is my activity class

public class CommunityPageActivity extends Activity {

    public String wishid = null;
    private Context context = null;
    public String TAG = "communitypage";

    private String cachedName = "community";
    private static final int URL_LOADER = 0;
    private Menu optionsMenu;
    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Intent i = getIntent();
        wishid = i.getStringExtra("id");
        context = getApplicationContext();

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);

        Common.setRefreshActionButtonState(true, optionsMenu);

            progress = new ProgressDialog(this);
    progress.setMessage("Loading...");
    progress.setIndeterminate(true);
    progress.show();

        setContentView(R.layout.community);
    }


    private class SendRequest extends AsyncTask<String, Integer, String> {

        // http://www.mysamplecode.com/2012/11/android-progress-bar-example.html

        ViewHolder holder = null;
        protected String doInBackground(String... requestURL) {

            String data = "";
            HttpURLConnection httpUrlConnection = null;

            try {

                data = Common.readStream(in);

            } catch (MalformedURLException exception) {
                Log.e(TAG, "MalformedURLException");
            } catch (IOException exception) {
                Log.e(TAG, "IOException");
            } finally {
                if (null != httpUrlConnection)
                    httpUrlConnection.disconnect();
            }
            return data;

        }

        protected void onPostExecute(String jsonString) {

            progress.dismiss();
            System.out.println(jsonString);
            JSONArray jsonArray = Common.getArrayFromJsonString(jsonString, 0);


            Inflater inflater;
//          View v = inflater.inflate(R.layout.community);
            holder.txtWishName = (TextView) CommunityPageActivity.this.findViewById(R.id.tv_communitywishname);
            System.out.println(holder);
            System.out.println(jsonArray);
        }


        /* private view holder class */
        private class ViewHolder {
            ImageView imageView;
            TextView txtWishName;
            ImageButton btn_touchwood;
            ImageButton btn_addwish;
            TextView tvGesture;
            TextView tvNotes;
        }

    }

}
Mukesh Yadav
  • 2,256
  • 2
  • 33
  • 51

2 Answers2

2

remove this line

 progress.dismiss();

you have not initialized and not used anywhere.. Based on comments

Your

ViewHolder holder is null
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
1

First of all, do not initialize your widgets inside an AsyncTask. Do not use inflater in there. You should initialize everything in onCreate(..) and alike.

Then you can perform expensive operations (HTTP connection and so on) in your AsyncTask. Then in onPostExecute you should update the UI widgets, which are initialized in your Activity/Fragment. At least that's how AsyncTask was meant to operate.

And as it was already mentioned, your holder variable is not initialized, that's why you get your Exception in the first place.

Upd: Moreover, parsing your JSON data in onPostExecute() is a bad idea, as it could negate the effect you would expect from usage of AsyncTask - this method is run on the UI-thread and could freeze it. Move the stuff to doInBackground().

Drew
  • 3,307
  • 22
  • 33