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
Shivani BankarShivani Bankar 

error: attempt to de-reference null object

hello,
I'm having attempt to de-reference null object error with following code

public class StringChecking{
    
    public String name1{get; set;}
    public List<contact> list2;
    Contact contact1;
    public Contact getContact(){
        if(contact1!=null){
        contact1=[select id,Name,Country__c from Contact where Name=:name1];
    }
        return contact1;
    }
    
    public pagereference ContactList(){
       return null;
    }
    public List<Contact> getList(){
        try{
            if(contact1!=null && contact1.Country__c=='India'){
                list2.add(contact1);                
            }
            
            else if(contact1!=null && contact1.Country__c=='US'){
                 list2.add(contact1);
            }
            
            else{
                 list2.add(contact1);
            }
        }
        catch(DmlException e)
        {
            System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        return list2;
    }
    
    public pagereference edit(){
        pagereference pg=new Pagereference('https://ap5.salesforce.com/'+contact1.Id);
        return pg;
    }
    
}


please help me to solve this
Best Answer chosen by Shivani Bankar
Gaurish Gopal GoelGaurish Gopal Goel
Hi Shivani, Please see the cleaned up code. It will solve your problem. Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.​
public class StringChecking
{
    public String name1{get; set;}
    public List<contact> list2;
    Contact contact1;
	
	public StringChecking()
	{
		list2 = new List<contact>();
	}
    public Contact getContact()
	{
		contact1 = [select id,Name,Country__c from Contact where Name =: name1];
		return contact1;
    }
    
    public pagereference ContactList(){
       return null;
    }
    public List<Contact> getList()
	{
        try
		{
            if(contact1!=null && contact1.Country__c=='India'){
                list2.add(contact1);                
            }
            
            else if(contact1!=null && contact1.Country__c=='US'){
                 list2.add(contact1);
            }
            
            else{
                 list2.add(contact1);
            }
        }
        catch(DmlException e)
        {
            System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        return list2;
    }
    
    public pagereference edit(){
        pagereference pg=new Pagereference('https://ap5.salesforce.com/'+contact1.Id);
        return pg;
    }
}

All Answers

Gaurish Gopal GoelGaurish Gopal Goel
Hi Shivani, Please see the cleaned up code. It will solve your problem. Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.​
public class StringChecking
{
    public String name1{get; set;}
    public List<contact> list2;
    Contact contact1;
	
	public StringChecking()
	{
		list2 = new List<contact>();
	}
    public Contact getContact()
	{
		contact1 = [select id,Name,Country__c from Contact where Name =: name1];
		return contact1;
    }
    
    public pagereference ContactList(){
       return null;
    }
    public List<Contact> getList()
	{
        try
		{
            if(contact1!=null && contact1.Country__c=='India'){
                list2.add(contact1);                
            }
            
            else if(contact1!=null && contact1.Country__c=='US'){
                 list2.add(contact1);
            }
            
            else{
                 list2.add(contact1);
            }
        }
        catch(DmlException e)
        {
            System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        return list2;
    }
    
    public pagereference edit(){
        pagereference pg=new Pagereference('https://ap5.salesforce.com/'+contact1.Id);
        return pg;
    }
}
This was selected as the best answer
Shivani BankarShivani Bankar
Thanks Gaurish Gopal Goel, this solves issue