Firebase检索数据失败。

huangapple go评论92阅读模式
英文:

Firebase Retrieve Data Failed

问题

我遇到了这个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.posinventory/com.example.posinventory.Bill}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference(java.lang.String)' on a null object reference

我发现当我尝试将以下代码添加到我的代码中时出现了这个错误:

di = mDatabase.getReference("transaction").child("Bill").child(id);

这是从Transaction子节点Bill数据库获取数据的整个代码:

public class Bill extends AppCompatActivity {
    // ... (这里省略了一部分变量声明)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ... (这里省略了一部分界面初始化的代码)

        // 初始化 Firebase 数据库
        mDatabase = FirebaseDatabase.getInstance();

        // ... (这里省略了一部分界面元素的初始化代码)

        // 获取 info 数据
        du = mDatabase.getReference("info");
        du.keepSynced(true);
        du.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // ... (这里省略了一部分处理 info 数据的代码)
            }

            @Override
            public void onCancelled(DatabaseError error) {
            }
        });

        Intent intent = getIntent();
        String id = intent.getStringExtra("key");
        // 获取 transaction 数据
        di = mDatabase.getReference("transaction").child("Bill").child(id);
        di.keepSynced(true);
        di.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // ... (这里省略了一部分处理 transaction 数据的代码)
            }

            @Override
            public void onCancelled(DatabaseError error) {
            }
        });
    }
}

Firebase 数据库截图:

Firebase检索数据失败。

英文:

I get this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.posinventory/com.example.posinventory.Bill}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference(java.lang.String)' on a null object reference

and I found out that when I try to add di = mDatabase.getReference("transaction").child("Bill").child(id); into my code.

This is the whole code to get data from Transaction child Bill database

public class Bill extends AppCompatActivity {
    TextView ph1, ph2, storename, storeaddr, transid, dlnum, date, gst, ptname, age, docname, totamt, name;
    TextView medname[], unit[], batch[], qty[], rate[], exp[], amt[];
    String ids[];
    private DatabaseReference du;
    private DatabaseReference di;
    private FirebaseDatabase mDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.invoice);
        getSupportActionBar().hide();

        ph1 = (TextView) findViewById(R.id.ph1);
        ph2 = (TextView) findViewById(R.id.ph2);
        storename = (TextView) findViewById(R.id.storename);
        storeaddr = (TextView) findViewById(R.id.storeaddr);
        transid = (TextView) findViewById(R.id.transid);
        dlnum = (TextView) findViewById(R.id.dlnum);
        date = (TextView) findViewById(R.id.date);
        gst = (TextView) findViewById(R.id.gst);
        ptname = (TextView) findViewById(R.id.ptname);
        age = (TextView) findViewById(R.id.age);
        docname = (TextView) findViewById(R.id.docname);
        totamt = (TextView) findViewById(R.id.totamt);
        name = (TextView) findViewById(R.id.name);

        medname = new TextView[4];
        qty = new TextView[4];
        amt = new TextView[4];

        ids = new String[] { "medname", "qty", "amt" };
        for (int j = 1; j <= 4; j++) {
            medname[j - 1] = (TextView) findViewById(getResources().getIdentifier(ids[0] + j, "id", getPackageName()));
            qty[j - 1] = (TextView) findViewById(getResources().getIdentifier(ids[1] + j, "id", getPackageName()));
            amt[j - 1] = (TextView) findViewById(getResources().getIdentifier(ids[2] + j, "id", getPackageName()));
        }


        du = mDatabase.getReference("info");
        du.keepSynced(true);
        du.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
                };
                Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
                name.setText(map.get("name"));
                storename.setText(map.get("storename"));
                dlnum.setText(map.get("tin"));
                storeaddr.setText(map.get("addr"));
                ph1.setText("Ph: " + map.get("ph1"));
                ph2.setText("Ph: " + map.get("ph2"));
                gst.setText(map.get("gst"));
            }

            @Override
            public void onCancelled(DatabaseError error) {
            }
        });

        Intent intent = getIntent();
        String id = intent.getStringExtra("key");
        di = mDatabase.getReference("transaction").child("Bill").child(id);
        di.keepSynced(true);
        di.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
                };
                Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
                totamt.setText(map.get("gtotamt"));
                date.setText(map.get("gdate"));
                String items = map.get("gitems");
                StringTokenizer st = new StringTokenizer(items, "®");
                int k = -1;
                while (st.hasMoreTokens()) {
                    k++;
                    // Toast.makeText(Bill.this,""+k , Toast.LENGTH_SHORT).show();
                    StringTokenizer sti = new StringTokenizer(st.nextToken(), "©");
                    while (sti.hasMoreTokens()) {
                        medname[k].setText(sti.nextToken());
                        qty[k].setText(sti.nextToken());
                        amt[k].setText(sti.nextToken());
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
            }
        });

    }


}

Firebase Database:

Firebase检索数据失败。

答案1

得分: 0

你尚未声明getInstance()。尝试添加以下内容:

du = mDatabase.getInstance().getReference("info")

以及

di = mDatabase.getInstance().getReference("transaction").child("Bill").child(id)

我的意思是,当你遇到mDatabase时,在其后面加上getInstance()。然后告诉我更新情况。

英文:

you have not declared getInstance(). Try adding
du = mDatabase.getInstance().getReference("info") and
di =mDatabase.getInstance().getReference("transaction").child("Bill").child(id)
. I mean when you face a mDatabase add a getInstance() to it. And let me know the update.

huangapple
  • 本文由 发表于 2020年9月17日 21:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63939541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定