Flink Error: is not serializable. The object probably contains or references non serializable fields.

Today, a colleague suddenly reported such an error. At first, he really didn’t react. Member variables can’t be serialized….

Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: java.lang.ref.ReferenceQueue$Lock@11fc564b is not serializable. The object probably contains or references non serializable fields.
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:151)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:126)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:126)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:126)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:126)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:126)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:71)
	at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.clean(StreamExecutionEnvironment.java:1821)
	at org.apache.flink.streaming.api.datastream.DataStream.clean(DataStream.java:188)
	at org.apache.flink.streaming.api.datastream.KeyedStream.process(KeyedStream.java:398)
	at org.apache.flink.streaming.api.datastream.KeyedStream.process(KeyedStream.java:374)
	at com.xintujing.flinkdemo.text.UserCount_3.main(UserCount_3.java:53)
Caused by: java.io.NotSerializableException: java.lang.ref.ReferenceQueue$Lock
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at org.apache.flink.util.InstantiationUtil.serializeObject(InstantiationUtil.java:586)
	at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:133)
	... 11 more

When you see this error, you are in an ignorant state and cannot be serialized. What do you mean?

First, analyze the problem. He tells us that a class cannot be serialized,

What happens if a member in a class does not implement a serializable interface?A simple question about the Java serialization process. If you try to serialize an object that implements a serializable class, but the object contains a reference to a non serializable class, a non serializable exception notserializableexception will be thrown at run time,

Well, if you take an object as a member variable of another object, all member variables of the object must be serializable. If the member variables cannot be serialized, this error will be reported. So. Don’t treat non serializable things as member variables.

Read More: