+ Start a Discussion
VidhiVidhi 

Attempt to deference a null object

Hello Friends ,

 

I am trying to insert quote line item list but it is giving error.I am not able to understand why i am getting this error.

ublic class CLS_NewQuoteLineItems {
public string surl='';
public string name{get;set;}
public id qid{get;set;}
public boolean b{get;set;}
public boolean b1{get;set;}
public List<WrapperProduct> prodlist ;
public List<WrapperProduct> selectprodlist {get;set;}
public list<product2> plist{get;set;}
public list<product2> plists{get;set;}
public quote q;
public list<Quotelineitem> qlist{get;set;}
public CLS_NewQuoteLineItems(){
b=true;
b1=false;
qlist=new list<Quotelineitem>();
surl = ApexPages.currentPage().getUrl();
if(surl.contains('0Q0'))
{
q=new quote();
qid=ApexPages.currentPage().getParameters().get('id');
q=[select name from quote where id=:qid];
name=q.name;
}
plist=new list<product2>();
prodlist=new List<WrapperProduct>();
selectprodlist=new List<WrapperProduct>();
plist=[select name,ProductCode ,Family,Description from product2];
system.debug('ppppppppppppp'+plist);

 

}
public List<WrapperProduct> getprodlist () {

integer i = 0;


for(product2 p: plist)
{


WrapperProduct lw = new WrapperProduct(p);
prodlist.add(lw);
i++;
}
system.debug('222222222222'+prodlist);
return prodlist;


}

public pageReference selectproduct()
{
b=false;
b1=true;
plists=new list<product2>();
for (WrapperProduct wp : prodlist)
{
if (wp.checked)
{
plists.add(wp.pro);
selectprodlist.add(wp);
}
}

System.debug('@@@@@@@@@@@@'+ selectprodlist);
return null;
}

public pageReference CancelRec()
{

pagereference pr = new pagereference('/apex/VF_QuoteDetailPage?id='+qid);
return pr;
}
public pageReference insertQuoteItem()
{

for(product2 p:plists)
{
Quotelineitem q=new Quotelineitem();
q.pricebookentry.product2id='01tA0000003IUkaIAG';//Here i am getting error.
q.QuoteId=qid;
q.Quantity=1;
q.Description='test';
qlist.add(q);

}
System.debug('uuuuuuuuuuuuuuu'+ qlist);
insert qlist;
pagereference pr = new pagereference('/apex/VF_QuoteDetailPage?id='+qid);
return pr;
}

public class WrapperProduct
{
public boolean checked {get;set;}
public string productName{get;set;}
public string productcode{get;set;}
public Decimal listprice{get;set;}
public string productdescription{get;set;}
public string productfamily{get;set;}
public id prodid{get;set;}
public product2 pro{get;set;}

public WrapperProduct()
{
checked = false;

}

public WrapperProduct(product2 p)
{
pro=p;
prodid=p.id;
productName =p.name;
productcode=p.ProductCode;
productdescription=p.Description;
productfamily = p.Family;
}
}
}

 

Urgent reply will be helpfull.

 

Thanks

Jeff MayJeff May

The error means that you are trying to get a field or do calculation on a field for an object that has not been set to an actual record.

VidhiVidhi
how can i correct this?
Parth_SevakParth_Sevak
first instantiate pricebookentry, then relate to to opportunitylineitem,
just for eg,
pricebookentry pEntry - new pricebookentry();
product2 p2 = [Select Id from product2id where Id = '01tA0000003IUkaIAG'];
pEntry.product2Id = p2.Id;
q.pricebookentry = pEntry;
VidhiVidhi
Now I am getting this error

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Entry ID: id value of incorrect type: 01tA0000002dsqXIAQ: [PricebookEntryId]
Error is in expression '{!insertQuoteItem}' in component <apex:page> in page vf_newquotelineitems
Parth_SevakParth_Sevak
Do Soql to get pricebookentry ,
pricebookentry = [Select Id from pricebookentry where Product2Id = p2.Id limit 1];