0

I'm trying to make a student registration system and I keep these students in firestore. I don't want it to add student with same number when adding student and for this, I created a singleton class. This singleton class has a flag value. I used this flag to provide control if there is a student with the same number in the firestore.

but it always returns null. I don't understand.

I'm just sharing the necessary codes.

My add student class

private ActivityOgrenciEkleBinding binding;
private FirebaseFirestore mFirestore =  FirebaseFirestore.getInstance();
private LinkedHashMap<String,String> linkedHashMap;
private OgrenciyiKontrolEt ogrenciyiKontrolEt;
Singleton singleton;

public void init(){

    linkedHashMap = new LinkedHashMap<>();
    singleton = Singleton.getInstance();
    btn_Ogrenci_EKLE();


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityOgrenciEkleBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
    init();
}

public void btn_Ogrenci_EKLE(){


    binding.btnEkleOgrenci.setOnClickListener(new View.OnClickListener() 
   {

        @Override
        public void onClick(View view) {

            String email_ogr = binding.emailOgrenci.getText().toString();
            String ad_ogr = binding.isimOgrenci.getText().toString();
            String soyisim_ogr = binding.soyisimOgrenci.getText().toString();
            String no_ogr = binding.numaraOgrenci.getText().toString();
            String parola_ogr = binding.sifreOgrenci.getText().toString();

           ogrenciyiKontrolEt = new 
           OgrenciyiKontrolEt(no_ogr,OgrenciEkleActivity.this);// I 
           //created object from 
           //control class to check students


            if(email_ogr.equals("") || ad_ogr.equals("") || soyisim_ogr.equals("") || no_ogr.equals("") || parola_ogr.equals("")){
                AlertDialog.Builder builder = new AlertDialog.Builder(OgrenciEkleActivity.this);
                builder.setTitle("UYARI !");
                builder.setMessage("Boş alan bırakmayınız !");
                builder.setIcon(R.drawable.warningicon);
                builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();
            }
            else{

                ogrenciyiKontrolEt.OGR_KONTROL();
                System.out.println("singleton get flag"+singleton.getflag()); //singleton returns null here and and it never 
                       //goes into the if loop
                
                if("100".equals(singleton.getflag())){

                    AlertDialog.Builder builder = new AlertDialog.Builder(OgrenciEkleActivity.this);
                    builder.setIcon(R.drawable.warningicon);
                    builder.setMessage(no_ogr+" numaralı öğrenci zaten kayıtlı lütfen farklı bir numara giriniz.");
                    builder.setTitle("UYARI");
                    builder.setPositiveButton("TAMAM", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    builder.show();

                }
                else{


                    linkedHashMap.put("name",ad_ogr);
                    linkedHashMap.put("lastname",soyisim_ogr);
                    linkedHashMap.put("number",no_ogr);
                    linkedHashMap.put("email",email_ogr);
                    linkedHashMap.put("password",parola_ogr);

                    mFirestore.collection("Students").add(linkedHashMap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                        @Override
                        public void onSuccess(@NonNull DocumentReference documentReference) {
                            Toast toast =   Toast.makeText(OgrenciEkleActivity.this,"Öğrenci başaralı bir şekilde eklendi.",Toast.LENGTH_LONG);
                            toast.show();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast toast =   Toast.makeText(OgrenciEkleActivity.this,"Öğrenci eklerken bir hata ile karşılaşıldı.",Toast.LENGTH_LONG);
                            toast.show();
                        }
                    });
                }


            }
        }
    });

}

Control class that I created to control students

public class OgrenciyiKontrolEt {


protected String ogrenci_no;
protected String firestore_ogr_no;
protected FirebaseFirestore firebaseFirestoreDb = FirebaseFirestore.getInstance();
public Context context;
Singleton singleton  = Singleton.getInstance();

public OgrenciyiKontrolEt(String ogr_no,Context context){
    this.ogrenci_no = ogr_no;
    this.context    = context;

}



public void OGR_KONTROL(){


    firebaseFirestoreDb.collection("Students")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(@NonNull QuerySnapshot queryDocumentSnapshots) {
                    List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();

                    for(DocumentSnapshot snapshot: snapshotList ) {

                        firestore_ogr_no = snapshot.getString("number");

                        if(firestore_ogr_no.equals(ogrenci_no)) {
                            singleton.setflag("100"); //If there is a student with the same 
                                              number, I set the flag in the singleton to 100.
                        }
                    }
                }

            })

            .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast toast = Toast.makeText(context,"Bir hata ile karşılaşıldı.",Toast.LENGTH_LONG);
                toast.show();
            }
        });





 }

}

My singleton class

public class Singleton {

**EDIT**
private String flag="1";
**EDIT**

private static Singleton singleton;

private Singleton() {

}

public String getflag() {
    return flag;
}

public void setflag(String flag) {
    this.flag = flag;
}

public static Singleton getInstance() {
    if (singleton == null) {
        singleton = new Singleton();
    }

    return singleton;

 }
 }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
partizal
  • 23
  • 6

1 Answers1

1

Your flag is not initialized when you call it, Either set something as default value or initialize in constructor like

private Singleton() {
    this.flag = "";
}

or call your setFlag before first occurrence of getFalg

Vojin Purić
  • 2,140
  • 7
  • 9
  • 22
  • Thank you it worked but I set the flag to 1 as the default value and even if there is a student with the same number, the flag is never 100. it doesn't go into the if loop at all and adds as if there is no student with the same number .why is this happening ? – partizal Dec 05 '21 at 14:22
  • This is extremely misleading. The actual problem is that the callback is async, meaning the flag isn't initialized in the order OP is expecting. – Zoe Dec 05 '21 at 14:26
  • I understand you told – partizal Dec 05 '21 at 14:47