+ Start a Discussion
deepakMdeepakM 

System.NullPointerException:Attempt to de-reference a null object

Hi every one .

 

 i am getting the error  "System.NullPointerException:Attempt to de-reference a null object" while runing a test case.

 

The below is my Class: i am getting error in red line below:

 

public class FFtestPage {

FFtest__c FF;
public List<Opportunity> listOpp;
public String Id {get; set;}
public Opportunity Opp {get; set;}
public String Msg {get; set;}
public static boolean isApexTest = false;
//boolean flag = false;
public FFtestPage(ApexPages.StandardController stdController)
{
FF = (FFtest__c)stdController.getRecord();
Id = FF.Opportunity__c;

}

public PageReference Save()
{

try
{
if(FF.Id == null)
{
string obj = main(FF.Opportunity__c,FF.Id,FF.FFGuid__c,FF.Account_Number__c,FF.Account_Type__c,FF.Last_Name__c,FF.Miles__c,FF.Password__c,'add');
// if(obj.contains('success'))
if(obj.contains('success')|| obj=='success')
{
System.debug('*************Railsobj*********'+obj);
string[] parts = obj.split(',');
if(parts.size()>1)
{
FF.FFGuid__c = parts[1];
}
System.debug('*************Guid*********'+FF.FFGuid__c);
insert FF;
System.debug('insert');
// ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, parts[0]));
PageReference pr = new PageReference('https://c.cs9.visual.force.com/apex/FFTestPAge?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
pr.setRedirect(true);
pr.getParameters().put('paramName', 'Saved Successfully');
return pr;

}
else
{
System.debug('notinsert');
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, obj));
// PageReference pr = new PageReference('https://c.na14.visual.force.com/apex/FFPage?retURL='+Id);
//pr.setRedirect(true);
// pr.getParameters().put('paramName', obj);
// return pr;

}

}
else
{
string obj = main(FF.Opportunity__c,FF.Id,FF.FFGuid__c,FF.Account_Number__c,FF.Account_Type__c,FF.Last_Name__c,FF.Miles__c,FF.Password__c,'update');
if(obj.contains('success')|| obj=='success')
{
string[] parts = obj.split(',');
if(parts.size()>1)
{
FF.FFGuid__c = parts[1];
}
System.debug('*************Guidupdate*********'+FF.FFGuid__c);
update FF;
System.debug('update');
//ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, obj));
PageReference pr = new PageReference('https://c.na14.visual.force.com/apex/FFTestPAge?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
pr.setRedirect(true);
pr.getParameters().put('paramName', 'Updated Successfully');
return pr;
}
else
{
System.debug('notupdate');
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, obj));
//PageReference pr = new PageReference('https://c.na14.visual.force.com/apex/FFPage?id='+FF.Id+'&retURL='+Id+'&sfdc.override=1');
// pr.setRedirect(true);
// pr.getParameters().put('paramName', obj);
// return pr;
}

}

}

catch(DmlException ex)
{

}
return null;
}
public String main(String opid,String ffid,String guid,String actnu,String acttype,String lastn,Decimal miles,String pwd,String action)
{

SalesForceUrl__c sf = SalesForceUrl__c.getValues('AMUrl');
SalesForceUrl__c sf2 = SalesForceUrl__c.getValues('IsSandBox');
// Do a whole bunch of stuff

// Build the http request
HttpRequest req = new HttpRequest();
Http h = new Http();
if(sf != null && sf2 != null)
{
req.setEndpoint(sf.SettingValue__c+'/Abc/test?oppid='+ opid+'&ffid='+ffid+'&guid='+guid +'&acctnu='+actnu +'&accttype='+acttype+'&lastname='+lastn+'&miles='+miles+'&pwd='+pwd+'&actionType='+action+'&IsSandbox='+sf2.SettingValue__c);
}
req.setMethod('GET');

// Invoke web service call
String result = '';
if (!isApexTest){
// Make a real callout since we are not running a test
HttpResponse res = h.send(req);
result = res.getBody();
System.debug('Result=' +result);
System.debug('STATUS:'+res.getStatus());
} else {
// A test is running
result = 'success';
}

// Do whole bunch of stuff

return result;
}


// Wrapper method for "main" that we will call in the Test Class
public String mainForTest(){
isApexTest = true;
DateTime dT = System.now();
Decimal i = 1.2;
return main('006d0000007jfwO','a00d000000560NZ','a00d000000560NZ','11','AA','aa',i,'asd','add');
}
public PageReference Cancel()
{

PageReference pr = new PageReference('/' + Id);
pr.setRedirect(true);
return pr;
}
}

 

 

My test Class is:

 

@isTest
private class FFtestPagetestcase {

static testMethod void myUnitTest() {
FFtest__c FF;
List<Opportunity> listOpp;
List<FFtest__c> listFF;
String Id;
String result;
String webresult;
String webresultu;

string obj = '';
List<String> parts;


Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.StageName = 'Ticketing';
opp.p1_First_Name__c ='A';
opp.p1_Last_Name__c ='D';
opp.p1_Gender__c = 'S';
opp.p1_First_Name__c ='S';
opp.p1_Last_Name__c ='A';
opp.p1_Gender__c = 'D';
opp.DOB_Main_Passenger__c = System.now().date();
opp.Number_of_Passengers__c = 4;
opp.CloseDate = System.now().date();
opp.Departure_Date__c = System.now().date();
opp.Origin_City__c = 'DEL';
opp.Destination_City__c= 'MUI';
opp.Return_Date__c = System.now().date();
opp.Override_Itinerary_Builder__c = true;
opp.Complete_Itinerary_Information__c ='test';
opp.Approved__c ='Test1';
opp.AmMessage__c ='success';
insert opp;

FF = new FFtest__c();
FF.Account_Number__c ='1234456';
FF.Account_Type__c = 'ANA';
FF.DOB__c = system.today();
FF.Opportunity__c = opp.Id;
FF.Last_Name__c = 'Dutt';
FF.Miles__c = 1.2;
FF.Password__c= 'asd';
FF.FFGuid__c ='hhhhdofhff';
webresult = 'success';
obj = 'Success,785765876';
parts = obj.split(',');
if(parts.size()>1)
{
FF.FFGuid__c = parts[1];
}

insert FF;
opp.StageName = 'FF Account Assigned';
update opp;


webresultu = 'success';
update FF;

try
{
ApexPages.StandardController sc = new ApexPages.StandardController(FF);
FFtestPage sc1 = new FFtestPage(sc);


String retVal = sc1.mainForTest();
// Add asserts for validation
String expectedResult = 'success';
System.assertEquals(retVal, expectedResult);
sc1.Save();


FFtest__c FF2 = new FFtest__c();
FF2.Account_Number__c ='1234456';
FF2.Account_Type__c = 'ANA';
FF2.DOB__c = system.today();
FF2.Opportunity__c = opp.Id;
FF2.Last_Name__c = 'Dutt';
FF2.Miles__c = 1.2;
FF2.Password__c= 'asd';
FF.FFGuid__c ='hhhhdofhff';
webresult = 'success';
insert FF2;
SalesForceUrl__c sf = SalesForceUrl__c.getValues('AMUrl');
sf.Name='AMUrl';

insert sf;
SalesForceUrl__c sf2 = SalesForceUrl__c.getValues('IsSandBox');
sf2.Name='IsSandBox';

insert sf2;
sc1.Cancel();
}

catch(System.CalloutException e)
{
webresult = e.getMessage();
}
System.assertEquals(webresult,'success');

}
}

 

 

please guide me where i am going wrong.

 

 

thanks

bob_buzzardbob_buzzard

That seems a very odd line to generate that error, as you aren't dereferencing anything.  

deepakMdeepakM

hi bob

 

As i newly created sandbox account and get all my  production code in sandbox .now i just want to know one thing that when i code in eclipse and deploy it in the sandbox server  it deploys there, without any error with the test class also.so it mean,now  i can deploy it successfully in production too, if it deploy in sandbox .

 

 

please giud me further.

 

Thanks

bob_buzzardbob_buzzard

No, you can't deploy to production without the test coverage.

deepakMdeepakM

 

So you mean in sandbox thr is no test coverage issue

 

Thanks.

bob_buzzardbob_buzzard

I've found that I can deploy code between sandboxes without full coverage, but when deploy to production I have to have it.

deepakMdeepakM

Thanks bob.