I wrote a recursive function to read through our large complicated service. I get this error "TypeError: Cannot read property 'sublayers' of null". It will error out at the same place every time, until I add a piece of code anywhere in the program then it will stop somewhere else or go through all the way. It is predictably unpredictable.
onLoadStatusChanged: {
console.log(mapImageLayer.mapImageSublayers.count)
if(mapImageLayer.mapImageSublayers.count > 0){
for (var x = 0; x < mapImageLayer.mapImageSublayers.count; x++) {
console.log("x = " + x.toString())
console.log("Outside: " + mapImageLayer.mapImageSublayers.get(x).name)
if(mapImageLayer.mapImageSublayers.get(x).sublayers.count > 0){
iname(mapImageLayer.mapImageSublayers.get(x))
}
}
}
}
function iname(thislayer) {
for (var y =0; y < thislayer.sublayers.count; y++) {
console.log(thislayer.subLayerContents
console.log(thislayer.subLayerContents
console.log("y = " + y.toString())
console.log("count= " + thislayer.subLayerContents
if(thislayer.subLayerContents
console.log(thislayer.subLayerContents
thislayer.subLayerContents
}else{
thislayer.subLayerContents
}
if(thislayer.subLayerContents
iname(thislayer.subLayerContents
}
}
}
Console:
qml: 9
qml: x = 0
qml: Outside: Districts
qml: Community College
qml: 1
qml: y = 0
qml: count= 0
qml: Supervisorial
qml: 2
qml: y = 1
qml: count= 0
qml: Elementary School
qml: 3
qml: y = 2
qml: count= 0
qml: High School
qml: 4
qml: y = 3
qml: count= 0
qml: Unified School
qml: 5
qml: y = 4
qml: count= 0
qml: Voter Precincts
qml: 6
qml: y = 5
qml: count= 0
qml: x = 1
qml: Outside: OC Development Services
qml: GENERAL PLAN
qml: 8
qml: y = 0
qml: count= 0
qml: Zoning
qml: 9
qml: y = 1
qml: count= 0
ArcGIS.AppFramework.Player: Object: QObject(0x0) Text: "file:///C:/Users/huntj/ArcGIS/AppStudio/Apps/6154a8cf097142b38e223b3d9abc999d/MonTopo2.qml:704: TypeError: Cannot read property 'sublayers' of null"
file:///C:/Users/huntj/ArcGIS/AppStudio/Apps/6154a8cf097142b38e223b3d9abc999d/MonTopo2.qml:704: TypeError: Cannot read property 'sublayers' of null
Hi,
Seems most likely like a timing issue. Have you made sure that all of the loadable classes are loaded? For example, the map image layer needs to be in state loadStatus === Enums.LoadStatusLoaded. And each sublayer needs to be loaded before accessing sublayers. More details here - https://developers.arcgis.com/qt/latest/qml/guide/loadable-pattern.htm
for example, you could do something like
sublayer.loadStatusChanged.connect(function(){
if (sublayer.loadStatus === Enums.LoadStatusLoaded) {
// access the sublayers now
}
});