Backendless Support
 
Not a Problem

null object Reference

null object reference error on my userId

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.backendless.BackendlessUser.getUserId()' on a null object reference

Leave a Comment

Comments (14)

photo
1

Hi Nomfundo

looks like you try to get userId of undefined user, don't you?

could you try this

  1. if(Backendless.UserService.CurrentUser() != null)
  2. String userId = Backendless.UserService.CurrentUser().getUserId()

Regards, Vlad

photo
1

im trying to edit information viewed on the list view

photo
1

did you try to add "IF" condition in place where the error occurs?

photo
1

yes

photo
1

please share with us a minimal (not all file) code snippet where the problem occurs and we will look into it.

photo
1

String whereClause = "ownerId = '" + Backendless.UserService.CurrentUser().getProperty("objectId")+"'";

errorCaused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.backendless.BackendlessUser.getProperty(java.lang.String)' on a null object reference

at com.example.hellen.xchngeinvestmenttraining.AdminAppointment.buttAppoint(AdminAppointment.java:82)

photo
1

are you sure that Backendless.UserService.CurrentUser() isn't "null" ?

photo
1

yes because its showing the list view but not the right person when im suppose to edit

photo
photo
1

Vladimir asks you to show a minimal code to reproduce a problem, do not list all your code in the topic

photo
1

Hello Nomfundo,

please share your APP ID and the part of code to reproduce this issue. What are you doing before this part of code?

  1. String whereClause = "ownerId = '" + Backendless.UserService.CurrentUser().getProperty("objectId")+"'";

Regards, Olga

photo
1

APP ID 89D16D79-08F2-6325-FF5A-C104B0556200

photo
1

What steps are you doing before trying to receive current user objectId? Please

write a code snippet (what are you doing with where clause, where do you call the login method)

Regards, Olga

photo
1

String whereClause = "ownerId = '" + Backendless.UserService.CurrentUser().getObjectId() + "'";

DataQueryBuilder queryBuilder = DataQueryBuilder.create();

queryBuilder.setWhereClause(whereClause);

queryBuilder.setPageSize(100).setOffset(0);

Toast.makeText(this, getIntent().getStringExtra("objectId"), Toast.LENGTH_SHORT).show();

Backendless.Persistence.of(AppointmentsData.class).find(queryBuilder, new AsyncCallback<List<AppointmentsData>>() {

@Override

public void handleResponse(List<AppointmentsData> lshow) {

for (int x = 0; x < lshow.size(); x++) {

fetch = x;

}

editAppointment = lshow;

edId.setText(lshow.get(fetch).getObjectId());

ediCon.setText(lshow.get(fetch).getConsultant());

ediTele.setText(lshow.get(fetch).getTelemarketer());

ediDate.setText(lshow.get(fetch).getAppointmentDate());

ediDay.setText(lshow.get(fetch).getDay());

ediTime.setText(lshow.get(fetch).getTime());

ediName.setText(lshow.get(fetch).getName());

ediSurname.setText(lshow.get(fetch).getSurname());

ediCell.setText(lshow.get(fetch).getCell());

ediHome.setText(lshow.get(fetch).getHomeNumber());

ediWork.setText(lshow.get(fetch).getWorkNumber());

ediOccu.setText(lshow.get(fetch).getOccuption());

ediCamp.setText(lshow.get(fetch).getCompany());

ediPhyscal.setText(lshow.get(fetch).getPhysicalAddress1());

ediPhysical1.setText(lshow.get(fetch).getPhysicalAddress2());

ediBuild.setText(lshow.get(fetch).getBuilding());

ediBlock.setText(lshow.get(fetch).getBlock());

ediFloor.setText(lshow.get(fetch).getFloor());

ediStreet.setText(lshow.get(fetch).getStreet());

ediNear.setText(lshow.get(fetch).getNearestCorner());

ediSub.setText(lshow.get(fetch).getSuburb());

edId.setText(lshow.get(fetch).getObjectId());

}

@Override

public void handleFault(BackendlessFault backendlessFault) {

Toast.makeText(EditActivity.this, "An error occured" + backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();

}

});

photo
1

1) You didn't login the user in your code - that's why currentUser returns null.

2) please add this code

  1. if ( Backendless.UserService.CurrentUser() == null ) {
  2. throw new RuntimeException( "NO USER LOGGED IN" );
  3. }

before

  1. String whereClause = "ownerId = '" + Backendless.UserService.CurrentUser().getObjectId() + "'";

Regards, Olga

photo