You need to sign in to do that
Don't have an account?
Prakhar 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?
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?
try this onces Thanks i hop its helps you
Mark it best answer iif it helps you so it make proper solution for others :)
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
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
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;
}
}
}
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;
}
}
}