firebaseを使ってアプリから投稿された内容を管理しています。
firebaseは下記の写真のようになっています。
![]
![
ここから現在ログインしているユーザーが投稿したものだけマップ画面に表示させたいのですが、
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
というエラーメッセージが出てきてしまいます。
MainActivity.javaからボタン(btPost)を押したときと、オプションメニューから選んだ場合にMyPostMapsActivity.javaに行くようになっていますが、画面遷移した瞬間にエラーが出てきている状態です。
このコードで一度試した時には普通にできたのですが、違う日にもう一度やってみるとできなくなっていました。
エラーが出ているコードはMyPostMapsActivity.java内の**if(name.equal(username))**に出ています。NullPointExceptionなのでusernameがnullになっているのではないかと思ったのですが、そうでもなさそうだったので、どこがおかしいのかわからず、質問しました。
該当するコードとそれに関係するコードは下記に示すとおりです。
MainActivity.java
java
1@Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 Intent intent=getIntent(); 6 extraname=intent.getStringExtra("name"); 7 TextView tvExample = findViewById(R.id.tvExample); 8 9 maxid=0; 10 btPost = findViewById(R.id.btPost); 11 btPost.setOnClickListener(new View.OnClickListener() { 12 @Override 13 public void onClick(View view) { 14 onPostClicked(view); 15 } 16 }); 17 //UIDの取得 18 mAuth=FirebaseAuth.getInstance(); 19 userID=mAuth.getCurrentUser().getUid(); 20 username=mAuth.getCurrentUser().getDisplayName(); 21 System.out.println(userID+":"+username); 22 Toast.makeText(MainActivity.this,"ようこそ"+username+"さん!",Toast.LENGTH_SHORT).show(); 23 //Postテーブル 24 post=new Post(); 25 reffpost=FirebaseDatabase.getInstance().getReference().child("Post"); 26 reffpost.addValueEventListener(new ValueEventListener() { 27 @Override 28 public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) { 29 if(snapshot.exists()){ 30 maxid=(snapshot.getChildrenCount()); 31 } 32 } 33 34 @Override 35 public void onCancelled(@NonNull @NotNull DatabaseError error) { 36 37 } 38 }); 39 } 40//入力内容をデータベースに登録 41 public void onPostClicked(View View){ 42 43 //データ追加 44 post.setName(username); 45 post.setTitle(title); 46 post.setDetail(detail); 47 post.setLatitude(latitude); 48 post.setLongitude(longitude); 49 if(comment.equals("")){ 50 post.setComment("なし"); 51 }else{ 52 post.setComment(comment); 53 } 54 if(image==null){ 55 image="null"; 56 } 57 post.setImage(imageName); 58 post.setUserId(userID); 59 maxid++; 60 reffpost.child(String.valueOf(maxid+1)).setValue(post); 61 Toast.makeText(MainActivity.this,"投稿されました!",Toast.LENGTH_LONG).show(); 62 Intent intent =new Intent(MainActivity.this,MyPostMapsActivity.class); 63 intent.putExtra("name",username); 64 startActivity(intent); 65 } 66 } 67@Override 68 public boolean onOptionsItemSelected(MenuItem item){ 69 int itemId=item.getItemId(); 70 switch (itemId){ 71 case R.id.menuSeeMyMap: 72 Intent mypostIntent=new Intent(MainActivity.this,MyPostMapsActivity.class); 73 mypostIntent.putExtra("name",username); 74 startActivity(mypostIntent); 75 break; 76 //省略 77 } 78 return super.onOptionsItemSelected(item); 79 } 80
Post.java
java
1public class Post { 2 public String name; 3 public String title; 4 public String detail; 5 public String image; 6 public double latitude; 7 public double longitude; 8 public String comment; 9 public String userId; 10 11 public Post(){ 12 13 } 14 public String getName(){ 15 return name; 16 } 17 public void setName(String name){ 18 this.name=name; 19 } 20 public String getDetail(){ 21 return detail; 22 } 23 public void setDetail(String detail){ 24 this.detail=detail; 25 } 26 public String getTitle(){ 27 return title; 28 } 29 public void setTitle(String title){ 30 this.title=title; 31 } 32 public String getImage(){return image;} 33 public void setImage(String image){ 34 this.image=image; 35 } 36 public double getLatitude(){return latitude;} 37 public void setLatitude(double latitude){ 38 this.latitude=latitude; 39 } 40 public double getLongitude(){return longitude;} 41 public void setLongitude(double longitude){ 42 this.longitude=longitude; 43 } 44 public String getComment(){return comment;} 45 public void setComment(String comment){ 46 this.comment=comment; 47 } 48 public String getuserId(){return userId;} 49 public void setUserId(String userId){ 50 this.userId=userId; 51 } 52}
MyPostMapsActivity.java
java
1public class MyPostMapsActivity extends FragmentActivity implements OnMapReadyCallback { 2 3 //省略 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_my_post_maps); 9 // Obtain the SupportMapFragment and get notified when the map is ready to be used. 10 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 11 .findFragmentById(R.id.map); 12 mapFragment.getMapAsync(this); 13 Intent intent=getIntent(); 14 username=intent.getStringExtra("name"); 15 Toast.makeText(MyPostMapsActivity.this,"ようこそ"+username,Toast.LENGTH_SHORT).show(); 16 maxId=intent.getLongExtra("maxId",0); 17 18 } 19 20 public void setMarker(String title,String detail,LatLng location,Bitmap bitmap){ 21 mMap.setInfoWindowAdapter(new CustomInfoAdapter()); 22 MarkerOptions options=new MarkerOptions(); 23 options.position(location); 24 options.title(title); 25 options.snippet(detail); 26 Marker marker=mMap.addMarker(options); 27 markerList[n-1]=marker; 28 marker.showInfoWindow(); 29 } 30 31 private class CustomInfoAdapter implements GoogleMap.InfoWindowAdapter{ 32 private final View mWindow; 33 public CustomInfoAdapter(){ 34 mWindow=getLayoutInflater().inflate(R.layout.info_window_layout,null); 35 } 36 37 @Override 38 public View getInfoWindow(Marker marker){ 39 render(marker,mWindow); 40 return mWindow; 41 } 42 @Override 43 public View getInfoContents(Marker marker){ 44 return null; 45 } 46 47 private void render(Marker marker,View view){ 48 for(int a=0;a<100;a++){ 49 if(marker.equals(markerList[a])){ 50 ivMapCamera.setImageBitmap(bitmapList[a]); 51 tvmapComment.setText(commentList[a]); 52 } 53 } 54 tvmapTitle.setText(marker.getTitle()); 55 tvmapDetail.setText(marker.getSnippet()); 56 } 57 } 58 59 /** 60 * Manipulates the map once available. 61 * This callback is triggered when the map is ready to be used. 62 * This is where we can add markers or lines, add listeners or move the camera. In this case, 63 * we just add a marker near Sydney, Australia. 64 * If Google Play services is not installed on the device, the user will be prompted to install 65 * it inside the SupportMapFragment. This method will only be triggered once the user has 66 * installed Google Play services and returned to the app. 67 */ 68 @Override 69 public void onMapReady(GoogleMap googleMap) { 70 mMap = googleMap; 71 // LatLng present=new LatLng(platitude,plongitude); 72 LatLng test=new LatLng(35.68944,139.69167); 73 // Add a marker in Sydney and move the camera 74 //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 75 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(test,7)); 76 77 //データベース取得処理 78 reff=FirebaseDatabase.getInstance().getReference("Post"); 79 reff.addValueEventListener(new ValueEventListener() { 80 @Override 81 public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) { 82 for(DataSnapshot data: snapshot.getChildren()){ 83 Post post=new Post(); 84 post=data.getValue(Post.class); 85 assert post != null; 86 String name=post.getName(); 87 if(name.equals(username)){ //エラー箇所 88 String title=post.getTitle(); 89 String detail=post.getDetail(); 90 String image=post.getImage(); 91 double latitude=post.getLatitude(); 92 double longitude=post.getLongitude(); 93 LatLng location=new LatLng(latitude,longitude); 94 String comment=post.getComment(); 95 commentList[i]=comment; 96 imageList[i]=image; 97 i++; 98 StoragePicked(image,title,detail,comment,location); 99 } 100 } 101 } 102 103 @Override 104 public void onCancelled(@NonNull @NotNull DatabaseError error) { 105 //データ取得失敗 106 Toast.makeText(MyPostMapsActivity.this,"Postテーブルのデータ取得に失敗しました",Toast.LENGTH_SHORT).show(); 107 } 108 }); 109 } 110 111 public void StoragePicked(String image,String title,String detail,String comment,LatLng location){ 112 //Storageから画像取得処理 113 //省略 114 setMarker(title,detail,location,bitmap); 115 116 117 }
回答よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。