I am using grails 2.3.3 in a development and I'm having a problem working with a 2 dimensional array that has been taken from a json web page. The particular problem occurs when there are null values in 3rd column of the 2D array.
Here is an overview of the workflow in grails:
The json object is first rendered:
render ( ['P' : jsonObject.picId, 'A' : jsonObject.audioId, 'C' : jsonObject.caption, 'V' : jsonObject.vidTemplateId ] as JSON)
It is then recast into a 2D array to pass in a storeVideoSlotData service:
def vidpNmList = [][]
vidpNmList[0] = jsonObject.picId
vidpNmList[1] = jsonObject.audioId
vidpNmList[2] = jsonObject.caption
Then a call to the service which passes vidpNmList:
def vsaInst = vdSlotService.storeVideoSlotData( vidpNmList, session)
Within the service it gets recast again into it's three separate components:
def picId = vidpNmList[0]
def audioId = vidpNmList[1]
def caption = vidpNmList[2]
Here is a listing of the two forms of the data:
vidpNmList: [[331, 332, 334, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null, null, null], [null, Input Caption Data, Input Caption Data, null, null, null, null, null, null, null, null]]
picId: [331, 332, 334, null, null, null, null, null, null, null, null, null]
audioId:[null, null, null, null, null, null, null, null, null, null, null, null]
Captions: [null, Input Caption Data, Input Caption Data, null, null, null, null, null, null, null, null]
Within this service any reference to specific values in this caption column that contain null values causes an error:
When caption[currSlotNo] has a null value this line causes an error:
if(!(caption[currSlotNo]) ) {
log.debug(" caption is null and cannot be displayed. ")
}
The error being:
homevu1.VdSlotService updVideoSlotData pictureInst : 331 picId[currSlotNo]: 331 homevu1.VdSlotService updVideoSlotData picMK: 331
homevu1.VdSlotService updVideoSlotData audMK: null
errors.GrailsExceptionResolver JSONException occurred when processing request: [POST] /HomeVu1/videoShr/getJsVidCoords
JSONArray[0] not found.. Stacktrace follows: org.codehaus.groovy.grails.web.json.JSONException: JSONArray[0] not found.
The same error occurs if I refer to the value of the element within the vidpNmList array.
What am I doing wrong or perhaps there is some work around?
Here is a workaround I've introduced with a 'try/catch' construct whcih leaves the 'captionVal' as null:
def captionVal = null
try{ captionVal = caption[currSlotNo]
log.debug(" ${functionName} caption value assigned correctly ")
}
catch (Exception eWrite)
{
// fails - can only trap and work around the error atm.
log.debug(" ${functionName} caption value fails to assign correctly - remains at null default - Error Message: $eWrite")
}
Code showing resetting the json object caption element from a "null" string to an actual null prior to assigning it to vidpNmList:
for (def iC = 0; iC < jsonObject.caption.size() ; iC++)
{
String currCap = jsonObject.caption[iC]
if( currCap == "null")
{
log.debug(" getJsVisCoords null value iC: $iC " + " caption[iC]: " + jsonObject.caption[iC] + " currCap Length: " + currCap.length())
jsonObject.caption[iC] = null
}
else
{
log.debug(" getJsVisCoords Non null value iC: $iC " + " caption[iC]: " + jsonObject.caption[iC] + " currCap Length: " + currCap.length())
}
}
vidpNmList[2] = jsonObject.caption
-mike