1

I am getting a java.lang.NullPointerException error, and even though I checked mInternshipId and it is the same as I was expecting, it does not work.

This is my Firebase Content

enter image description here

This is my Internship Class

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;
    }
}

This is where I am using it. It shows that mInternship is not been initialized. Thank you everyone in advance for your time.

//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) {
        }
    });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

The problem is at this line

mInternshipId = intent.getStringExtra(INTERNSHIP_ID);

you are not getting your extra, so you can't reference to an object inside your Firebase Database, and this is causing a null pointer at a null reference to retrieve the data.

Make sure that you are getting the extra in this class, put a debug point and see the value of mInternshipId

There is a way to check if mInternshipId is the problem.

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

Run this line without mInternshipId and hardcode a child to get the data.

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

If that works with a hardcode child, you can assure that the problem is at your getStringExtra().

halfer
  • 19,824
  • 17
  • 99
  • 186
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77