Cadence error: unexpectedly found nil while forcing an Optional value

317 Views Asked by At

I'm running into a Cadence error on the Flow blockchain. I'm getting the error: unexpectedly found nil while forcing an Optional value when trying to save a resource to a dynamic storage path. I'm using the emulator and flow cli v.0.33.1. Here's the code:

transaction(betId: String, numMembers: Int) {
    prepare(acct: AuthAccount) {
        let adminRef = acct.borrow<&BetContractComposer.Administrator>(
            from: /storage/BetAdmin)
            ?? panic("Could not get admin reference")
        
        log("Bet ID: ".concat(betId))
        let path = StoragePath(identifier: betId)
        let publicPath = PublicPath(identifier: betId)
        acct.save(<-adminRef.createContractComposer(numMembers: numMembers),
            to: path!)
        acct.link<&BetContractComposer.ContractComposer>(publicPath!,
            target: path!)
    }
}

I get the error on the variable path in the call to acct.save(<-adminRef.createContractComposer(numMembers: numMembers), to: path!). I'm printing out the betId variable and it is not nil or empty. Any help is appreciated. Thanks!

1

There are 1 best solutions below

0
On

Figured it out. The problem was that the betId sometimes starts with a number. Cadence paths can't start with numbers. Just prepending 'bet' in front of all ID paths