-1

I am experiencing a frustrating issue where I'm getting a null pointer exception when setting my custom listview adapter to my listview. I cannot figure out what would be null. I did something similar in another part of the app, and didn't have any issues. I'm not sure if this issue is related to using images this time?

Below is my code:

Fragment class where I'm getting data and initiating listview, (I've tested successfully that I'm getting my data)

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;


public class FinalUnitStatusFragment extends Fragment {

Context finalUnitStatusContext;
Activity finalUnitStatusActivity;

ProgressDialog progressDialog;

//FinalUnitStatusListViewAdapter finalUnitStatusLVAdapter;

String ReadOnly;
String LexaUser;
String Password;
String SearchValue;

String finalResultFinalUS;
String HttpURLFinalUS = "https://[my/path/to/page]/getFinalUnitStatus.php";
HashMap<String, String> hashMapFinalUS = new HashMap<>();
HttpParse httpParseFinalUS = new HttpParse();

String[] Uid;
String[] Ustatus;
String[] Uqastandard;
String[] Ufinaloutcome;
String[] Ugencause;
String[] Uspeccause;
String[] Usurface;
String[] Uorigin;
String[] Ulexauser;
String[] Ulexausercomment;
String[] Urejphoto1String;
Bitmap[] Urejphoto1;
String[] Ulexausertimestamp;
String[] Uqauser;
String[] Uqacomment;
String[] Urejphoto2String;
Bitmap[] Urejphoto2;
String[] Uqausertimestamp;

ListView finalUSListView;

public FinalUnitStatusFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_siblings, container, false);

    finalUSListView = (ListView) view.findViewById(R.id.FinalUnitStatusList);

    finalUnitStatusContext = getContext();

    finalUnitStatusActivity = getActivity();

    if (getArguments() != null) {
        SearchValue = getArguments().getString("SearchValue");
        LexaUser = getArguments().getString("LexaUser");
        getFinalUnitStatus(SearchValue, LexaUser);
    } else {
        Toast.makeText(finalUnitStatusContext, "Arguments Null", Toast.LENGTH_LONG).show();
    }

    return view;
}

public void getFinalUnitStatus(String searchInput, String lexaUser) {

    class FinalUnitStatusClass extends AsyncTask<String,Void,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog = ProgressDialog.show(finalUnitStatusContext, "Loading Data", null, true, true);
        }

        @Override
        protected void onPostExecute(String httpResponseMsg) {

            super.onPostExecute(httpResponseMsg);

            if (httpResponseMsg != null) {

                try {

                    JSONArray json = new JSONArray(httpResponseMsg);

                    Uid = new String[json.length()];
                    Ustatus = new String[json.length()];
                    Uqastandard = new String[json.length()];
                    Ufinaloutcome = new String[json.length()];
                    Ugencause = new String[json.length()];
                    Uspeccause = new String[json.length()];
                    Usurface = new String[json.length()];
                    Uorigin = new String[json.length()];
                    Ulexauser = new String[json.length()];
                    Ulexausercomment = new String[json.length()];
                    Urejphoto1String = new String[json.length()];
                    Urejphoto1 = new Bitmap[json.length()];
                    Ulexausertimestamp = new String[json.length()];
                    Uqauser = new String[json.length()];
                    Uqacomment = new String[json.length()];
                    Urejphoto2String = new String[json.length()];
                    Urejphoto2 = new Bitmap[json.length()];
                    Uqausertimestamp = new String[json.length()];

                    for (int i = 0; i < json.length(); i++) {

                        JSONObject object = json.getJSONObject(i);

                        Uid[i] = object.getString("statusID");
                        Ustatus[i] = object.getString("unitStatus");
                        Uqastandard[i] = object.getString("qaStandard");
                        Ufinaloutcome[i] = object.getString("finalOutcome");
                        Ugencause[i] = object.getString("genCause");
                        Uspeccause[i] = object.getString("specCause");
                        Usurface[i] = object.getString("surface");
                        Uorigin[i] = object.getString("origin");
                        Ulexauser[i] = object.getString("lexaUser");
                        Ulexausercomment[i] = object.getString("initComment");
                        Urejphoto1String[i] = object.getString("rejImage1");
                        Ulexausertimestamp[i] = object.getString("addDate");
                        Uqauser[i] = object.getString("qaUser");
                        Uqacomment[i] = object.getString("qaComment");
                        Urejphoto2String[i] = object.getString("rejImage2");
                        Uqausertimestamp[i] = object.getString("qaAddDate");

                        if (Urejphoto1String.equals("Could not find image.")) {
                            String placeholder = "";
                        } else {
                            byte[] decodedString = Base64.decode(Urejphoto1String[i], Base64.DEFAULT);
                            Urejphoto1[i] = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                        }
                        if (Urejphoto2String.equals("Could not find image.")) {
                            String placeholder = "";
                        } else {
                            byte[] decodedString2 = Base64.decode(Urejphoto2String[i], Base64.DEFAULT);
                            Urejphoto2[i] = BitmapFactory.decodeByteArray(decodedString2, 0, decodedString2.length);
                        }
                    }

                    FinalUnitStatusListViewAdapter finalUSLVAdapter = new FinalUnitStatusListViewAdapter(finalUnitStatusActivity, Uid, Ustatus, Uqastandard, Ufinaloutcome, Ugencause, Uspeccause, Usurface, Uorigin, Ulexauser, Ulexausercomment, Urejphoto1, Ulexausertimestamp, Uqauser, Uqacomment, Urejphoto2, Uqausertimestamp);
                    if (finalUSLVAdapter != null) {
                        finalUSListView.setAdapter(finalUSLVAdapter);
                    } else {
                        Toast.makeText(finalUnitStatusContext, "Error: ListView Adapter is null", Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    Log.e("JSONException", "Error: " + e.toString());
                    Toast.makeText(finalUnitStatusContext, "Error: " + e.toString(), Toast.LENGTH_LONG).show();
                } // catch (JSONException e)

                progressDialog.dismiss();

            } else {
                progressDialog.dismiss();
                Toast.makeText(finalUnitStatusContext, "HttpResponseMsg is null.", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected String doInBackground(String... params) {

            hashMapFinalUS.put("searchinput", params[0]);
            hashMapFinalUS.put("lexauser", params[1]);

            finalResultFinalUS = httpParseFinalUS.postRequest(hashMapFinalUS, HttpURLFinalUS);

            return finalResultFinalUS;
        }
    }
    FinalUnitStatusClass finalUnitStatusClass = new FinalUnitStatusClass();
    finalUnitStatusClass.execute(searchInput, lexaUser);
}

}

My custom adapter:

package com.ltiws.www.lexaapp;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FinalUnitStatusListViewAdapter extends BaseAdapter {

    Activity context;
    String Uid[];
    String Ustatus[];
    String Uqastandard[];
    String Uresolution[];
    String Ugencause[];
    String UspecCause[];
    String Usurface[];
    String Uorigin[];
    String Ulexauser[];
    String Uinitcomment[];
    Bitmap Urejphoto1[];
    String Ulexausertimestamp[];
    String Uqauser[];
    String Uqacomment[];
    Bitmap Urejphoto2[];
    String Uqausertimestamp[];

 public FinalUnitStatusListViewAdapter(Activity context, String[] Uid, String[] Ustatus, String[] Uqastandard, String[] Uresolution, String[] Ugencause, String[] UspecCause, String[] Usurface, String[] Uorigin, String[] Ulexauser, String[] Uinitcomment, Bitmap[] Urejphoto1, String[] Ulexausertimestamp, String[] Uqauser, String[] Uqacomment, Bitmap[] Urejphoto2, String[] Uqausertimestamp) {
    super();
    this.context = context;
    this.Uid = Uid;
    this.Ustatus = Ustatus;
    this.Uqastandard = Uqastandard;
    this.Uresolution = Uresolution;
    this.Ugencause = Ugencause;
    this.UspecCause = UspecCause;
    this.Usurface = Usurface;
    this.Uorigin = Uorigin;
    this.Ulexauser = Ulexauser;
    this.Uinitcomment = Uinitcomment;
    this.Urejphoto1 = Urejphoto1;
    this.Ulexausertimestamp = Ulexausertimestamp;
    this.Uqauser = Uqauser;
    this.Uqacomment = Uqacomment;
    this.Urejphoto2 = Urejphoto2;
    this.Uqausertimestamp = Uqausertimestamp;
    }

public int getCount() {
    // TODO Auto-generated method stub
    return Uid.length;
    }

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
    }

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    }

private class ViewHolder {
  TextView txtViewUid;
  TextView txtViewStatus;
  TextView txtViewQAStandard;
  TextView txtViewResolution;
  TextView txtViewGenCause;
  TextView txtViewSpecCause;
  TextView txtViewSurface;
  TextView txtViewOrigin;
  TextView txtViewLexaUser;
  TextView txtViewInitComment;
  ImageView imageViewRejPhoto1;
  TextView txtViewLexaUserTimestamp;
  TextView txtViewQAUser;
  TextView txtViewQAComment;
  ImageView imageViewRejPhoto2;
  TextView txtViewQAUserTimestamp;

}

public View getView(int position, View convertView2, ViewGroup parent)
{
    // TODO Auto-generated method stub
    FinalUnitStatusListViewAdapter.ViewHolder holder2;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView2 == null)
    {
        convertView2 = inflater.inflate(R.layout.finalunitstatus_list, null);
        holder2 = new FinalUnitStatusListViewAdapter.ViewHolder();
        holder2.txtViewUid = (TextView) convertView2.findViewById(R.id.FinalStatusID);
        holder2.txtViewStatus = (TextView) convertView2.findViewById(R.id.FinalStatus);
        holder2.txtViewQAStandard = (TextView) convertView2.findViewById(R.id.FinalQAStandard);
        holder2.txtViewResolution = (TextView) convertView2.findViewById(R.id.FinalResolution);
        holder2.txtViewGenCause = (TextView) convertView2.findViewById(R.id.FinalGenCause);
        holder2.txtViewSpecCause = (TextView) convertView2.findViewById(R.id.FinalSpecCause);
        holder2.txtViewSurface = (TextView) convertView2.findViewById(R.id.FinalSurface);
        holder2.txtViewOrigin = (TextView) convertView2.findViewById(R.id.FinalOrigin);
        holder2.txtViewLexaUser = (TextView) convertView2.findViewById(R.id.FinalLexaUser);
        holder2.txtViewInitComment = (TextView) convertView2.findViewById(R.id.FinalLexaUserComment);
        holder2.imageViewRejPhoto1 = (ImageView) convertView2.findViewById(R.id.FinalLexaUserPhoto);
        holder2.txtViewLexaUserTimestamp = (TextView) convertView2.findViewById(R.id.FinalLexaUserTimestamp);
        holder2.txtViewQAUser = (TextView) convertView2.findViewById(R.id.FinalQAUser);
        holder2.txtViewQAComment = (TextView) convertView2.findViewById(R.id.FinalQAUserComment);
        holder2.imageViewRejPhoto2 = (ImageView) convertView2.findViewById(R.id.FinalQAUserPhoto);
        holder2.txtViewQAUserTimestamp = (TextView) convertView2.findViewById(R.id.FinalQAUserTimestamp);
        convertView2.setTag(holder2);
    }
    else
    {
        holder2 = (FinalUnitStatusListViewAdapter.ViewHolder) convertView2.getTag();
    }

    holder2.txtViewUid.setText(Uid[position]);
    holder2.txtViewStatus.setText(Ustatus[position]);
    holder2.txtViewQAStandard.setText(Uqastandard[position]);
    holder2.txtViewResolution.setText(Uresolution[position]);
    holder2.txtViewGenCause.setText(Ugencause[position]);
    holder2.txtViewSpecCause.setText(UspecCause[position]);
    holder2.txtViewSurface.setText(Usurface[position]);
    holder2.txtViewOrigin.setText(Uorigin[position]);
    holder2.txtViewLexaUser.setText(Ulexauser[position]);
    holder2.txtViewInitComment.setText(Uinitcomment[position]);
    holder2.imageViewRejPhoto1.setImageBitmap(Urejphoto1[position]);
    holder2.txtViewLexaUserTimestamp.setText(Ulexausertimestamp[position]);
    holder2.txtViewQAUser.setText(Uqauser[position]);
    holder2.txtViewQAComment.setText(Uqacomment[position]);
    holder2.imageViewRejPhoto2.setImageBitmap(Urejphoto2[position]);
    holder2.txtViewQAUserTimestamp.setText(Uqausertimestamp[position]);

    return convertView2;
  }
}

My Stack Trace:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: [package], PID: 25971
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                  at com.ltiws.www.lexaapp.FinalUnitStatusFragment$1FinalUnitStatusClass.onPostExecute(FinalUnitStatusFragment.java:168)
                  at com.ltiws.www.lexaapp.FinalUnitStatusFragment$1FinalUnitStatusClass.onPostExecute(FinalUnitStatusFragment.java:92)
                  at android.os.AsyncTask.finish(AsyncTask.java:660)
                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:159)
                  at android.app.ActivityThread.main(ActivityThread.java:6097)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

line 168 of my code causing the break:

finalUSListView.setAdapter(finalUSLVAdapter);

I just can't seem to figure out what would be null in my adapter...

All help is appreciated! Thank you!

rdimouro
  • 225
  • 1
  • 4
  • 17
  • is 'finalUSListView' variable somehow getting set back to null? maybe put a check before to see if that ListView is nulled out? – dave Jun 14 '18 at 19:47
  • could you post your layout.xml as well? – Ruan_Lopes Jun 14 '18 at 19:49
  • 1
    `fragment_siblings` does not have `FinalUnitStatusList` – Vladyslav Matviienko Jun 14 '18 at 20:07
  • @VladyslavMatviienko Yes I just realized this silly mistake! I just correctly and all works perfectly now! – rdimouro Jun 14 '18 at 20:09
  • I assume finalUSListView is null, not the adapter. Maybe you want to check if your layout fragment_siblings contains a ListView called FinalUnitStatusList – j3App Jun 14 '18 at 20:10
  • Not related, but you could gain a lot of clarity by having a data class for your items, eg: `Entry` containing all your strings. Instead of having 20 arrays for each value, you could have a single array or list of `Entry` objects. – Nicolas Jun 14 '18 at 20:57

2 Answers2

3

The problem seems to be with your listview finalUSListView and not with the adapter. Are you sure you're using the correct id to inflate this view? Can you check if it's been instantiated correctly?

ViniciusMS
  • 56
  • 6
1

Vinicius Sant' Ana is right, your listView is null because you use uppercase id to bind id

(ListView) view.findViewById(R.id.FinalUnitStatusList);

Rename the reference in your xml and your code to final_unit_status_list instead

Check this if you want more information :

Why is it not possible to use uppercase in naming resources in android?

For the next time a good thing to do is to launch your app in debug mode and add a breakpoint on the error line you will see quickly that your listview is null

Vodet
  • 1,491
  • 1
  • 18
  • 36