Backendless Support
 
Solved

Data object stores properly, but object in callback is a blank / default constructor object

I'm trying to create the simplest app that can store and retrieve data. I've created Comment class to be the type of data stored:

  1. public class Comment {
  2. private String Message;
  3. private String authorEmail;
  4. public Comment() {
  5. this.Message = "Default message";
  6. }
  7. public String getMessage() {
  8. return Message;
  9. }
  10. public void setMessage(String message) {
  11. this.Message = message;
  12. }
  13. public String getAuthorEmail() {
  14. return authorEmail;
  15. }
  16. public void setAuthorEmail(String authorEmail) {
  17. this.authorEmail = authorEmail;
  18. }
  19. }

When I touch the Submit button, the app properly stores a Comment object in the Backendless Comment table. The Message field's data appears as expected, while the authorEmail always shows up as the default literal I specify in the code. But I don't get real data back from backendless. Only default constructor objects.

  1. public class MainActivity extends AppCompatActivity {
  2. EditText mComment;
  3. Button bSubmit;
  4. Button bRetrieve;
  5. TextView status;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. String appID = "_my_app_id_";
  11. String secretKey = "_my_secret_key_";
  12. Backendless.initApp(this,appID,secretKey,"v1");
  13. mComment = (EditText) findViewById(R.id.etComment);
  14. bSubmit = (Button) findViewById(R.id.bSubmit);
  15. bRetrieve = (Button) findViewById(R.id.bRetrieve);
  16. status = (TextView) findViewById(R.id.status);
  17. bSubmit.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. Comment comment = new Comment();
  21. comment.setMessage(mComment.getText().toString());
  22. Log.i("Comments",comment.getMessage());
  23. comment.setAuthorEmail("generic@domain.com");
  24. Backendless.Persistence.save(comment, new AsyncCallback<Comment>()
  25. {
  26. @Override
  27. public void handleResponse(Comment comment) {
  28. Log.i("Comments", "New comment saved");
  29. status.setText("New comment saved: "+comment.getMessage());
  30. mComment.setText("");
  31. }
  32. @Override
  33. public void handleFault(BackendlessFault backendlessFault) {
  34. Log.i("Fault", backendlessFault.toString());
  35. }
  36. } );
  37. }
  38. });
  39. bRetrieve.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. Backendless.Persistence.of(Comment.class).find(
  43. new AsyncCallback<BackendlessCollection<Comment>>() {
  44. @Override
  45. public void handleResponse(BackendlessCollection<Comment> data) {
  46. status.setText(data.getData().get(0).getMessage());
  47. }
  48. @Override
  49. public void handleFault(BackendlessFault backendlessFault) {
  50. Log.i("Retrieve",backendlessFault.toString());
  51. }
  52. }
  53. );
  54. }
  55. });
  56. }
  57. }

The data stores properly in the online table. However, the Comment objects I get back from the callback functions are always just a default constructor file instead of real data from backendless. The message is always "Default message" that I specified as the default value in the Comment() no argument constructor method. This is true in the callback for both handleResponse() methods used above(Persistence.save and for Persistence.find).

To be specific, my "Retrieve" button onClick method works, but it always changes my status TextView's text to "Default message" instead of whatever the first Comment's actual Message is. And when I hit the "Submit" button, it always says "New comment saved: Default message" instead of whatever it is that I had typed into my EditText.

How can I troubleshoot this issue?

FYI, I got the SDK by copying backendless.jar, version 3.0.21, into my app/libs folder. I was having issues doing it via maven dependency.

Best Answer
photo

Hi Dan,

try changing the "Message" private field to "message", like this:

private String message;

Rebuild the code and try again.

Let me know if it helped.

Regards,

Mark

Leave a Comment

Comments (6)

photo
1

Hi Dan,

Could you please let me know your app id?

Regards,

Mark

photo
1

App ID is the following:

36B067E1-F470-1927-FFE8-C99EF45CAA00

photo
2

Hi Dan,

try changing the "Message" private field to "message", like this:

private String message;

Rebuild the code and try again.

Let me know if it helped.

Regards,

Mark

photo
1

Yes, that worked. Is lowercase first character a requirement for field names?

photo
1

Yes, with the version of the SDK you use, the capitalization of the field names must match the names of the columns in your data tables.

photo
1

Ok. I didn't realize the table online was lower case. Thanks very much for helping with my silly issue.

photo