System.NullPointerException:尝试取消引用空对象
我必须创建一个新对象并将所有字段从旧对象复制到新对象,这是机会的子对象。
copyfields 是一种将字段从一个对象复制到另一个对象的方法,克隆方法中的函数调用行是我收到异常的行。
public LQAgreementCloneCtrl(ApexPages.StandardController controller) {
lqa = [Select off1stdate__c, hosp1stdate__c, Zip_Code_New__c, X66_Contract__c,WAWF__c,,AccountRevenue__c
From LQ_Agreement__c Where id=:ApexPages.currentPage().getParameters().get('id')];
o = [Select of_Hospitals__c, X8_Gal__c, X4_Gal__c, X3mo_Avg_LBS_stop__c, X3_Gal__c,
X2_Gal__c, X1st_Pick_Up_Date__c, X17_Gal__c, X12_month_Actual_Stops__c,
X12_mo_Avg_Rev__c, Waste_Destruction_Date__c, WS_Other__c, Vision_Match__c,
Value_analysis_committee__c, AR_FuelFee__c, AR_FixerFee__c, AR_EnergyFee__c, APPROVALS__c,RecordType.Name
From Opportunity WHERE Id=:lqa.Opportunity__c];
}
public PageReference cloning(){
if(lqa.Status__c=='Deal Approved'){
//oclone=o;
//oclone.id=null;
oclone=o.clone();
insert oclone;
System.debug('Oclone>>>>>>>'+oclone);
LQ_Agreement__c lqaclone=new LQ_agreement__c();
//lqaclone=new LQ_Agreement__c();
lqaclone.Opportunity__c=oclone.Id;
System.debug('LQAClone>>>>>'+lqaclone);
lqaclone=copyfields(lqaclone,lqa);
oclone.Name=o.Name+'-Amended';
//Checking the Record type of the original Opportunity to create the new cloned Opp with RecordType of same waste stream + amendment added
if(o.RecordType.Name=='LQ Bio/SMS Renewal'|| o.RecordType.Name=='LQ Bio/SMS New Business' )
oclone.RecordType.Name='LQ BIO/SMS Amendment';
if(o.RecordType.Name=='LQ Haz Waste New Business'|| o.RecordType.Name=='LQ Haz Waste Renewal' )
oclone.RecordType.Name='LQ Haz Waste Amendment';
if(o.RecordType.Name=='LQ RMW New Business'|| o.RecordType.Name=='LQ RMW Renewal' )
oclone.RecordType.Name='LQ RMW Amendment';
if(o.RecordType.Name=='LQ Rx/Pharma New Business'|| o.RecordType.Name=='LQ Rx/Pharma Renewal' )
oclone.RecordType.Name='LQ Rx/Pharma Amendment';
//Checking the Record type of the original LQ Agreement to create the new cloned LQA with RecordType + amendment added
if(lqa.RecordType.Name=='LQ Existing Agreement' || lqa.RecordType.Name=='LQ New Agreement' )
lqaclone.RecordType.Name='LQ New Agreement – Amendment';
if(lqa.RecordType.Name=='LQ Existing Agreement GPO' || lqa.RecordType.Name=='LQ New Agreement GPO' )
lqaclone.RecordType.Name='LQ New Agreement GPO – Amendment';
insert lqaclone;
update oclone;
p=new ApexPages.StandardController(lqaclone).view();
}
else{
System.debug('Inside Else statement');
p=new ApexPages.StandardController(lqa).view();
}
return p;
}
public LQ_Agreement__c copyfields(LQ_Agreement__c lqaclone1,LQ_Agreement__c lqa1){
lqaclone1.Approved_By_RSD__c=lqa1.Approved_By_RSD__c;
lqaclone1.ApprovedByBrent__c=lqa1.ApprovedByBrent__c;
lqaclone1.ApprovedByJIM__c=lqa1.ApprovedByJIM__c;
lqaclone1.ApprovedByVP__c=lqa1.ApprovedByVP__c;
}
我在对复制字段的函数调用中遇到异常。我得到的异常是尝试取消引用空对象。复制字段是一个很大的功能。我只是给出了几行
I have to create a new object and copy all the fields form the old one to a new one and it is a child to opportunity.
The copyfields is a method that copies the fields from one object to another and the function call line in the cloning method is the line where I am getting the exception.
public LQAgreementCloneCtrl(ApexPages.StandardController controller) {
lqa = [Select off1stdate__c, hosp1stdate__c, Zip_Code_New__c, X66_Contract__c,WAWF__c,,AccountRevenue__c
From LQ_Agreement__c Where id=:ApexPages.currentPage().getParameters().get('id')];
o = [Select of_Hospitals__c, X8_Gal__c, X4_Gal__c, X3mo_Avg_LBS_stop__c, X3_Gal__c,
X2_Gal__c, X1st_Pick_Up_Date__c, X17_Gal__c, X12_month_Actual_Stops__c,
X12_mo_Avg_Rev__c, Waste_Destruction_Date__c, WS_Other__c, Vision_Match__c,
Value_analysis_committee__c, AR_FuelFee__c, AR_FixerFee__c, AR_EnergyFee__c, APPROVALS__c,RecordType.Name
From Opportunity WHERE Id=:lqa.Opportunity__c];
}
public PageReference cloning(){
if(lqa.Status__c=='Deal Approved'){
//oclone=o;
//oclone.id=null;
oclone=o.clone();
insert oclone;
System.debug('Oclone>>>>>>>'+oclone);
LQ_Agreement__c lqaclone=new LQ_agreement__c();
//lqaclone=new LQ_Agreement__c();
lqaclone.Opportunity__c=oclone.Id;
System.debug('LQAClone>>>>>'+lqaclone);
lqaclone=copyfields(lqaclone,lqa);
oclone.Name=o.Name+'-Amended';
//Checking the Record type of the original Opportunity to create the new cloned Opp with RecordType of same waste stream + amendment added
if(o.RecordType.Name=='LQ Bio/SMS Renewal'|| o.RecordType.Name=='LQ Bio/SMS New Business' )
oclone.RecordType.Name='LQ BIO/SMS Amendment';
if(o.RecordType.Name=='LQ Haz Waste New Business'|| o.RecordType.Name=='LQ Haz Waste Renewal' )
oclone.RecordType.Name='LQ Haz Waste Amendment';
if(o.RecordType.Name=='LQ RMW New Business'|| o.RecordType.Name=='LQ RMW Renewal' )
oclone.RecordType.Name='LQ RMW Amendment';
if(o.RecordType.Name=='LQ Rx/Pharma New Business'|| o.RecordType.Name=='LQ Rx/Pharma Renewal' )
oclone.RecordType.Name='LQ Rx/Pharma Amendment';
//Checking the Record type of the original LQ Agreement to create the new cloned LQA with RecordType + amendment added
if(lqa.RecordType.Name=='LQ Existing Agreement' || lqa.RecordType.Name=='LQ New Agreement' )
lqaclone.RecordType.Name='LQ New Agreement – Amendment';
if(lqa.RecordType.Name=='LQ Existing Agreement GPO' || lqa.RecordType.Name=='LQ New Agreement GPO' )
lqaclone.RecordType.Name='LQ New Agreement GPO – Amendment';
insert lqaclone;
update oclone;
p=new ApexPages.StandardController(lqaclone).view();
}
else{
System.debug('Inside Else statement');
p=new ApexPages.StandardController(lqa).view();
}
return p;
}
public LQ_Agreement__c copyfields(LQ_Agreement__c lqaclone1,LQ_Agreement__c lqa1){
lqaclone1.Approved_By_RSD__c=lqa1.Approved_By_RSD__c;
lqaclone1.ApprovedByBrent__c=lqa1.ApprovedByBrent__c;
lqaclone1.ApprovedByJIM__c=lqa1.ApprovedByJIM__c;
lqaclone1.ApprovedByVP__c=lqa1.ApprovedByVP__c;
}
I am getting the exception at the function call to copyfields. The exception I get is Attempt to de-reference null object. Copy fields is a big function. I just gave a few lines
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误发生在copyfield之后,我的猜测是您的
oclon.recordType
子对象未定义,clone
本身不会复制它,它只能来自SOQL结果。此外,您不能分配 oclone.recordType.name,要分配要克隆的记录类型,您必须将正确的 RecordType Id 分配给oclone.RecordTypeId
字段。Error is after copyfield, my guess is that your
oclone.recordType
sub-object is not defined,clone
on its own does not replicate it, it can only come from SOQL result. Also you cannot assign oclone.recordType.name, to assign record type to clone you must assign proper RecordType Id tooclone.RecordTypeId
field.不知道为什么仅使用标准 SObject.clone(false, true) 方法是不够的(false - 不保留 Id 和 true - 制作真实的副本,而不仅仅是引用)。同样,可以使用 List.deepClone(false) 真正克隆 SObject 列表,而不是编写自己的克隆/复制逻辑。
一般来说,我会考虑清理您的逻辑,以便您可以更清楚地看到代码流程,特别是在变量应具有的状态和值方面。例如,从您的代码来看:
假设 copyfields 返回一个 new SObject,似乎以下内容是有意的:
但同样,以下内容对我来说似乎是正确的:
除非我错过了您更喜欢滚动的其他原因你自己的克隆。
Not sure why just using the standard SObject.clone(false, true) method wouldn't suffice (false - don't preserve the Id and true - make a true copy, not just a reference). A list of SObjects can similarly be truly cloned with List.deepClone(false) instead of crafting your own clone/copy logic.
In general I'd consider cleaning up your logic so that you can see the flow of your code more clearly, especially in terms of what state and values your variables should have. For example from your code:
It seems the following was intended, assuming copyfields returned a new SObject:
But again, the following seems correct to me:
unless I'm missing some other reason that you preferred to roll your own clone.