why it always return null when i use java multichain sdk to retrive stream items

+1 vote

everything goes well if i use the command line or interactive mode , i create the stream , i grant the send permission , i subscribe to it , i publish item to stream , when i use the command line or interactive mode the items returned successfully ....but when i use below  java command to retrive the items via postman ....only null values returns ..... for example . i published 2 items , it will return "null,null" ,  i publish 3 items , it returns "null,null,null" ,   i tested this both on root stream and my own stream , the same output

    List<StreamKeyItem> items = new ArrayList<>();
    try {
        items = (List<StreamKeyItem>) cm.invoke(CommandElt.LISTSTREAMITEMS, streamidentifier, true, 10, 0);
    } catch (MultichainException e) {
        e.printStackTrace();
        //should return meaningful message to the front-end
        return GSonUtil.getInstance().object2Json(new MultichainOperationResult(e.getMessage(), false));
    }
    return GSonUtil.getInstance().object2Json(items);
}
asked Jan 23, 2020 by Leo-LL
You would need to contact the author of this library about this.

1 Answer

0 votes

I am using the same Java API and facing this issue while trying to retrieve the stream items.
I have found that the problem occur because here you are directly converting the output of invoke method into  List of StreamKeyItem.

items = (List<StreamKeyItem>) cm.invoke(CommandElt.LISTSTREAMITEMS, streamidentifier, true, 10, 0);
Because of that you are getting null values.
Please replace the below code with your existing one to get output..

Object result = null;
List<StreamKeyItem> items = new ArrayList<>();

try {
        final Object[] params = new Object[] { "stream_name", Boolean.TRUE, 10, 0 };

result = commandManager.invoke(CommandElt.LISTSTREAMITEMS, params);

items = (ArrayList<StreamKeyItem>) result;

    } catch (MultichainException e) {

        e.printStackTrace();

        //should return meaningful message to the front-end

        return GSonUtil.getInstance().object2Json(new MultichainOperationResult(e.getMessage(), false));

    }

    return GSonUtil.getInstance().object2Json(items);


So, As per the conclusion whatever output came from invoke method, you should have to take it into the Object and then convert it.

answered Feb 14, 2020 by Nikunj Dhameliya
...