简体   繁体   English

Java空指针异常,但已初始化

[英]java null pointer exception but has already initialized

stacktrack: 堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean' java.util.ArrayList.add(java.lang.Object)' on a null object reference  
at android.net.LinkProperties.<init>(LinkProperties.java:159)
at com.android.server.ConnectivityService.getUnfilteredActiveNetworkState(ConnectivityService.java:957)
at com.android.server.ConnectivityService.getActiveNetworkInfo(ConnectivityService.java:1024)
at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:696)
at 

linkproperties.java: linkproperties.java:

public final class LinkProperties implements Parcelable {
// The interface described by the network link.
private String mIfaceName;
private ArrayList<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
private ArrayList<InetAddress> mDnses = new ArrayList<InetAddress>();
private String mDomains;
private ArrayList<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
...
public LinkProperties(LinkProperties source) {
    if (source != null) {
        mIfaceName = source.getInterfaceName();
        for (LinkAddress l : source.getLinkAddresses()) mLinkAddresses.add(l);
        for (InetAddress i : source.getDnsServers()) mDnses.add(i);
        mDomains = source.getDomains();
        for (RouteInfo r : source.getRoutes()) mRoutes.add(r);
        mHttpProxy = (source.getHttpProxy() == null)  ?
                null : new ProxyInfo(source.getHttpProxy());
        for (LinkProperties l: source.mStackedLinks.values()) {
            addStackedLink(l);
        }
        setMtu(source.getMtu());
        mTcpBufferSizes = source.mTcpBufferSizes;
    }
}  

In the stacktrace we can find this exception in happened in init method , that means when construct this data, while doing ArrayList.add method , throw a NullPointerException . 在stacktrace中,我们可以在init方法中发现此异常,这意味着在构造此数据时,在执行ArrayList.add方法时会抛出NullPointerException。 So in the constructor method ,only mLinkAddresses , mDnses and MRoutes these three ArrayLists , but they all have been initialized. 因此,在构造函数方法中,只有mLinkAddressesmDnsesMRoutes这三个ArrayLists,但是它们都已初始化。 So I was confusing about this. 所以我对此感到困惑。

All Arraylist has been init before create ,but still get NullPointerException , and this issue only happened on MTK platform. 所有Arraylist在创建之前已经被初始化,但是仍然获得NullPointerException ,并且此问题仅在MTK平台上发生。

方法参数LinkProperties source不为null,但在调用source.getLinkAddresses(),source.getDnsServers(),source.getRoutes()可能具有null值。您必须检查此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM