Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio nullpointer warnings/notice in code view

until now I couldn't find any answer that suits my problem/question. In android studio it often shows a notice that e.g. findViewById may produce a NPE, althogh i know the item IS existing in the layout I use. I also never got a NPE thrown at this locations at runtime, so why the warning/notice?

I just don't know how I have to react/handle to such warnings/infos in the code view? Are there any tips to avoid that messages (because for me it looks like valid and working code)? It's only the warnings in the code view, that makes me uncertain.

Here a snippet where a warning is shown on findViewByID.

//fetch predefined item layout
row = inflater.inflate(R.layout.view_searchresult_item,null);
//get table layout for inserting items
TableLayout itemtablelayout = (TableLayout)row.findViewById(R.id.resultitem_tablelayout);
like image 819
Faenor Avatar asked Sep 03 '13 09:09

Faenor


Video Answer


1 Answers

Warning is not an Error. And the warning which you are talking about says "it may produce", don't say 'it must produce'. So choice is yours. Either add null check or not


So, If you are sure that findViewById in your code will never be cause of NPE, then don't add the null check.

like image 173
Pankaj Kumar Avatar answered Sep 19 '22 04:09

Pankaj Kumar