Throwing null pointer exception

前端 未结 6 1646
别那么骄傲
别那么骄傲 2021-01-28 00:29

I am doing one application in android for that I need to access com.android.internal.telephony APIs. Now I am able to access those APIs but problem is wherever I call the

6条回答
  •  灰色年华
    2021-01-28 01:07

    Obviously getConnections() returns null here and you cannot get the size of the null-object.

    Here's how to fix it:

    if (l == null || l.size() == 0) 
    {
        return null;
    }
    

    So, if for some unknown reason, there is no Connection-List or the list is empty, null will be returned. Using the operator || will do the same as

    if (l == null)
    {
        return null;
    }
    else if ( l.size() == 0)
    {
        return null;
    }
    

提交回复
热议问题