如何解决 Singelton 为 NULL 问题[重复]
Posted
技术标签:
【中文标题】如何解决 Singelton 为 NULL 问题[重复]【英文标题】:How can I solve Singelton is NULL problem [duplicate] 【发布时间】:2022-01-11 00:53:18 【问题描述】:我正在尝试制作一个学生注册系统,并将这些学生保存在 Firestore 中。我不希望它在添加学生时添加具有相同编号的学生,为此,我创建了一个单例类。这个单例类有一个标志值。我使用这个标志来控制firestore中是否有相同号码的学生。
但它总是返回 null。我不明白。
我只是分享必要的代码。
我的添加学生班级
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();
);
);
我为控制学生而创建的控制类
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();
);
我的单身班
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;
【问题讨论】:
【参考方案1】:你的 flag 在你调用它时没有被初始化,要么设置为默认值,要么在构造函数中初始化,比如
private Singleton()
this.flag = "";
或在第一次出现getFalg
之前致电您的setFlag
【讨论】:
谢谢它的工作,但我将标志设置为 1 作为默认值,即使有相同号码的学生,标志也永远不会 100。它不会进入 if 循环根本没有,并添加好像没有相同数量的学生。为什么会发生这种情况? 这是极具误导性的。实际的问题是回调是异步的,这意味着标志没有按照 OP 期望的顺序初始化。 我明白你告诉了以上是关于如何解决 Singelton 为 NULL 问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何解决“无法无条件调用运算符‘0’,因为接收者可以为‘null’”[重复]
使用 zoho 图像获取 api 时,如何在 react native 中解决图像数据响应为 null 的问题?