Throwing null pointer exception

前端 未结 6 1557
别那么骄傲
别那么骄傲 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 00:53

    Presumably getConnections() is returning null, so l is null, so trying to call l.size() is throwing the exception. We don't know what getConnections() does though.

    Note that this looks suspicious too:

    Connection myConn = new MyConn();
    myConn = myCall.getEarliestConnection();
    

    Why are you creating an instance of MyConn in the first line, only to throw it away again? Why not just use:

    Connection myConn = myCall.getEarliestConnection();
    

    In general, it's worth declaring local variables as late as you can, ideally when you have a useful value - so in your method, you could declare c within the loop, assign t a value in the declaration, declare l at the point of assignment, etc. You should also consider using generics where possible - e.g. List instead of just the raw List type.

    EDIT: In contrast to the accepted answer, I would suggest changing getConnections() to always return a non-null value, returning an empty list if there are no connections. That will be easier to work with.

提交回复
热议问题