You need to sign in to do that
Don't have an account?
Amarjeet
Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Apex Test Result Detail
Time Started1/2/2018 12:15 AM
Classaccsearchcontrollertest
Method NameTestclear
Pass/FailFail
Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.accsearchController.clear: line 25, column 1
Class.accsearchcontrollertest.Testclear: line 49, column 1
Apex Test Class:-
@isTest
public class accsearchcontrollertest
{
@testSetup static void createTestData()
{
Account acc= new Account();
acc.Name='TestAccount';
acc.Is_Acct_IP_or_RP__c='IP';
acc.Buying_Groups__c='N/A (none)';
insert acc;
Opportunity op = new Opportunity ();
op.Name = 'Test' ;
op.Accountid= acc.Id;
op.StageName='A = Market Participant';
op.CloseDate= System.today() + 5;
insert op;
Quote qt = new Quote();
qt.Name = 'Test123';
qt.CBG_Employee_Responsible__c = UserInfo.getUserId();
qt.CBG_Incoterms__c = 'CFR';
qt.CBG_PaymentTerms__c = 'CO04 payable immediatly';
qt.Format__c = 'List';
qt.CBG_SAPQuoteNumber__c = '30010012';
qt.OpportunityId=op.id;
insert qt;
}
static TestMethod void TestprocessLinkClick()
{
PageReference pref1 = Page.CBG_SearchQuote ;
Test.setCurrentPage(pref1);
accsearchcontroller Obj = new accsearchcontroller();
Obj.searchstring = '30010012';
//Obj.clear();
Obj.processLinkClick();
}
static TestMethod void Testclear()//////////////////////////////////This one failing---->How to pass value to search string
{
accsearchcontroller Obj = new accsearchcontroller();
Obj.searchstring = '30010012';
Obj.clear();
}
static TestMethod void Testsearch()
{
accsearchcontroller Obj = new accsearchcontroller();
Obj.search();
}
}
Apex Class:-
public with sharing class accsearchController
{
public accsearchcontroller(ApexPages.StandardController controller) {
}
public String l { get; set; }
public list <quote> acc {get;set;}
public string searchstring {get;set;}
public String quoteid{get; set;}
public Boolean search{get; set;}
public Boolean result{get; set;}
public accsearchcontroller( )
{
}
public void search(){
string searchquery='select QuoteNumber,Name,Email,Opportunity.Name,CBG_SAPQuoteNumber__c ,CBG_Phoenix_Offer_Number__c from quote where CBG_SAPQuoteNumber__c like \'%'+searchstring+'%\' OR CBG_SAPQuoteNumber__c like \'*'+searchstring+'*\' OR CBG_Phoenix_Offer_Number__c like \'%'+searchstring+'%\' OR CBG_Phoenix_Offer_Number__c like \'*'+searchstring+'*\' Limit 20';
acc= Database.query(searchquery);
}
public void clear(){
acc.clear();
}
public PageReference processLinkClick() {
search = false;
result = false;
Pagereference p1 = new PageReference('https://cs84.salesforce.com/'+quoteid);
p1.setRedirect(true);
return p1;
}
}
Time Started1/2/2018 12:15 AM
Classaccsearchcontrollertest
Method NameTestclear
Pass/FailFail
Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.accsearchController.clear: line 25, column 1
Class.accsearchcontrollertest.Testclear: line 49, column 1
Apex Test Class:-
@isTest
public class accsearchcontrollertest
{
@testSetup static void createTestData()
{
Account acc= new Account();
acc.Name='TestAccount';
acc.Is_Acct_IP_or_RP__c='IP';
acc.Buying_Groups__c='N/A (none)';
insert acc;
Opportunity op = new Opportunity ();
op.Name = 'Test' ;
op.Accountid= acc.Id;
op.StageName='A = Market Participant';
op.CloseDate= System.today() + 5;
insert op;
Quote qt = new Quote();
qt.Name = 'Test123';
qt.CBG_Employee_Responsible__c = UserInfo.getUserId();
qt.CBG_Incoterms__c = 'CFR';
qt.CBG_PaymentTerms__c = 'CO04 payable immediatly';
qt.Format__c = 'List';
qt.CBG_SAPQuoteNumber__c = '30010012';
qt.OpportunityId=op.id;
insert qt;
}
static TestMethod void TestprocessLinkClick()
{
PageReference pref1 = Page.CBG_SearchQuote ;
Test.setCurrentPage(pref1);
accsearchcontroller Obj = new accsearchcontroller();
Obj.searchstring = '30010012';
//Obj.clear();
Obj.processLinkClick();
}
static TestMethod void Testclear()//////////////////////////////////This one failing---->How to pass value to search string
{
accsearchcontroller Obj = new accsearchcontroller();
Obj.searchstring = '30010012';
Obj.clear();
}
static TestMethod void Testsearch()
{
accsearchcontroller Obj = new accsearchcontroller();
Obj.search();
}
}
Apex Class:-
public with sharing class accsearchController
{
public accsearchcontroller(ApexPages.StandardController controller) {
}
public String l { get; set; }
public list <quote> acc {get;set;}
public string searchstring {get;set;}
public String quoteid{get; set;}
public Boolean search{get; set;}
public Boolean result{get; set;}
public accsearchcontroller( )
{
}
public void search(){
string searchquery='select QuoteNumber,Name,Email,Opportunity.Name,CBG_SAPQuoteNumber__c ,CBG_Phoenix_Offer_Number__c from quote where CBG_SAPQuoteNumber__c like \'%'+searchstring+'%\' OR CBG_SAPQuoteNumber__c like \'*'+searchstring+'*\' OR CBG_Phoenix_Offer_Number__c like \'%'+searchstring+'%\' OR CBG_Phoenix_Offer_Number__c like \'*'+searchstring+'*\' Limit 20';
acc= Database.query(searchquery);
}
public void clear(){
acc.clear();
}
public PageReference processLinkClick() {
search = false;
result = false;
Pagereference p1 = new PageReference('https://cs84.salesforce.com/'+quoteid);
p1.setRedirect(true);
return p1;
}
}
Your test method Testclear() is failing because, the acc variable that is attempted to be cleared in the clear() method, is already null. Hence, you will either need to initialize the acc variable in your constructor or first call the search() method to initialize the acc variable with a value and then call the clear() method.
Refer the sample test code to do so.
Moreover, the methodname clear, is already a standard Sobject method (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm) and a part of the reserved keywords. It may not be directly affecting your code here, but its always a good idea to refrain from using any reserved keywords as method names.
Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
All Answers
Your test method Testclear() is failing because, the acc variable that is attempted to be cleared in the clear() method, is already null. Hence, you will either need to initialize the acc variable in your constructor or first call the search() method to initialize the acc variable with a value and then call the clear() method.
Refer the sample test code to do so.
Moreover, the methodname clear, is already a standard Sobject method (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm) and a part of the reserved keywords. It may not be directly affecting your code here, but its always a good idea to refrain from using any reserved keywords as method names.
Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.