Throwing null pointer exception

2019-03-06 09:41发布

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 getEarliestConnection() method of Class Call.java in my own class it's throwing a NullPointerException. You can find Call.java here http://hi-android.info/src/com/android/internal/telephony/Call.java.html. In this class there is the following method:

1.    public Connection
2.        getEarliestConnection() {
3.            List l;
4.            long time = Long.MAX_VALUE;
5.            Connection c;
6.            Connection earliest = null;
7.    
8.            l = getConnections();
9.  
10.            if (l.size() == 0) {
11.                return null;
12.            }

        for (int i = 0, s = l.size() ; i < s ; i++) {
            c = (Connection) l.get(i);
            long t;

            t = c.getCreateTime();

            if (t < time) {
                earliest = c;
                time = t;
            }
        }

        return earliest;
    }

I want to call this method in my class. the class Call.java ia an abstract class I created subclass of Call class and called the above method like this:

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

But It's throwing a NullPointerException on line no:10 of above method and line no:3 of above code.

6条回答
该账号已被封号
2楼-- · 2019-03-06 09:50

You method

l = getConnections();

returns null, so your line 10 should look like: if (l==null) {

查看更多
成全新的幸福
3楼-- · 2019-03-06 09:56

I think you forgot

CallManager cm;    
cm = new CallManager();
查看更多
何必那么认真
4楼-- · 2019-03-06 09:57

The method getEarliestConnection() makes a call to getConnections() which returns null.

Since getConnections() is abstract in Call check the method you've provided in MyCall.

查看更多
祖国的老花朵
5楼-- · 2019-03-06 09:59

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;
}
查看更多
放荡不羁爱自由
6楼-- · 2019-03-06 09:59

Your cm variable has been never initialized before getConnections() call. So, it is null and you get NullPointerException when trying to invoke cm.getActiveFgCall().

查看更多
叛逆
7楼-- · 2019-03-06 10:07

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<MyConn> 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.

查看更多
登录 后发表回答