How can I identify an anonymous inner class in a NotSerializableException
Asked Answered
M

2

6

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.

Maiolica answered 17/10, 2012 at 10:17 Comment(0)
C
5

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:

enter image description here

Cloudcapped answered 17/10, 2012 at 10:24 Comment(2)
I copied my files to Eclipse and used Package Explorer to dig into Board but found nothing. But based on the signature you used, new Object() {}, I found new MouseAdapter() {...}, which didn't appear to show up in Eclipse or NetBeans. I inserted transient to prevent its being written to file. That seems to work, but now I have another unrelated bug... I'll work on that now, assuming I have solved this one.Maiolica
OK - the other unrelated bug is fixed as is the original problem. Obvious in hindsight. Many thanks, Lukas. Now I'm wondering if a bit of suspect code on my part is the reason for the MouseAdapter not showing up, but that's another issue.Maiolica
D
4

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.

Danielledaniels answered 8/12, 2015 at 0:22 Comment(1)
checked Netbeans 8 and couldn't figure out a way to show anonymous inner classes in Navigator panel or navigate to it like IntelliJ =/Danielledaniels

© 2022 - 2024 — McMap. All rights reserved.