Cloud Firestore - QuerySnapshot.toObjects Throws Null Pointer Exception - firebase

I've stumbled upon a strange problem. I'm trying to accomplish a simple thing - convert everything from a QuerySnapshot to a collection of a certain type.
fun createReminder(reminder: Reminder) =
remindersCollectionRef.document("${reminder.taskId}").set(reminder)
fun tryRemoveReminder(taskId: Int) =
remindersCollectionRef.document("$taskId").delete()
fun getReminders(onComplete: (List<Reminder>) -> Unit) {
remindersCollectionRef.get()
.addOnSuccessListener { querySnapshot ->
if (!querySnapshot.isEmpty)
onComplete(querySnapshot.toObjects(Reminder::class.java))
}
}
The problem is in the getReminders function. I know that there is one reminder in the collection. It is also 100% of type Reminder and as you can see I already check if querySnapshot is not empty. Still, I get a null pointer exception when I call toObjects.
Stack trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference
at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source:57)
at com.google.android.gms.internal.zzevb.zza(Unknown Source:1025)
at com.google.android.gms.internal.zzevb.zza(Unknown Source:2)
at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source:10)
at com.google.firebase.firestore.QuerySnapshot.toObjects(Unknown Source:27)
at com.mypackage.util.FirestoreUtil$Companion$getReminders$1.onSuccess(FirestoreUtil.kt:131)
at com.mypackage.util.FirestoreUtil$Companion$getReminders$1.onSuccess(FirestoreUtil.kt:18)
at com.google.android.gms.tasks.zzj.run(Unknown Source:27)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Reminder class:
data class Reminder(val taskDocRef: DocumentReference, val taskId: Int)
This issue seems really strange to me. I will appreciate your help :)

If you carefully read documentation for Cloud Firestore query requirements for mapping Custom objects here, there will be notice:
Important: Each custom class must have a public constructor that takes
no arguments. In addition, the class must include a public getter for
each property.

Make Sure that you change the rules in the Firestore
For rules go to https://firebase.google.com/docs/firestore/quickstart#secure_your_data and copy the rules of testmode and paste them in the firestore rules
It take 10 minutes to change the rules.So run the app again after 10 minutes

Related

Anonymous function results in "Null check operator used on a null value"

Minimum reproducible code:
void main() {
// All type of initialization is done.
// Throws null exception.
FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
print(message);
});
// Works.
FirebaseMessaging.onBackgroundMessage(_foo);
}
Future<void> _foo(RemoteMessage message) async {
print(message);
}
Here's the error:
E/flutter (14716): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
Null check operator used on a null value
E/flutter (14716): #0 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler
(package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:179:53)
E/flutter (14716): #1 FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/
platform_interface/platform_interface_messaging.dart:101:16)
E/flutter (14716): #2 FirebaseMessaging.onBackgroundMessage (package:firebase_messaging/src/messaging.dart:83:31)
I don't understand how come there be a null exception on using an anonymous function? Although docs states that this should not be an anonymous function but a null exception for that or am I misinterpreting something?
the cloud messaging docs:
There are a few things to keep in mind about your background message handler:
It must not be an anonymous function.
It must be a top-level function (e.g. not a class method which requires initialization).
Since the handler runs in its own isolate
outside your applications context, it is not possible to update
application state or execute any UI impacting logic. You can however
perform logic such as HTTP requests, IO operations (updating local
storage), communicate with other plugins etc.

Corda - Passing Anonymous Implementations Over RPC

Is it possible to pass anonymous object implementations over Corda's RPC interface? - For example:
Workflow
#CordaSerializable
interface ExampleInterface {
val number: Int
}
#StartableByRPC
class SquareFlow(private val example: ExampleInterface) : FlowLogic<Int>() {
#Suspendable
override fun call(): Int = example.number * example.number
}
RPC Client
val value = object : ExampleInterface {
override val number: Int = 5
}
return rpc.startFlowDynamic(SquareFlow::class.java, value).returnValue.getOrThrow()
Exception
net.corda.client.rpc.RPCException: java.util.List<*> -> Unable to create an object serializer for type class com.example.client.ExampleService$square$value$1: No unique deserialization constructor can be identified
Either annotate a constructor for this type with #ConstructorForDeserialization, or provide a custom serializer for it
Whilst this is an example, what I actually want to do is pass object : TypeReference<SomeType>(){} over RPC.
As per Corda's docs anonymous objects are not supported by their AMQP
serialization which is used in the RPC client which you can read here. This is likely due to a public constructor being needed for serialization and deserialization I imagine.
Additionally I assume you are referencing Jackson's TypeReference class which uses generics which are only available at compile time. Due to how Corda's serialization works the generic won't be available at runtime so when the TypeReference is passed from the rpc application to the corda node the corda node will not know what the generic is. I haven't tried this myself but I believe some exception will be thrown whenever the flow attempts to suspend or possibly before in the RPC client.
You can probably pass a jackson JavaType class to a flow without a problem as I believe it is whitelisted as serializable in Corda by default, if not look at how to whitelist the class here using the SerializationWhitelist. You can obtain the JavaType by instantiating a TypeFactory and calling constructType(new TypeReference<SomeType> {}). then just pass the JavaType to your flows constructor.

JpaSagaStore in conjunction with Jackson unable to properly store state

In a SpringBoot application, I have the following configuration:
axon:
axonserver:
servers: "${AXON_SERVER:localhost}"
serializer:
general: jackson
messages: jackson
events: jackson
logging.level:
org.axonframework.modelling.saga: debug
Downsizing the scenario to bare minimum, the relevant portion of Saga class:
#Slf4j
#Saga
#ProcessingGroup("AuctionEventManager")
public class AuctionEventManagerSaga {
#Autowired
private transient EventScheduler eventScheduler;
private ScheduleToken scheduleToken;
private Instant auctionTimerStart;
#StartSaga
#SagaEventHandler(associationProperty = "auctionEventId")
protected void on(final AuctionEventScheduled event) {
this.auctionTimerStart = event.getTimerStart();
// Cancel any pre-existing previous job, since the scheduling thread might be lost upon a crash/restart of JVM.
if (this.scheduleToken != null) {
this.eventScheduler.cancelSchedule(this.scheduleToken);
}
this.scheduleToken = this.eventScheduler.schedule(
this.auctionTimerStart,
AuctionEventStarted.builder()
.auctionEventId(event.getAuctionEventId())
.build()
);
}
#EndSaga
#SagaEventHandler(associationProperty = "auctionEventId")
protected void on(final AuctionEventStarted event) {
log.info(
"[AuctionEventManagerSaga] Current state: {scheduleToken={}, auctionTimerStart={}}",
this.scheduleToken,
this.auctionTimerStart
);
}
}
In the final compiled class, we will end up having 4 properties: log (from #Slf4j), eventScheduler (transient, #Autowired), scheduleToken and auctionTimerStart.
For reference information, here is a sample of the general approach I've been using for both Command and Event classes:
#Value
#Builder
#JsonDeserialize(builder = AuctionEventStarted.AuctionEventStartedBuilder.class)
public class AuctionEventStarted {
AuctionEventId auctionEventId;
#JsonPOJOBuilder(withPrefix = "")
public static final class AuctionEventStartedBuilder {}
}
When executing the code, you get the following output:
2020-05-12 15:40:01.180 DEBUG 1 --- [mandProcessor-4] o.a.m.saga.repository.jpa.JpaSagaStore : Updating saga id c8aff7f7-d47f-4616-8a96-a40044cb7e3b as {}
As soon as the general serializer is changed to xstream, the content is serialized properly, but I face another issue during deserialization, since I have private static final class Builder classes using Lombok.
So is there a way for Axon to handle these scenarios:
1- Axon to safely manage Jackson to ignore #Autowired, transient and static properties from #Saga classes? I've attempted to manually define #JsonIgnore at non-state properties and it still didn't work.
2- Axon to safely configure XStream to ignore inner classes (mostly Builder classes implemented as private static final)?
Thanks in advance,
EDIT: I'm pursuing a resolution using my preferred serializer: JSON. I attempted to modify the saga class and extend JsonSerializer<AuctionEventManagerSaga>. For that I implemented the methods:
#Override
public Class<AuctionEventManagerSaga> handledType() {
return AuctionEventManagerSaga.class;
}
#Override
public void serialize(
final AuctionEventManagerSaga value,
final JsonGenerator gen,
final SerializerProvider serializers
) throws IOException {
gen.writeStartObject();
gen.writeObjectField("scheduleToken", value.eventScheduler);
gen.writeObjectField("auctionTimerStart", value.auctionTimerStart);
gen.writeEndObject();
}
Right now, I have something being serialized, but it has nothing to do with the properties I've defined:
2020-05-12 16:20:01.322 DEBUG 1 --- [mandProcessor-0] o.a.m.saga.repository.jpa.JpaSagaStore : Storing saga id c4b5d94c-7251-40a5-accf-332768b1cacd as {"delegatee":null,"unwrappingSerializer":false}
EDIT 2 Decided to add more insight into the issue I experience when I switch general to use XStream (even though it's somewhat unrelated to the main issue described in the title).
Here is the issue it complains to me:
2020-05-12 17:08:06.495 DEBUG 1 --- [ault-executor-0] o.a.a.c.command.AxonServerCommandBus : Received command response [message_identifier: "79631ffb-9a87-4224-bed3-a957730dced7"
error_code: "AXONIQ-4002"
error_message {
message: "No converter available\n---- Debugging information ----\nmessage : No converter available\ntype : jdk.internal.misc.InnocuousThread\nconverter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter\nmessage[1] : Unable to make field private static final jdk.internal.misc.Unsafe jdk.internal.misc.InnocuousThread.UNSAFE accessible: module java.base does not \"opens jdk.internal.misc\" to unnamed module #7728643a\n-------------------------------"
location: "1#600b5b87a922"
details: "No converter available\n---- Debugging information ----\nmessage : No converter available\ntype : jdk.internal.misc.InnocuousThread\nconverter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter\nmessage[1] : Unable to make field private static final jdk.internal.misc.Unsafe jdk.internal.misc.InnocuousThread.UNSAFE accessible: module java.base does not \"opens jdk.internal.misc\" to unnamed module #7728643a\n-------------------------------"
}
request_identifier: "2f7020b1-f655-4649-bbe0-d6f458b3c2f3"
]
2020-05-12 17:08:06.505 WARN 1 --- [ault-executor-0] o.a.c.gateway.DefaultCommandGateway : Command 'ACommandClassDispatchedFromSaga' resulted in org.axonframework.commandhandling.CommandExecutionException(No converter available
---- Debugging information ----
message : No converter available
type : jdk.internal.misc.InnocuousThread
converter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
message[1] : Unable to make field private static final jdk.internal.misc.Unsafe jdk.internal.misc.InnocuousThread.UNSAFE accessible: module java.base does not "opens jdk.internal.misc" to unnamed module #7728643a
-------------------------------)
Still no luck on resolving this...
I've worked on Axon systems where the only used Serializer implementation was the JacksonSerializer too. Mind you though, this is not what the Axon team recommends. For messages (i.e. commands, events and queries) it makes perfect sense to use JSON as the serialized format. But switching the general Serializer to jackson means you have to litter your domain logic (e.g. your Saga) with Jackson specifics "to make it work".
Regardless, backtracking to my successful use case of jackson-serialized-sagas. In this case we used the correct match of JSON annotations on the fields we desired to take into account (the actual state) and to ignore the one's we didn't want deserialized (with either transient or #JsonIgnore). Why both do not seem to work in your scenario is not entirely clear at this stage.
What I do recall is that the referenced project's team very clearly decided against Lombok due to "overall weirdnes" when it comes to de-/serialization. As a trial it thus might be worth to not use any Lombok annotations/logic in the Saga class and see if you can de-/serialize it correctly in such a state.
If it does work at that moment, I think you have found your culprit for diving in further search.
I know this isn't an exact answer, but I hope it helps you regardless!
Might be worthwhile to share the repository where this problems occurs in; might make the problem clearer for others too.
I was able to resolve the issue #2 when using XStream as general serializer.
One of the Sagas had an #Autowired dependency property that was not transient.
XStream was throwing some cryptic message, but we managed to track the problem and address it.
As for JSON support, we had no luck. We ended up switched everything to XStream for now, as the company only uses Java and it would be ok to decode the events using XStream.
Not the greatest solution, as we really wanted (and hoped) JSON would be supported properly out of the box. Mind you, this is in conjunction with using Lombok which caused for the nuisance in this case.

Maintain dependency object instance within the thread

Need help on below issue, I have below method:
public IHttpActionResult Test()
{
Task.Run(() => DoTheStuff())
Return Ok()
}
Note: Here I don't want to use async/await keyword, as I don't care about the result of DoTheStuff() method. I just need to open one thread and execute the code.
DoTheStuff() method refers the objects which are injected through dependency injection (Autofac). and in Module.Config I have registered all the required dependencies with lifetimescope.
Below issue I am facing call to Task.Run(() => DoTheStuff()) starts new thread executing DoTheStuff() method.
At the same time Test() method completes it execution with return Ok(), but DoTheStuff method is still running asynchronously.
With execution of Test() method, the registered dependencies gets disposed, and DoTheStuff() method throws below exception:
Nested lifetime cannot be created from the LifetimeScope as it has
already been disposed
Can someone please let me know how to maintain dependency object instance within the thread?
I can see a couple options.
Extract DoTheStuff into its own service. Register the services that you're injecting and using in DoTheStuff as InstancePerDependency so that the service gets its own instance. See https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html.
Add a Wait:
var t = Task.Run(() => DoTheStuff());
t.Wait();
return Ok();
Let me know if neither of those works.

Photon Networking Instantiate Error (Unity3d)

Ok, so I'm making an online FPS in Unity and I was scripting that Photon Networking Script to connect and spawn the player and I keep getting these two errors:
Assets/Resources/GameManager.cs(64,23): error CS1502: The best overloaded method match for `PhotonNetwork.Instantiate(string, UnityEngine.Vector3, UnityEngine.Quaternion, int)' has some invalid arguments
Assets/Resources/GameManager.cs(64,23): error CS1503: Argument `#1' cannot convert `UnityEngine.Transform' expression to type `string'
Here is where the error is in my code:
// When Connected [Photon Callback]
void OnJoinedRoom()
{
PhotonNetwork.Instantiate(playerPrefab, transform.position, Quaternion.identity, 0);
}
//In Game: Disconnect from room.
void InGameGUI()
{
if (GUILayout.Button("Leave Game"))
PhotonNetwork.LeaveRoom();
}
}
And I did reference the Transform at the top:
public Transform playerPrefab;
Any ideas on what I did wrong and how I could fix it. Please help!
PhotonNetwork.Instantiate requires a string, not a Transform object as it's first parameter. (I do believe this was changed from a Transform object a while ago). Simply name the prefab that you want to instantiate (which must be in the Resources folder).

Resources