-4

I have mentioned the line numbers where I am getting run time error line 27,118,62 and 134.Logcat is also there.I am getting these errors after 30-35 seconds.

Why I am getting it on line 27

Line 134 : ListView listView = (ListView) getView().findViewById(R.id.list4);

The warning shows that may causes NPE

  public class Quizze extends android.support.v4.app.Fragment {  //line 27


JSONObject jsonobject;
JSONArray jsonarray;
String idno = " ";



ArrayList<Details> details;
LinearLayout layout;
ArrayList<String> nameList;
TextView textView;

public Quizze() {
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    SharedPreferences pref = getActivity().getPreferences(0);
    pref.getString("url-address", "empty");

    new DownloadJSON().execute();
    return inflater.inflate(R.layout.quiz_result, container, false);





}


class DownloadJSON extends AsyncTask<Void, Void, Void> {  //line 62

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


        SharedPreferences pref = getActivity().getPreferences(0);
        String url = pref.getString("url-address", "empty");

        details = new ArrayList<>();

        jsonobject = JSONfunctions
                .getJSONfromURL(url);

        try {
            // Locate the NodeList name
            jsonarray = jsonobject.getJSONArray("results");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);
                Collections.reverse(details);
                if (jsonobject.getString("resType").contains("Multiple Choice Questions")) {
                    {
                        Details detailAll = new Details();

                        detailAll.setResType(jsonobject.optString("resType"));
                        detailAll.setName(jsonobject.optString("resName"));
                        detailAll.setUrl(jsonobject.optString("resLink"));
                        detailAll.setId(jsonobject.optInt("id"));


                        details.add(detailAll);
                        Collections.reverse(details);
                    }
                }

            }


        } catch (JSONException e) {
            Log.e("Error", "" + e.getMessage());
            e.printStackTrace();
        }
        return null;


}

    @Override
    protected void onPostExecute(Void args) {


        nameList = new ArrayList<>();
        for (Details bean : details) {
            nameList.add(bean.getResType());


            aero(details);  //line 118
        }
    }
}

private void aero(List<Details> mList) {


    for (Details bean : details) {
        final String urlChar = bean.getUrl();

        idno = String.valueOf(bean.getId());

        if (bean.getResType().equals("Multiple Choice Questions"))

        {
            ListView listView = (ListView) getView().findViewById(R.id.list4);       //Line 134

            CustomListViewAdapterMindMaps adapter = new CustomListViewAdapterMindMaps(getContext(),
                    R.layout.quiz_result, mList);
            listView.setAdapter(adapter);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {


                    Toast.makeText(getActivity(),
                            "Click ListItem Number " + idno, Toast.LENGTH_LONG);


                    Intent intent = new Intent(getContext(), Main_Activity_For_Quiz.class);
                    //add data to the Intent object
                    intent.putExtra("idquiz", idno);
                    intent.putExtra("urlchar", urlChar);
                    Toast.makeText(getActivity(),
                            "Click ListItem Number " +idno+ urlChar, Toast.LENGTH_LONG)
                            .show();
                    //start the second activity
                     startActivity(intent);

                }


            });
        }
    }

   }
}




 FATAL EXCEPTION: main
                                                                        Process: com.example.wonder5.player, PID: 21792
                                                                        java.lang.NullPointerException
                                                                            at com.example.wonder5.player.Quizze.aero(Quizze.java:134)
                                                                            at com.example.wonder5.player.Quizze.access$000(Quizze.java:27)
                                                                            at com.example.wonder5.player.Quizze$DownloadJSON.onPostExecute(Quizze.java:118)
                                                                            at com.example.wonder5.player.Quizze$DownloadJSON.onPostExecute(Quizze.java:62)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

IDE warns you that getView() is not NULL safe If you change:

ListView listView = (ListView) getView().findViewById(R.id.list4);

to:

ListView listView;
View view = getView();
if(view != null){
listView = (ListView) view .findViewById(R.id.list4);
}

You wont get it anymore

hrskrs
  • 4,447
  • 5
  • 38
  • 52