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
Shital SomvanshiShital Somvanshi 

Apex Basics & Database: Manipulate Records with DML

i am getting the following error while checking the challenge
"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"
below is my code:
public class AccountHandler{
    
    public static Account insertNewAccount(String strName)
    {
        if(strName!=null)
        {
            
       
        Account acc=new Account(Name=strName);
        try
        {
            insert acc;
            return acc;
        }
        catch(DmlException e)
        {
            System.debug('Dml exception has occured' +e.getMessage());
            return (null);
        }
        }
        else
        {
            return null;
        }
    }
}
Suraj TripathiSuraj Tripathi
Hi Shital,

You Can Try This Code
 
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;
        }   
    }    
}



If This Solves Your Problem, Please Mark This Question as Best Answer to remove from unsolved questions.

Regards
Suraj
Shital SomvanshiShital Somvanshi
hey suraj,
i have tried to check the challenge with your code, but still m getting the same error
Suraj TripathiSuraj Tripathi
Hi Shital,

This is the error which you have mentioned above:
"System.NullPointerException: Attempt to de-reference a null object"

Even, I have checked 'your' code in my Org, It is working fine, 
Maybe Some of your Trigger or Process maybe active, which may cause an error
then, Please Make Sure Any Trigger or Process must not be active before checking challenge.

Regards
Suraj
v varaprasadv varaprasad
Hi Suraj,

Please check once below code:
public class AccountHandler {
    
    public static account insertNewAccount(string name){
        account a = new account();
        a.name = name;
        try{            
            insert a;
            system.debug('record insertd successfully');
        }catch(Exception e){
            system.debug('error message'+e.getmessage());           
            return null;
        }
        
        return a;
    }

}

If any other trigger is there on account object please deactivate and check once.Maybe some triggers are firing.


Thanks
Varaprasad
 
Shital SomvanshiShital Somvanshi
hi Varaprasad,
can u guide me about how to deactivate triggers on account object?
v varaprasadv varaprasad
Hi Shital,

Please check once below screenshot.

User-added image
v varaprasadv varaprasad
Then click on edit and deactivate the trigger.


User-added image 


 
Shital SomvanshiShital Somvanshi
i have checked for triggers but there are no triggers defined for account object.
User-added image
v varaprasadv varaprasad
Please check once below code : 
 
public class AccountHandler {
public static Account insertNewAccount(String name) {
Account a = new Account();
a.Name = name;
try
{
insert a;
} catch (Exception e) {
return null;
}
return a;
}
}

 
Shital SomvanshiShital Somvanshi
i have tried the above code also,still it is not working
Tanishq ShrivastavaTanishq Shrivastava
Try with new Playground. It worked for me.