1

So I have the following controller:

List<User> users {get; set;}
 public reshapeUserDivisions() 
 {
    users = new List<User>();
    users = [SELECT Id,  Division, FROM User WHERE isActive=true];
 }

public void changedivisions()
{
    boolean isChanged = true;
    for(User c: users)
    {
        if(c.Division.equals('LA'))
        {
            c.Division = 'Los Angeles';
            update c;
            isChanged = true;
        }
        else if(c.Division.equals('NYC'))
        {
            c.Division = 'New York';
            update c;
            isChanged = true;
        } 
        else
        {
            isChanged = false;
        }
    }

}

 public static testMethod void reshapedivisiontest()
 {
    Pagereference pageref = new Pagereference('/apex/objecteditor');
    Test.setCurrentPage(pageref);

    reshapeUserDivisions control = new reshapeUserDivisions();
    Profile p = [select id from profile where name='Saatchi - Standard User'];

    User testUser = new User(alias = 'u1', email='[email protected]',
    emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
    localesidkey='en_US', profileid = p.Id, country='United States',
    timezonesidkey='America/Los_Angeles', username='[email protected]', division = 'NYC');

    insert testUser;

    control.changedivisions();

    List<User> users = [SELECT name, division FROM User WHERE alias = 'u1'];
    System.assertEquals(users[0].Division, 'New York');
 }

Called by the following VisualForce page:

<apex:page controller="reshapeUserDivisions">
<apex:form >
  <apex:pageBlock title="Object Editor" id="objeditor" mode="edit">
  <apex:commandButton action="{!changedivisions}" value="Standardize User Divisions" />
  <apex:actionFunction name="reshapeusers" action="{!changedivisions}" rerender="results,debug,errors">
  </apex:actionFunction>
  </apex:pageBlock>
  </apex:form>
</apex:page>

And keep getting a Null Pointer Exception at the unit test [specificially, at control.changedivisions()], no idea why. Any insights?

1
  • FYI, it was a combination of all of these answers. It turns out that some user divisions were returning a null value, and my code was not handling it. I ended up wrapping the code within the for loop in a try...catch... block, to handle exceptions caused by null values.
    – gfppaste
    Commented Feb 21, 2013 at 15:19

3 Answers 3

4

You actually have two problems. One problem, as @PJC referenced, you are saving your User after you instantiate your class which means it won't get loaded by in the constructor. Your other problem is you have malformed SOQL with another , after Division. I would suggest changing your users object to be lazy loaded as:

private List<User> users {
    get{
        if(users == null){
            users = [SELECT 
                         Id,  Division 
                     FROM 
                         User 
                     WHERE 
                         isActive = true
            ];
        }
        return users;
    }
    set;
}
public ReshapeUserDivisions(){

}

This will populate your users object the first time you access it and it won't require your constructor to worry about loading it.

4

You are inserting your User record, after you have instantiated your class, which, in the constructor loads all the users from the database. Try this:

public static testMethod void reshapedivisiontest()
 {
    Pagereference pageref = new Pagereference('/apex/objecteditor');
    Test.setCurrentPage(pageref);

    Profile p = [select id from profile where name='Saatchi - Standard User'];
    User testUser = new User(alias = 'u1', email='[email protected]',
    emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
    localesidkey='en_US', profileid = p.Id, country='United States',
    timezonesidkey='America/Los_Angeles', username='[email protected]', division = 'NYC');

    insert testUser;

    reshapeUserDivisions control = new reshapeUserDivisions();
    control.changedivisions();

    List<User> users = [SELECT name, division FROM User WHERE alias = 'u1'];
    System.assertEquals(users[0].Division, 'New York');
 }
2
  • Still getting the null pointer exception... I even added a System.assert(!control.users.isEmpty());, and it's passing that... I honestly can't think of why it's doing this haha.
    – gfppaste
    Commented Feb 21, 2013 at 14:22
  • Try using the debug log, and check where it is going wrong. Setup -> Monitoring -> Debug Logs -> Monitor your User -> Run test again, and check your debug log
    – pjcarly
    Commented Feb 21, 2013 at 14:24
1

Well just went through your code and what I can see is null pointer exception can be generated by this line if(c.Division.equals('LA')), when the Division is null.

Probable Cause

  • Well firstly check the API version of class, Probably your class is using older API, Something like 24/25 where test methods can see the organizational data. Try setting the API version to latest One 27.0 .
1
  • That field shouldn't return null if the data isn't there, it should just return an empty string I believe. Commented Feb 21, 2013 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .