Find out what variable is throwing a NullPointerException programmatically

后端 未结 8 1627
醉酒成梦
醉酒成梦 2020-12-05 10:30

I know I can find out if a variable is null in Java using these techniques:

  • if (var==null) -> too much work
  • try { ... } catch (Null
8条回答
  •  借酒劲吻你
    2020-12-05 10:37

    What has worked really beautifully to me is catching the exception where it normally gets thrown and then using Log to see whether any of them have 'null' values.

    My code:

     try {
                     if (description_visible) advice_title_cur.setText(all_title_array[pos]);
                     else advice_title_cur.setText(all_title_array[pos] + "...");
    
                 } catch (NullPointerException e) {
                     e.printStackTrace();
                     Log.e("My name", "description_visible " + description_visible);
                     Log.e("My name", "advice_title_cur " + advice_title_cur);
                     Log.e("My name", "all_title_array " + all_title_array);
                     Log.e("My name", "pos " + pos);
                 }
    

提交回复
热议问题