Java 这怎么可能导致NullPointerException?
我正在对安卓应用程序崩溃进行验尸,我的推理无法解释应用程序如何在这种情况下结束 由于这是一个生产应用程序,它与ProGuard混淆了,我愚蠢到丢失了应用程序特定版本的映射文件。然而,通过一些巫术,我能够从中得出:Java 这怎么可能导致NullPointerException?,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在对安卓应用程序崩溃进行验尸,我的推理无法解释应用程序如何在这种情况下结束 由于这是一个生产应用程序,它与ProGuard混淆了,我愚蠢到丢失了应用程序特定版本的映射文件。然而,通过一些巫术,我能够从中得出: 0 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference 1
0 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
1 at com.einpix.android.a.a.a.a(Unknown Source)
2 at com.einpix.android.d.f.a(Unknown Source)
3 at com.einpix.android.d.f.a(Unknown Source)
4 at com.einpix.android.f.g.a(Unknown Source)
5 at com.einpix.android.d.g.onLocationChanged(Unknown Source)
6 at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:290)
...
问题实际上发生在等于(…)
方法中:
public abstract class Entity
{
private static final String TAG = "Entity";
protected int localId;
protected int remoteId;
public Entity(int localId, int remoteId)
{
this.localId = localId;
this.remoteId = remoteId;
}
public int getId()
{
return getLocalId();
}
public int getLocalId()
{
return localId;
}
public int getRemoteId()
{
return remoteId;
}
public void setLocalId(int id) {
this.localId = id;
}
public void setRemoteId(int id)
{
this.remoteId = id;
}
public boolean equals(Entity other)
{
if (other == null) {
return false;
}
if (App.DEBUG) Log.d(TAG, "equals: " + this.getClass().getCanonicalName() + " vs " + other.getClass().getCanonicalName() + " --> " + (other.getClass().getCanonicalName().equals(this.getClass().getCanonicalName()) ? "Y":"N"));
if (!other.getClass().getCanonicalName().equals(this.getClass().getCanonicalName())) {
return false;
}
if (App.DEBUG) Log.d(TAG, "equals-localId: " + this.getLocalId() + " vs " + other.getLocalId() + " --> " + (this.getLocalId() == other.getLocalId() ? "Y":"N"));
if (this.getLocalId() == other.getLocalId() && this.getLocalId() > 0) {
return true;
}
if (App.DEBUG) Log.d(TAG, "equals-remoteId: " + this.getRemoteId() + " vs " + other.getRemoteId() + " --> " + (this.getRemoteId() == other.getRemoteId() ? "Y":"N"));
if (this.getRemoteId() == other.getRemoteId() && this.getRemoteId() > 0) {
return true;
}
return false;
}
}
(有一个逻辑序列指向此位置,这是我的代码中实际使用.getClass()
的少数几个位置之一)
所以我的问题是,考虑到这个方法被调用(因此,this
无论如何都不能为null),第一个if
检查是看other
是否为null-怎么可能在某个点上对这两个方法之一调用.getClass
会导致NullPointerException
p.S.App.DEBUG
在发布应用程序之前设置为false
,因此在这种情况下,这是一个安全的假设。不过,为了完整起见,我将这些行留在这里
更新
对该方法的调用来自下面一级的if子句:
if (currentClosest != null && !currentClosest.equals(lastClosest) && !currentClosest.equals(currentChosen))
currentClosest
、lastClosest
和currentSelected
作为公共类场馆扩展实体的实例(或可能为空)
显然,在我的场地
课程中,我定义了另一个equals()
方法:
@Override
public boolean equals(Object o)
{
if (!(o instanceof Venue)) {
return false;
}
Venue v = (Venue) o;
return (this.getLocalId() == v.getLocalId() || this.getRemoteId() == v.getRemoteId());
}
这是愚蠢和不必要的,但我不认为它会在这次撞车事件中起到任何作用。此方法没有任何对.getClass()
的调用,您在哪里调用此方法?您是否意识到它不会覆盖对象的等于?(虽然Object
的equal
在另一个对象为null时不会抛出NPE)。它来自下面一个级别的if条件:if(currentClosest!=null&&!currentClosest.equals(lastClosest)&!currentClosest.equals(currentSelected))
-但不确定这是否相关。至于覆盖,我不在乎-只要我能比较实体的两个实例
,并确保它们属于同一类+它们匹配的本地或远程IDcurrentClosest
作为公共类场地扩展实体的实例
,以及lastClosest
和CurrentSelected
(可选为null
)确保在vention
类中没有另一个equals
方法被调用,而不是Entity
类中的方法。事实上,我显然有(见问题中的更新)。我仍然看不出这其中有什么作用。由于NPE来自.getClass()
调用,我仍然认为执行并触发异常的是实体的方法