0

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(){
nameFile=contentFile.toString();
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];
     acctNames.add(a.Name);
     accstoupload.add(a);
  }
     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);
    System.debug('******Sai******');
  }
   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)){
     newAccts.add(acct);
   }
  }
   try{
 //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');
   ApexPages.addMessage(errormsg);
   } 

public List<Account> getuploadedAccounts(){

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

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

0

1 Answer 1

3
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
acctNames.add('Lavanya');

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.