Silver: Using try? still really often results in object reference is not set to the instance of object

IDE: Fire
Version: _9.0.97.2071, 9.1.100.2076
Target: Java
Description:

Not sure how much this is helpful for you guys to debug, but try? still often results in cunning compile time error: object reference is not set to the instance of object.
I couldn’t yet find pattern but the example would be calling this method:

try? report.deleteLocally()

where delete locally is defined as

/// doesn't remove the _persistentEntity from this report object. Only deletes it.
/// after calling, you should not be accessing any accessors of deleted Report except 'hasBeenDeletedLocally'
/// calling the other accessors will lead to underfined behaviour. 
public func deleteLocally() throws {
		
	try RealmGate.write { (realm: Realm) in
		#if COOPER
			(self.persistentEntity as RealmObject).deleteFromRealm()
		#else
			realm.delete(self.persistentEntity)
		#endif
	}
		
	self.hasBeenDeletedLocally = true
}

where RealmGate.write is defined as:

public static func write(_ closure: @escaping (Realm) -> Void) throws {
		
	let realm = try self.defaultRealm()
	try realm.write {
		closure(realm)
	}
}

which is defined in java class(RealmGate) extension.
where realm.write is defined in Realm android library respectfully.

Unfortunately cannot provide the project for this thing, since it is not open-source. Hope it helps

I’m sorry, but without some way to reproduce this I can’t fix it.

@ck: This one is quite general I believe, play around with try?, I have them occurring quite often, I am sure you will find a few object reference is not set. I will provide you a sample project for this a bit later. If you really wish to reproduce the same issue as I have, you will need to

  1. use realm android library as dependency in Fire
  2. create a Java library with sample realm object.
  3. Define a RealmGate class that has write method defined above:
public static func defaultRealm() throws -> Realm {
	#if COOPER
		return self.getDefaultRealm()
	#else
		return try Realm()
	#endif
}
  1. add the write method from my previous post to RealmGate.
  2. Create a container that will store realmObject inside.
  3. add the deleteLocally() method to this container.
  4. call try? realmContainer.deleteLocally()