gpt4 book ai didi

Java构造函数NullPointerException问题

转载 作者:行者123 更新时间:2023-12-02 00:37:34 25 4
gpt4 key购买 nike

我在理解如何创建子类的构造函数(它扩展了父类)时遇到了一些问题。这是我正在使用的代码:

父类构造函数:

public RPCPacket(   int apacketId,
RPCPacketType apacketType,
RPCOperationType aoperationType,
String tmpPacket_objectOid,
int aobjectId,
int adataSize,
String tmpPacket_dataHash,
RPCPacketDataType adataType,
byte[] apacketData){


RPCPacket packet=null;

switch(apacketType){
case ST_OBJECT_TYPE_INFO_START:
{
packet = new InfoStartRPCPacket();
break;
}
case ST_OBJECT_TYPE_INFO_END:
{
packet = new InfoEndRPCPacket();

break;
}
case ST_OBJECT_TYPE_INFO_ERROR:
{
packet = new InfoErrorRPCPacket();

break;
}

// Basic packets
case ST_OBJECT_TYPE_COLLECTION:
{
packet = new CollectionRPCPacket();

break;
}
case ST_OBJECT_TYPE_CATEGORY:
{
packet = new CategoryRPCPacket();

break;
}
case ST_OBJECT_TYPE_CARD:
{
packet = new CardRPCPacket();

break;
}
case ST_OBJECT_TYPE_MESSAGE:
{
packet = new MessageRPCPacket();

break;
}
case ST_OBJECT_TYPE_GENRE:
{
packet = new GenreRPCPacket();

break;
}
case ST_OBJECT_TYPE_TAG:
{
packet = new TagRPCPacket();

break;
}
case ST_OBJECT_TYPE_USER:
{
packet = new UserRPCPacket();

break;
}
case ST_OBJECT_TYPE_CARDSTATS_CATEGORY:
{
packet = new CardStatsCategoryRPCPacket();

break;
}
case ST_OBJECT_TYPE_CONTENT:
{
packet = new ContentRPCPacket();

break;
}

// Media packets
case ST_OBJECT_TYPE_MEDIA_COLLECTION:
{
packet = new MediaCollectionRPCPacket();

break;
}
case ST_OBJECT_TYPE_MEDIA_CATEGORY:
{
packet = new MediaCategoryRPCPacket();

break;
}
case ST_OBJECT_TYPE_MEDIA_CARD:
{
packet = new MediaCardRPCPacket();

break;
}
case ST_OBJECT_TYPE_MEDIA_TAG:
{
packet = new MediaTagRPCPacket();

break;
}
case ST_OBJECT_TYPE_MEDIA_COLLECTION_BUTTON:
{
packet = new MediaCollectionButtonRPCPacket();

break;
}

// unknown packet
default:
{
packet=null;

break;
}
}

}

子类构造函数:

static int apacketId;
static RPCPacketType apacketType;
static RPCOperationType aoperationType;
static String tmpPacket_objectOid;
static int aobjectId;
static int adataSize;
static String tmpPacket_dataHash;
static RPCPacketDataType adataType;
static byte[] apacketData;

public InfoStartRPCPacket() {
super(apacketId, apacketType, aoperationType, tmpPacket_objectOid, aobjectId,
adataSize, tmpPacket_dataHash, adataType, apacketData);//line 46 from LogCat error
}

以及 LogCat 异常:

09-08 11:18:05.614: WARN/System.err(3376): java.lang.NullPointerException
09-08 11:18:05.614: WARN/System.err(3376): at com.stampii.stampii.comm.rpc.RPCPacket.<init>(RPCPacket.java:68)
09-08 11:18:05.614: WARN/System.err(3376): at com.stampii.stampii.comm.rpc.InfoStartRPCPacket.<init>(InfoStartRPCPacket.java:46)
09-08 11:18:05.614: WARN/System.err(3376): at com.stampii.stampii.comm.rpc.RPCPacket.<init>(RPCPacket.java:71)
09-08 11:18:05.614: WARN/System.err(3376): at com.stampii.stampii.user.UserLogin$2.onClick(UserLogin.java:122)
09-08 11:18:05.614: WARN/System.err(3376): at android.view.View.performClick(View.java:2408)
09-08 11:18:05.614: WARN/System.err(3376): at android.view.View$PerformClick.run(View.java:8817)
09-08 11:18:05.614: WARN/System.err(3376): at android.os.Handler.handleCallback(Handler.java:587)
09-08 11:18:05.614: WARN/System.err(3376): at android.os.Handler.dispatchMessage(Handler.java:92)
09-08 11:18:05.614: WARN/System.err(3376): at android.os.Looper.loop(Looper.java:144)
09-08 11:18:05.614: WARN/System.err(3376): at android.app.ActivityThread.main(ActivityThread.java:4937)
09-08 11:18:05.614: WARN/System.err(3376): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 11:18:05.614: WARN/System.err(3376): at java.lang.reflect.Method.invoke(Method.java:521)
09-08 11:18:05.614: WARN/System.err(3376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-08 11:18:05.614: WARN/System.err(3376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-08 11:18:05.624: WARN/System.err(3376): at dalvik.system.NativeStart.main(Native Method)

有什么建议可以解决我的问题吗?

最佳答案

您收到 NullPointerException 是因为(我假设)您没有实例化您在构造函数中使用的任何静态变量。您需要在调用 InfoStartRPCPacket() 之前传入这些参数的值。

但是,这对我来说看起来像是一个糟糕的面向对象的情况,除非您非常具体地需要在子类中使用静态变量,否则我建议您的子构造函数应该采用参数父级的构造函数,这样您就可以在子级中优雅地使用 super(...) 构造函数。就像这样:

InfoStartRPCPacket(
int apacketId,
RPCPacketType apacketType,
RPCOperationType aoperationType,
String tmpPacket_objectOid,
int aobjectId,
int adataSize,
String tmpPacket_dataHash,
RPCPacketDataType adataType,
byte[] apacketData) {
super(apacketId, apacketType, aoperationType, tmpPacket_objectOid, aobjectId,
adataSize, tmpPacket_dataHash, adataType, apacketData);
//your custom constructor code here
}

这样,在调用构造函数之前,没有人需要知道实例化所有静态类型变量(一种称为顺序耦合的反模式)。

更好的是,如果您在 InfoStartRPCPacket 中没有其他工作需要完成,则完全省略子构造函数。它将简单地继承父级的构造函数。

关于Java构造函数NullPointerException问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com