如何调试错误(没有有效的构造函数)?
Posted
技术标签:
【中文标题】如何调试错误(没有有效的构造函数)?【英文标题】:How to debug Error(no valid constructor)? 【发布时间】:2017-03-28 20:57:08 【问题描述】:在我使用 writeobject 之前没有错误,但它一直给我没有有效的构造函数错误,即使我创建了无参数构造函数和 UID,我的编码有什么问题?
这是控制台中的错误代码
java.io.InvalidClassException: AirSwift.Game_player$Player; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at AirSwift.Game_stage.GameLoad(Game_stage.java:168)
at AirSwift.Game_main$7.actionPerformed(Game_main.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是一个实现 Externalizable 的类
public class Player implements Externalizable
/**
*
*/
private static final long serialVersionUID = 1L;
public int x, y, mode, attackspeed, speed, speedinattack, firecount, hp, mp, time;
public boolean modechanging = false;
public int modechangecount = 0;
Image Player_img = new ImageIcon("Images/Player/Player_0.png").getImage();
Image []Player_Mode_img;
String sound_fire, sound_change;
public Player()
public Player(int hp)
this.ChangeMode(1);
this.hp = hp;
this.mp = 100;
this.x = 100;
this.y = 600;
this.time = 0;
Image AnimeMode()
switch(mode)
case 1:
if(modechangecount < 10) return Player_Mode_img[0];
else if((9<modechangecount)&&(modechangecount<20)) return Player_Mode_img[1];
else return Player_Mode_img[2];
case 2:
return Player_Mode_img[0];
case 3:
if(modechangecount < 10) return Player_Mode_img[0];
else if((9<modechangecount)&&(modechangecount<20)) return Player_Mode_img[1];
else return Player_Mode_img[2];
default: return default_img;
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
// TODO Auto-generated method stub
this.x = in.readInt(); this.y = in.readInt(); this.hp = in.readInt(); this.time = in.readInt();
public void writeExternal(ObjectOutput out) throws IOException
// TODO Auto-generated method stub
out.writeInt(this.x); out.writeInt(this.y); out.writeInt(this.hp); out.writeInt(this.time);
【问题讨论】:
【参考方案1】:将默认构造函数也添加到AirSwift.Game_player
或将Player
声明为public static
【讨论】:
以上是关于如何调试错误(没有有效的构造函数)?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:如何在构造函数中传递值,以便我可以重用我的小部件?