-1

im set marker onclick to another activity

public class CoffeeShopDetail  extends AppCompatActivity {
 TextView DetailText;
 TextView DetailTipe;
 TextView DetailAsal;
 ImageView DetailImage;
 private CustomGauge gauge1;
 TextView DetailPersen;

 int i;
 private TextView text1;


 String LokasiId="";
 FirebaseDatabase database;
 DatabaseReference lokasi;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.detail_loc);


     database = FirebaseDatabase.getInstance();
     lokasi = database.getReference().child("Lokasi");

     DetailText = findViewById(R.id.detail_shop);
     DetailImage = findViewById(R.id.detail_image1);
     DetailTipe = findViewById(R.id.descCS);
     DetailAsal = findViewById(R.id.tandatanya);
     Intent intent = getIntent();
     Bundle bd = intent.getExtras();



    if (getIntent() != null)
         LokasiId = getIntent().getStringExtra("LokasiId");
     if ("LokasiId".equals(true)) {

         return;

     }
     getDetailLokasi(LokasiId);



 }

 //-----1-----
 private void getDetailLokasi(final String LokasiId) {

     lokasi.child(String.valueOf(LokasiId)).addValueEventListener(new ValueEventListener() {

             @Override
             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                 final LocationDB LocationDB = dataSnapshot.getValue(LocationDB.class);

                     Picasso.get().load(LocationDB.getImage()).into(DetailImage);

                     DetailText.setText(LocationDB.getNama_tempat());
                     // DetailTipe.setText(Jenis.getTipe_kopi());
                     //  DetailAsal.setText(Jenis.getAsal());
                     // DetailPersen.setText(Jenis.percent);

             }

             @Override
             public void onCancelled(@NonNull DatabaseError databaseError) {

             }
         }
     );

 }
}

ERROR

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 491
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.myapplication.Lokasi.LocationDB.getImage()' on a null object reference
        at com.example.myapplication.CoffeeShopDetail$1.onDataChange(CoffeeShopDetail.java:87)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@18.0.1:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@18.0.1:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@18.0.1:55)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Have you ever tried changing the variable name of LocationDB?

Because the LocationDB is a data Model Class and you are using it as a variable.

But you can try this changing your variable LocationDB to other name.

 final LocationDB myLocationDB= dataSnapshot.getValue(LocationDB.class);


 Picasso.get().load(myLocationDB.getImage()).into(DetailImage);

  DetailText.setText(myLocationDB.getNama_tempat());
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
0

It is not necessary that you will get data what you are requested for. So instead of directly binding it with view, first add some validation on it.

         public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
             if(dataSnapshot.getValue(LocationDB.class) != null) {
                 final LocationDB locationDB = dataSnapshot.getValue(LocationDB.class);

                 Picasso.get().load(locationDB.getImage()).into(DetailImage);

                 DetailText.setText(locationDB.getNama_tempat());
                 // DetailTipe.setText(Jenis.getTipe_kopi());
                 //  DetailAsal.setText(Jenis.getAsal());
                 // DetailPersen.setText(Jenis.percent);
             } else {

                 Picasso.get().load("").error(R.drawable.placeholder_error_loading).into(DetailImage);

                 DetailText.setText("");
             }
         }

Tip: Always use good naming convention for variables. i.e., always start variable name with lowercase like mVariableName, variableName.

DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27