Getting java.io.InvalidClassException (no valid constructor) with case objects

2,079 views
Skip to first unread message

wookietreiber

unread,
Dec 11, 2012, 5:00:03 AM12/11/12
to akka...@googlegroups.com
Hello,

I have the following case objects:

  package ckit

  import java.awt.Color

  sealed abstract class StateCategory(val color: Color, val light: Color)

  case object Deleted   extends StateCategory(Color.ORANGE, new Color(255, 225, 140))
  case object Error     extends StateCategory(Color.RED,    new Color(255, 200, 200))
  case object Pending   extends StateCategory(Color.BLUE,   new Color(200, 200, 255))
  case object Running   extends StateCategory(Color.GREEN,  new Color(200, 255, 200))
  case object Suspended extends StateCategory(Color.YELLOW, new Color(255, 255, 200))
  case object Unknown   extends StateCategory(Color.GRAY,   new Color(200, 200, 200))

I want to send these as parts of other messages with akka, but I get InvalidClassException (IP addresses omitted):

[ERROR] [12/11/2012 10:15:18.856] [ckit-4] [NettyRemoteTransport(akka://ckit@...:2552)] RemoteClientError@akka://ckit@...:2552: Error[
java.io.InvalidClassException: ckit.Running$; no valid constructor
        at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:147)
        at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:755)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
        at scala.collection.immutable.$colon$colon.readObject(List.scala:361)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
        at akka.serialization.JavaSerializer$$anonfun$1.apply(Serializer.scala:120)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:57)
        at akka.serialization.JavaSerializer.fromBinary(Serializer.scala:120)
        at akka.serialization.Serialization$$anonfun$deserialize$1.apply(Serialization.scala:67)
        at scala.util.Try$.apply(Try.scala:161)
        at akka.serialization.Serialization.deserialize(Serialization.scala:67)
        at akka.remote.MessageSerializer$.deserialize(MessageSerializer.scala:23)
        at akka.remote.RemoteMessage.payload$lzycompute(RemoteTransport.scala:338)
        at akka.remote.RemoteMessage.payload(RemoteTransport.scala:338)
        at akka.remote.RemoteTransport.receiveMessage(RemoteTransport.scala:284)
        at akka.remote.netty.ActiveRemoteClientHandler.messageReceived(Client.scala:270)
        at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:95)
        at org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler.handleUpstream(IdleStateAwareChannelHandler.java:43)
        at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:565)
        at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:793)
        at org.jboss.netty.handler.execution.ChannelUpstreamEventRunnable.doRun(ChannelUpstreamEventRunnable.java:45)
        at org.jboss.netty.handler.execution.ChannelEventRunnable.run(ChannelEventRunnable.java:69)
        at org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor$ChildExecutor.run(OrderedMemoryAwareThreadPoolExecutor.java:315)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)

What can I do to fix this? Is this akka-serialization related or is this a general problem of case objects?


Best Regards

Roland Kuhn

unread,
Dec 11, 2012, 5:58:33 AM12/11/12
to akka...@googlegroups.com
Hi,

case objects are still objects (i.e. not static), so they will be serialized. Are you sure that java.awt.Color is serializable? If not you should register your own serializer for StateCategory or implement writeReplace (e.g. something wrapping a string).

Regards,

Roland

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user?hl=en.
 
 



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn

Rodrigo Boavida

unread,
Sep 15, 2014, 8:43:32 AM9/15/14
to akka...@googlegroups.com
Hi, 

I had similiar problem with case classes which also derive from an abstract one. Did you get around it?

tnks.
Rod

√iktor Ҡlang

unread,
Sep 15, 2014, 9:00:13 AM9/15/14
to Akka User List
Tried making StateCategory extends Serializable?


>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.

To post to this group, send email to akka...@googlegroups.com.



--
Cheers,

Patrik Nordwall

unread,
Sep 16, 2014, 4:13:00 AM9/16/14
to akka...@googlegroups.com
You can solve this by adding the needed default constructor in StateCategory:

sealed abstract class StateCategory(val color: Color, val light: Color) {
  // needed for serialization
  def this() = this(Color.BLACK, Color.BLACK)
}

Cheers,
Patrik

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Rodrigo Boavida

unread,
Sep 17, 2014, 7:15:04 AM9/17/14
to akka...@googlegroups.com
That was exactly the solution I found. Tnks ayway for pointing out! Confirms I found the right solution. Still getting up to speed on Scala, its a whole different world from C# and .NET...:)

Cheers,
Rod

You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/3Q_qid0NnjU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages