currentState variable is not defined in State object neither determined in it subclass CP
Hello, I've got this error while deserializing the byte[] cp to the cp object.
org.json.JSONException: JSONObject["currentState"] not found while deserializing cp using java
And I found out that the object to deserialize (CP-->byte[]) doesn't have the variable currentState determined.
To reproduce : Follow the cp tutorial using java untill the response of the issue tx.
Can you point to the file and code line in question?
Sure.
In the application-java papernet, the CommercialPaper.java has a field "currentState" on the deserialize function which has not been defined in the contract-java so not in the byte object to be serialized.
I tried to declaring a int currentState in the State Object and define it in CommercialPaper both in contract-java and papernetwork but I couldn't still get the current state of the object.
I ended up getting rid of current state variable and used another approch by getting the state of the object using the state variable defined in CP while deserializing.
Here is the function in commercial-paper/organization/magnetocorp/application-java/src/org/papernet
`/** * Deserialize a state data to commercial paper * * @param {Buffer} data to form back into the object */ public static CommercialPaper deserialize(byte[] data) { JSONObject json = new JSONObject(new String(data, UTF_8));
String issuer = json.getString("issuer");
String paperNumber = json.getString("paperNumber");
String issueDateTime = json.getString("issueDateTime");
String maturityDateTime = json.getString("maturityDateTime");
String owner = json.getString("owner");
int faceValue = json.getInt("faceValue");
int currentState = json.getInt("currentState");
String state = STATES[currentState-1];
return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue, owner, state);
}
`
Why can't I install the chain code after I change it~ I need help ...
Make sure you rebuild your smart contracts and have the same code on both organisation. After rebuilding with gradle, the installation, definition and committing should work fine.
The issue was in state variable. I changed it to String type. Below code change is working. /** * Deserialize a state data to commercial paper * * @param {Buffer} data to form back into the object */ public static CommercialPaper deserialize(byte[] data) { JSONObject json = new JSONObject(new String(data, UTF_8));
String issuer = json.getString("issuer");
String paperNumber = json.getString("paperNumber");
String issueDateTime = json.getString("issueDateTime");
String maturityDateTime = json.getString("maturityDateTime");
String owner = json.getString("owner");
int faceValue = json.getInt("faceValue");
//below is the change
String state = json.getString("state");
return createInstance(issuer, paperNumber, issueDateTime, maturityDateTime, faceValue, owner, state);
}
Had figured it out, was just lazy updating here.
The current code in the release-2.2 branch looks to match the working code above so I will close this issue.