是啥导致了这个 NullPointerException? [复制]
Posted
技术标签:
【中文标题】是啥导致了这个 NullPointerException? [复制]【英文标题】:What makes this NullPointerException? [duplicate]是什么导致了这个 NullPointerException? [复制] 【发布时间】:2019-11-01 01:49:21 【问题描述】:我从源中收到一个空错误,以显示我的关注和关注列表。 以前它工作,但由于某种原因突然发生错误。 不知为何找了几天。 帮帮我..
我已经检查了我的 firebase 数据列表,但我不知道这不是 firebase 问题
这是我的错误
2019-06-18 21:19:51.482 1875-1894/? E/memtrack: Couldn't load memtrack module
2019-06-18 21:19:54.043 14937-14937/com.example.blogapp E/result db user: 2okCgbdTqlVgK6oDqPiYPwyM0kD3
2019-06-18 21:19:54.044 14937-14937/com.example.blogapp E/result db user: 2zUL5Bv7CcPnBENXuWZrr1bxbgs1
2019-06-18 21:19:54.044 14937-14937/com.example.blogapp E/result db user: 6YZdQrf5RycKO1kwvze0vCXL8V83
2019-06-18 21:19:54.045 14937-14937/com.example.blogapp E/id: 6YZdQrf5RycKO1kwvze0vCXL8V83
2019-06-18 21:19:54.045 14937-14937/com.example.blogapp E/arrayList: 7
2019-06-18 21:19:54.045 14937-14937/com.example.blogapp E/result db user: 8wYJrIJ1brPTFvY1GdH2cxv9W4v1
2019-06-18 21:19:54.045 14937-14937/com.example.blogapp E/result db user: BCka8CPfqhfyWZlhgVAwJXDA8fi1
2019-06-18 21:19:54.045 14937-14937/com.example.blogapp E/id: BCka8CPfqhfyWZlhgVAwJXDA8fi1
2019-06-18 21:19:54.045 14937-14937/com.example.blogapp E/arrayList: 7
2019-06-18 21:19:54.046 14937-14937/com.example.blogapp E/result db user: BcNxFCVZZrgxC0pp48G5LQqEP2B2
2019-06-18 21:19:54.046 14937-14937/com.example.blogapp E/result db user: Bj6OGUpKgTYq4JQ2ZtlmTNmb6xj2
2019-06-18 21:19:54.046 14937-14937/com.example.blogapp E/result db user: DAiGCMPqcyOdjIAU7RvHWC0xfbG2
2019-06-18 21:19:54.046 14937-14937/com.example.blogapp E/id: DAiGCMPqcyOdjIAU7RvHWC0xfbG2
2019-06-18 21:19:54.046 14937-14937/com.example.blogapp E/arrayList: 7
2019-06-18 21:19:54.046 14937-14937/com.example.blogapp E/result db user: J9H2hS8jBbcuDhDBdI9ENkCy3Qp1
2019-06-18 21:19:54.047 14937-14937/com.example.blogapp E/result db user: Lo4XZ1pMXGac6BXHEBc7gWz1Lbf2
2019-06-18 21:19:54.047 14937-14937/com.example.blogapp E/result db user: Meqo3PIqWrVv66kmGtVDWXknHbh1
2019-06-18 21:19:54.047 14937-14937/com.example.blogapp E/result db user: NTq9UMZTBefE5Ze2lZdc9hGGhtZ2
2019-06-18 21:19:54.047 14937-14937/com.example.blogapp E/id: NTq9UMZTBefE5Ze2lZdc9hGGhtZ2
2019-06-18 21:19:54.047 14937-14937/com.example.blogapp E/arrayList: 7
2019-06-18 21:19:54.050 14937-14937/com.example.blogapp E/result db user: NmrCWzy7aTXSTMSCnwGRGcm417u1
2019-06-18 21:19:54.050 14937-14937/com.example.blogapp E/result db user: P8uBLb9Ge5MPRbcgrOkl1xJMymI3
2019-06-18 21:19:54.050 14937-14937/com.example.blogapp E/result db user: Sjzjl1pD2PQBoCcc1x0Ji4gaFkI3
2019-06-18 21:19:54.050 14937-14937/com.example.blogapp E/id: Sjzjl1pD2PQBoCcc1x0Ji4gaFkI3
2019-06-18 21:19:54.050 14937-14937/com.example.blogapp E/arrayList: 7
2019-06-18 21:19:54.051 14937-14937/com.example.blogapp E/result db user: VxLgeLvRV9RZO8yg5MfYM2CrsfJ3
2019-06-18 21:19:54.051 14937-14937/com.example.blogapp E/result db user: bazmIYkmqwSdrkNbX6SIFlXuzO22
2019-06-18 21:19:54.051 14937-14937/com.example.blogapp E/result db user: dJCOwYuLAgfEEfhKyxT0bon8zfv2
2019-06-18 21:19:54.051 14937-14937/com.example.blogapp E/result db user: fm6XldWYbOdVQYOuiTVLTRouN573
2019-06-18 21:19:54.052 14937-14937/com.example.blogapp E/id: fm6XldWYbOdVQYOuiTVLTRouN573
2019-06-18 21:19:54.052 14937-14937/com.example.blogapp E/arrayList: 7
2019-06-18 21:19:54.052 14937-14937/com.example.blogapp E/result db user: lPKdx41KR2R3PsZh62ZM3QuyuS93
2019-06-18 21:19:54.052 14937-14937/com.example.blogapp E/result db user: m8sTZfxm3xSCysNTLdNYOabXRU23
2019-06-18 21:19:54.052 14937-14937/com.example.blogapp E/result db user: oT1sKEE9ILZjNn2B5O4Yf7gjNqV2
2019-06-18 21:19:54.057 14937-14937/com.example.blogapp E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.blogapp, PID: 14937
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:240)
at com.example.together.Activities.FollowersActivity$5.onDataChange(FollowersActivity.java:158)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@17.0.0:184)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是我的完整代码,显示关注和关注者列表
private void getFollowing()
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
.child(id).child("following");
reference.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
idList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
idList.add(snapshot.getKey());
showUsers();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
private void getFollowers()
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
.child(id).child("followers");
reference.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
idList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
idList.add(snapshot.getKey());
showUsers();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
private void showUsers()
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
userList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
User user = snapshot.getValue(User.class);
System.out.println(user+"TEST");
Log.e("result db user",user.getId());
for (String id : idList)
if (user.getId().equals(id))
Log.e("id",user.getId());
Log.e("arrayList",idList.size()+"");
userList.add(user);
userAdapter.notifyDataSetChanged();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
这是我的按钮,当我想检查我的关注和关注者列表时
followers.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(getContext(), FollowersActivity.class);
intent.putExtra("id", profileid);
intent.putExtra("title","followers");
startActivity(intent);
);
following.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(getContext(), FollowersActivity.class);
intent.putExtra("id", profileid);
intent.putExtra("title","following");
startActivity(intent);
);
我想知道是什么让它为空......
【问题讨论】:
user+"TEST" 我的日志中没有显示任何内容 user+"TEST" 已写入 System.out,可能不会出现在您的日志中。此外,从堆栈跟踪中可以明显看出 NPE 是在 LOG.e() 中引起的。如果您要传递一些具有空值的变量,请检查您出现的 LOG.e()。 java.lang.NullPointerException: println needs a message 的可能重复项(TL;DR :user.getId()
为空,这使得 Log.e("result db user",user.getId());
因您遇到的错误而失败)
【参考方案1】:
总是像下面的代码一样记录错误(附加空白字符串),
Log.e("id",user.getId()+"");
不是这样的,
Log.e("id",user.getId());
【讨论】:
以上是关于是啥导致了这个 NullPointerException? [复制]的主要内容,如果未能解决你的问题,请参考以下文章