This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                在8个月前关闭。
            
                    
我收到一个java.lang.NullPointerException错误,即使我检查了mInternshipId并且它与我期望的相同,但它不起作用。

这是我的Firebase内容

java - 检索Firebase对象空指针异常-LMLPHP

这是我的实习班

public class Internship {
    private String internshipId;
    private String internshipTitle;
    private String internshipDesc;
    private String internshipDate;
    private String internshipImage;
    private String internshipCreatorId;

    public Internship() {

    }

    public Internship(String internshipId, String internshipTitle, String internshipDesc, String internshipDate, String internshipImage, String internshipCreatorId) {
        this.internshipId = internshipId;
        this.internshipTitle = internshipTitle;
        this.internshipDesc = internshipDesc;
        this.internshipDate = internshipDate;
        this.internshipImage = internshipImage;
        this.internshipCreatorId = internshipCreatorId;
    }

    public String getInternshipTitle() {
        return internshipTitle;
    }


    public String getInternshipId() {
        return internshipId;
    }

    public void setInternshipId(String internshipId) {
        this.internshipId = internshipId;
    }

    public void setInternshipTitle(String internshipTitle) {
        this.internshipTitle = internshipTitle;
    }

    public String getInternshipDesc() {
        return internshipDesc;
    }

    public void setInternshipDesc(String internshipDesc) {
        this.internshipDesc = internshipDesc;
    }

    public String getInternshipDate() {
        return internshipDate;
    }

    public void setInternshipDate(String internshipDate) {
        this.internshipDate = internshipDate;
    }

    public String getInternshipImage() {
        return internshipImage;
    }

    public void setInternshipImage(String internshipImage) {
        this.internshipImage = internshipImage;
    }

    public String getInternshipCreatorId() {
        return internshipCreatorId;
    }

    public void setInternshipCreatorId(String internshipCreatorId) {
        this.internshipCreatorId = internshipCreatorId;
    }
}


这是我使用它的地方。它表明mInternship尚未初始化。预先感谢大家的宝贵时间。

//Get The Internship ID
    Intent intent = getIntent();
    mInternshipId = intent.getStringExtra(INTERNSHIP_ID);

    //Initialize the Database Reference
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Internships").child(mInternshipId);
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            //Initialize Useful Variables
            mInternship = dataSnapshot.getValue(Internship.class);
            organizationId = mInternship.getInternshipCreatorId();
            databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child("Organizations").child(organizationId);
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    internshipLocation = (String) dataSnapshot.child("location").getValue();
                }

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

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

最佳答案

问题是在这条线上

mInternshipId = intent.getStringExtra(INTERNSHIP_ID);


您没有得到额外的收益,因此您无法引用Firebase数据库中的对象,这会导致在null引用处指向null指针以检索数据。

确保您在此类中得到更多帮助,放置一个调试点并查看mInternshipId的值

有一种方法可以检查mInternshipId是否是问题所在。

databaseReference = FirebaseDatabase.getInstance().getReference().child("Internships").child(mInternshipId);


在不使用mInternshipId的情况下运行此行,并对子代进行硬编码以获取数据。

databaseReference = FirebaseDatabase.getInstance().getReference().child("Internships").child("-LchJYhSf3uwlv7AAXtr");


如果这适用于硬编码的子级,则可以确保问题出在您的getStringExtra()上。

07-26 08:49