Hi i am trying to check whether the account name exists or not, while uploading a csv file to create account, contact and opportunity. But in this i am getting error,i tried a lot but i can't resolve this. my code apex class look like ths.

List<String> acctNames;
public Pagereference ReadFile(){
filelines = nameFile.split('\n');
accstoupload = new List<Account>();
contoupload = new List<Contact>();
opptoupload = new List<Opportunity>();
custtoupload = new List<CustomLead__c>();

 for (Integer i=1;i<filelines.size();i++){
     String[] inputvalues = new String[]{};
     inputvalues = filelines[i].split(',');
     Account a = new Account();
     a.Name = inputvalues[0];
     List<Account> existingAccts = [SELECT Id, Name FROM Account where name in  :acctNames];
   //create a map with names as key
   Map<String, Id> acctNamesIdMap = new Map<String, Id>();
   // load the map - this will help you find out if an account name exists already
   for (Account acct : existingAccts){
    acctNamesIdMap.put(acct.Name, acct.Id);
   List<Account> newAccts = new List<Account>();
  for (Account acct : accstoupload){
    //if account name does not exist in map, add it to list of new accounts
    if (!acctNamesIdMap.containsKey(acct.Name)){
 //insert accstoupload;
  insert newAccts;
   catch (Exception e){
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An   error has occured. Please check the template or try again later');

public List<Account> getuploadedAccounts(){

  if (accstoupload!= NULL)
    if (accstoupload.size() > 0)
       return accstoupload;
       return null;
     return null;

Error Mg: Attempt to de-reference a null object Error is in expression '{!ReadFile}' in component in page uploadaccountsall


1 Answer 1

List<String> acctNames;
acctNames.add('Lavanya');//Here is the issue .

try the above to observe the issue yourself in execute anonymous or developer console

You have not instantiated the list and hence its giving you the exception.

List<String> acctNames=new List<String>() ;//Correct way to instantiate and use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.