Java NullPointerException when saving data

I’m trying to save an object in a data table with two 1to1 relations with the user table. However when I run the following code to add them the new object is added to the table without the relations and the callback returns a java.lang.NullPointerException.

Insights insight = new Insights();
insight.setFromUser(user);
insight.setToUser(contacts.get(position));
insight.setType(type);
insight.setMessage(message);

Backendless.Data.of(Insights.class).save(insight, new AsyncCallback<Insights>() {
    @Override
    public void handleResponse(Insights response) {
        //TODO: broadcast success
        //TODO: add insight to saved list
    }

    @Override
    public void handleFault(BackendlessFault fault) {
        //TODO: broadcast failure
        Log.e("Save insight", "failure");
        Log.e("Save insight", fault.getDetail());
    }
});

Is there someone who could help me understand where the problem is?

Thank you very much

Gabriele

Hi Gabriele,

In Backendless version 4 you can no longer save related objects along with the parent one. Instead you should create all objects first, then atomically set the relations between them using Relations API (here’s the doc: https://backendless.com/docs/android/doc.html#data_relations).
In case you’ve been using version 3 before, here is the migration guide which should help you understand the differences: https://backendless.com/docs/android/doc.html#migration_from_3_x_to_4_x

Hi Sergey,

Thank you, that was probably one of the problems, however even when I change the code this way there still the problem that the object is saved but the program enters in the handleFault with the same exception and so the relations are not added.

Insights insight = new Insights();
insight.setType(2);
insight.setMessage(message);




Backendless.Data.of(Insights.class).save(insight, new AsyncCallback<Insights>() {
    @Override
    public void handleResponse(Insights response) {


        Log.w("save","ok");


        AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
            @Override
            public void handleResponse(Integer response) {


            }


            @Override
            public void handleFault(BackendlessFault fault) {


            }
        };


        BackendlessUser


        List<BackendlessUser> fromList = new ArrayList<BackendlessUser>();
        fromList.add(user);
        Backendless.Persistence.of( Insights.class ).setRelation(response, "fromUser", fromList, callback );


        List<BackendlessUser> toList = new ArrayList<BackendlessUser>();
        fromList.add(contacts.get(position));
        Backendless.Persistence.of( Insights.class ).setRelation(response, "toUser", toList, callback );


        //TODO: broadcast success
        //TODO: add insight to saved list
    }


    @Override
    public void handleFault(BackendlessFault fault) {
        //TODO: broadcast failure
        Log.e("Save insight", "failure");
        Log.e("Save insight", fault.getDetail());
    }
});

Could you please quote the exception text?

This is all I get in the logs:

12-05 … /Save insight: java.lang.NullPointerException

Is it possible for you to prepare a minimal verifiable sample reproducing the problem to help us investigate it?