+ Start a Discussion
Prakhar KPrakhar K 

Create a method for inserting accounts in Trailhead!!

Below code throws error saying: Getting error attempt dereference a  null pointer:

public class AccountHandler {
public class MyException extends Exception{}
public static Account insertNewAccount(String name) {
Account a = new Account();
try
{
    IF(name=='')
    {
    throw new MyException('Empty paramenter');
    }
    else
    {
    a.Name = name;
    insert a;
    return a;
    }
}
catch (DmlException e) {
return null;
}

}
}

what am I missing?
sfdcMonkey.comsfdcMonkey.com
hi Kumar
try this onces
public class AccountHandler {

    public static Account insertNewAccount (String accName){
        
        
    if(accName!=''){    
        try{
            Account a = new Account(Name=accName);
            insert a;
            System.debug('Bravo Andrè! Account created');
            return a;
        } catch(Exception e){
            System.Debug('Account not created');
            return null;
        }
    } else {
        return null;
    }
     
        
    }    
}
Thanks i hop its helps you
Mark it best answer iif it helps you so it make proper solution for others :)
 
Prakhar KPrakhar K
Hi Piyush_soni,
It gives error!!
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object
 
sfdcMonkey.comsfdcMonkey.com
hi kumar.prakhar
above class is working for my trailhead , you got this error because of any other configuration in your org
so Please create a new developer org and try again with above code.
Thanks
Daniel R PeaperDaniel R Peaper

Hi @kumar.prakhar

While Piyush_soni's solution gets the job done, it's not in the spirit of what the trail is teaching. We're not learning about the if statement here and so I believe that the code must attempt to insert the account record with a blank field and then we have to catch the error. I would suggest the following code:-

public class AccountHandler {

    public static account insertNewAccount(string AcName){
        
            try{
                Account acct = new Account(Name=AcName);
                    insert acct;
                System.debug('Account '+AcName+' created');
                return acct;
            }    catch(DmlException e){
                system.debug('DML Error:' + e.getMessage());
                return null;
            }
        }
}

Veena GopalVeena Gopal
There is no use of try -catch if you are using if -else. so use just try-catch
AK DAK D
I have just used the try-catch .. if-else is not required as the empty string throws the DMLexception and returns Null .
public class AccountHandler 
{
public static Account insertNewAccount(String Name) 
{

try
    {
    Account myAccount = new Account();
    myAccount.Name = Name;
    insert myAccount;
    System.debug('Account created');
    return myAccount;
    } 
catch (DmlException e) 
    {
    System.debug('A DML exception has occurred: ' +e.getMessage());
    return Null;    
    }
}

}