How can I identify an anonymous inner class in a NotSerializableException

大兔子大兔子 提交于 2019-12-17 14:48:40

问题


I have received the following error message when trying to debug an application in NetBeans:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: board.Board$1

In the course of debugging I have had to insert 'implements Serializable' in a number of classes as the exception arose in the course of reading from a file that stores a large object. This has not been difficult as the class needing attention has been clear from the exception message. What has thrown me is the apparent anonymous inner class 'Board$1'. I can't for the life of me identify the source with 'Board' that is causing the problem. How can I do this?

As it's a question of debugging practice rather than the specifics of the code (I think) I haven't included it, but I can easily add that in if it helps.


回答1:


Board$1 is the first anonymous class encountered in Board.java. For example:

class Board {
  public static void main(String[] args) {
    new Object() {}; // Board$1
    new Object() {}; // Board$2
  }
}

With an IDE like Eclipse, it's easy to spot those anonymous classes in the outline view. I'm sure NetBeans has similar views:




回答2:


In IntelliJ (and Android Studio) one can Navigate->Class (⌘-O on mac) and paste in Board$1, and it will take you to the code for that inner class.



来源:https://stackoverflow.com/questions/12932079/how-can-i-identify-an-anonymous-inner-class-in-a-notserializableexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!