Java Gson在未使用的字段上运行IllegalArgumentException

Java Gson在未使用的字段上运行IllegalArgumentException,java,swing,gson,applet,illegalargumentexception,Java,Swing,Gson,Applet,Illegalargumentexception,我试图在Swing用户界面中使用GSON,当我试图创建GSON对象时,考虑到java.applet.appletclass,我得到一个错误。奇怪的是,我甚至没有使用GSON使用Applet元素来保存或加载 有人能给我解释一下发生了什么事吗 代码如下 我遇到的错误 java.lang.IllegalArgumentException: class java.applet.Applet declares multiple JSON fields named accessibleContext 点击

我试图在Swing用户界面中使用GSON,当我试图创建GSON对象时,考虑到
java.applet.applet
class,我得到一个错误。奇怪的是,我甚至没有使用GSON使用
Applet
元素来保存或加载

有人能给我解释一下发生了什么事吗

代码如下

我遇到的错误

java.lang.IllegalArgumentException: class java.applet.Applet declares multiple JSON fields named accessibleContext
点击按钮将对象加载到json:

JButton selectProfileSaveLocationButton = new JButton("Select Location");
    selectProfileSaveLocationButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //todo create new empty profile
            JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            int returnValue = jfc.showSaveDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                if (jfc.getSelectedFile().isDirectory()) {
                    profileSaveLocationTextfield.setText(jfc.getSelectedFile().getAbsolutePath());
                    try {
                        System.out.println("Saving file to: " + jfc.getSelectedFile().getAbsolutePath() + File.separatorChar + profileNameTextfield.getText() + ".json");
                        File dataFile = new File(jfc.getSelectedFile().getAbsolutePath() + File.separatorChar + profileNameTextfield.getText() + ".json");
                        //dataFile.createNewFile();
                        Data.botProfileLocation = dataFile;


                        SlayerProfile profile = newEmptyProfile(profileNameTextfield.getText());
                        String json = FryslanSlayer.gson.toJson(profile);

                        System.out.println(json);

                        FileWriter writer = new FileWriter(Data.botProfileLocation.getAbsolutePath());
                        FryslanSlayer.gson.toJson(profile, writer);

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
});
selectProfileSaveLocationButton.setBackground(new Color(65, 105, 225));
selectProfileSaveLocationButton.setForeground(new Color(0, 0, 139));
selectProfileSaveLocationButton.setBounds(610, 274, 124, 25);
generalPanel.add(selectProfileSaveLocationButton);

我能估计的唯一解释是SlayerProfile.class要么扩展了Applet,要么包含了一个扩展Applet的字段

GSON无法处理在类层次结构的多个级别上具有相同字段名的类,小程序类就是这样。有关更多详细信息,请参阅

如果不分享你对SlayerProfile的定义,很难确定这就是问题所在


您的问题中我缺少的另一件事是如何设置GSON对象的定义。此问题可能与GSON对象的错误配置有关。

您遇到此问题是因为您的类
SlayerProfile
具有类型为
Applet
或在某个点引用
Applet
的属性(可能有很多层次)。这肯定不是由于继承,因为
Gson
调用
Object#getClass
来获取该错误中显示的类型,因此它将显示实现类

Gson无法反序列化
Applet
的原因是它使用属性名将对象转换为JSON。JSON没有Java所具有的层次结构概念,因此您无法创建具有这种模糊性的JSON,这种模糊性是有意义的,并且可以序列化回同一个对象。更多关于:

一些Json库使用类型的getter来推断Json元素。我们选择使用所有非瞬时、静态或合成的字段(继承层次结构上)。我们这样做是因为并非所有类都使用适当命名的getter编写。此外,getXXX或isXXX可能是语义的,而不是指示属性


对于您提供的数据,没有最好的解决方案。根据您的需要,您可能希望使
小程序
引用暂时化,创建一个自定义的或其他对您的情况更合理的东西。

是您的
SlayerProfile
类,在
小程序
的子类中定义,没有
static
关键字?如果是这样,它隐式地引用了
Applet
子类,因此
Gson
也尝试序列化该类,并且您需要将
static
关键字添加到
SlayerProfile

Gson在序列化时查看对象的私有字段,这意味着包含了超类的所有私有字段,当您有同名字段时,它会抛出一个错误

如果您不想包含任何特定字段,则必须使用
transient
关键字对其进行标记,例如:

protected transient DontSerializeMeClass dontSerializeMeClass;
在本例中,您可以通过将accessibleContext字段添加到Slayer来尝试隐藏该字段。我不确定您的代码的其余部分,因为我只能看到它的一小部分,但我认为它要么继承了Applet,要么声明了一个来自该家族树的变量

与此主题相关的文章可在此处找到:

请参阅这篇SO文章,这篇文章让我们明白了您所看到的错误

要更快获得更好的帮助,请添加或。硬编码GSON数据。
private static transient AccesssibleContext accessibleContext = null;