function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cassycassy 

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

Hallo everyone,

 

my name ist Cassandra an I'm a beginner in developing VisualPages, now I want to create a Visualforce Page, that can search for Accounts, Contacts And Opportunities given in a Customer Object called Hannover_ Potentials. The AccountName, -street, -City and ZipCode is given in the CustomerObject as  Formalfields and also the firstName And LastName Of the Contacts are given and the Name of the Opportunity.

I have written a search Method in me extensionController SearchController, where I selected all Hannover_Potential Objects with the same accountname as my inputName and accountcity is like my inputcity etc...Then I select in Account all Accounts, who have the same name as the Accountname of my hannover_Potential Object and write them into a list of Accounts.

I do the same for the Contacts and for the opportunities.

Then I have a list of Accounts, Contacts and Opportunities and I want to put them in PageblockTables.

But I think my search-function is going wrong, because I get this Error Massage, when I click on my SearchButton in my Visualforce Page.

 

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

 

I know that my Visualfocepage code is correct because I have tested it with an other Controller and everything was fine. So the mistake must be in my Searchcontrollerclass.

 


Here is the Code Of my ControllerClass

public with sharing class SearchController {

     //AusgabeListe 
     public Account [] acc{get; set;}
     public Opportunity [] opp{get; set;}
     public Contact [] con1{get; set;}
     public Contact [] con2{get;set;}
     
 // InputText Argumente
     public String firstName1{get; set;}
     public String lastName1{get;set;}
     public String street1{get; set;}
     public String city1{get; set;}
     public String plz1{get; set;}
 //Constructor
    public SearchController(ApexPages.StandardSetController controller) {
        firstName1='';
        lastName1='';
        city1='';
        street1='';
        plz1='';
        
    }
 // Action for Search Button
    public PageReference search1() {
        
       this.searchH();
                return null;
    }
public void searchH() {
         Account tempacc;
         Contact tempcon;
         Opportunity tempopp;
         Integer a=1;
         
         
         
         // Search for Accounts
            for(Hannover_Potentials__c hp:[SELECT Account_Name__c,Account_ZIP_CODE__c,Account_City__c,Account_Street__c FROM Hannover_Potentials__c WHERE Account_Name__c like:('%'+firstName1+'%') and ( Account_Street__c like:('%'+street1+'%') and Account_City__c like:('%'+city1+'%') and Account_ZIP_CODE__c like:('%'+plz1+'%'))])
           {
                tempacc=[SELECT Id, OwnerId, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name like: ('%'+hp.Account_Name__c+'%') and BillingCity like: ('%'+hp.Account_City__c+'%') and BillingStreet like: ('%'+hp.Account_Street__c+'%') and BillingPostalCode like: ('%'+hp.Account_ZIP_CODE__c +'%')];
                 System.debug('aaaaaaaaaaaaaaaaaaa'+tempacc.Name);
                acc[a]=tempacc;
                a=a+1;
            }
            
            
            a=0;
            
            //Search for Contact 1
           for(Hannover_Potentials__c hp:[SELECT  General_Contact_Person_First_Name__c, General_Contact_Person_Last_Name__c FROM Hannover_Potentials__c WHERE General_Contact_Person_First_Name__c like:('%'+firstName1+'%') and General_Contact_Person_Last_Name__c like:('%'+lastName1+'%')])
           {
                tempcon= [SELECT Id,Name, OwnerId, Email FROM Contact WHERE FirstName like: ('%'+hp.General_Contact_Person_First_Name__c+'%') and LastName like: ('%'+hp.General_Contact_Person_Last_Name__c+'%')];
                con2[a]=tempcon;
                a=a+1;
            }
            
            a=0;
            
            
           // Search for Contact 2
           for(Hannover_Potentials__c hp:[SELECT Main_Contact_Last_Name__c, Main_Contact_First_Name__c FROM Hannover_Potentials__c WHERE Main_Contact_First_Name__c like:('%'+firstName1+'%') and Main_Contact_Last_Name__c like:('%'+lastName1+'%')])
           {
                tempcon= [SELECT Id, Name, OwnerId, Email FROM Contact WHERE FirstName like: ('%'+hp.Main_Contact_First_Name__c+'%') and LastName like: ('%'+hp.Main_Contact_Last_Name__c+'%')];
                con1[a]=tempcon;
                a=a+1;
            }
            
            a=0;
            //suche nach Opportunities
           for(Hannover_Potentials__c hp:[SELECT Opportunity_Name__c FROM Hannover_Potentials__c WHERE Opportunity_Name__c like:('%'+firstName1+'%') or Opportunity_Name__c like:('%'+lastName1+'%')])
           {
                tempopp= [SELECT Id, Name FROM Opportunity WHERE Name like: ('%'+hp.Opportunity_Name__c+'%')];
                opp[a]=tempopp;
                a=a+1;
            }
            a=0;

return;
}
}


  And my debugcall informed me the in the problem is here in my for loop:

 tempcon= [SELECT Id,Name, OwnerId, Email FROM Contact WHERE FirstName like: ('%'+hp.General_Contact_Person_First_Name__c+'%') and LastName like: ('%'+hp.General_Contact_Person_Last_Name__c+'%')];
                con2[a]=tempcon;
                a=a+1;

I will be very happy for every advices, you can give me.

 

Thanks in advance

 

Cassandra

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In your SearchController, you define the arrays to hold the accounts, opportunities, contacts, but these will remain null unless you instantiate the array.  Thus when you try to add elements to the arrays in searchH, you get the nullpointerexception.

 

I'd suggest you want to add the following to your constructor:

 

public SearchController(ApexPages.StandardSetController controller) {
        firstName1='';
        lastName1='';
        city1='';
        street1='';
        plz1='';
        acc=new List<Account>();
        opp=new List<Opportunity>();
        con1=new List<Contact>();
        con2=new List<Contact>(); 
    }

 also, arrays start from index 0 rather than 1, so when you declare the variable 'a' in searchH, you should initialise it to 0 rather than 1.

 

 

All Answers

bob_buzzardbob_buzzard

In your SearchController, you define the arrays to hold the accounts, opportunities, contacts, but these will remain null unless you instantiate the array.  Thus when you try to add elements to the arrays in searchH, you get the nullpointerexception.

 

I'd suggest you want to add the following to your constructor:

 

public SearchController(ApexPages.StandardSetController controller) {
        firstName1='';
        lastName1='';
        city1='';
        street1='';
        plz1='';
        acc=new List<Account>();
        opp=new List<Opportunity>();
        con1=new List<Contact>();
        con2=new List<Contact>(); 
    }

 also, arrays start from index 0 rather than 1, so when you declare the variable 'a' in searchH, you should initialise it to 0 rather than 1.

 

 

This was selected as the best answer
cassycassy

thanks that was a good advice. Now my Searchfunction is finish.:smileyhappy :)