NPE的原因是啥?

Posted

技术标签:

【中文标题】NPE的原因是啥?【英文标题】:What the cause of NPE here?NPE的原因是什么? 【发布时间】:2016-08-28 03:36:46 【问题描述】:

我已经提到了运行时错误行 27,118,62 和 134 的行号。Logcat 也在那里。我在 30-35 秒后收到这些错误。

为什么我在第 27 行得到它

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

警告显示可能导致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)

【问题讨论】:

【参考方案1】:

IDE 警告你 getView() 不是 NULL safe 如果你改变:

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

到:

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

你不会得到它了

【讨论】:

感谢警告消失了@hrskrs,让我检查一下它现在是否正常

以上是关于NPE的原因是啥?的主要内容,如果未能解决你的问题,请参考以下文章

为啥完成Activity会抛出NPE?

案例复现,带你分析Priority Blocking Queue比较器异常导致的NPE问题

为什么阿里巴巴开发手册中强制要求 POJO 类使用包装类型?NPE问题防范

Compiler.loadFile中的随机NullPointerException没有原因

高压缩视频中出现的块的原因/用途/原因是啥?

这个错误的原因是啥