Home > controller > System Null Pointer Exception: debug help

System Null Pointer Exception: debug help

February 21Hits:1
Advertisement

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 protected]',     emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',     localesidkey='en_US', profileid = p.Id, country='United States',     timezonesidkey='America/Los_Angeles', [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?

Answers

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.

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 protected]',
    emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
    localesidkey='en_US', profileid = p.Id, country='United States',
    timezonesidkey='America/Los_Angeles', [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');
 }

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 .

Related Articles

  • System Null Pointer Exception: debug helpFebruary 21

    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

  • Using System.err.println() for debugging in JavaApril 30

    System.err The "standard" error output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user. By conventio

  • Trigger causes system null pointer exceptionDecember 6

    I'm having problem with my trigger.Please help me to resolve this system null pointer exception trigger theatreupdate on Transaction__c (before insert) { for(Transaction__c t:Trigger.new) { t.TheaterT__r.Number_Of_Seats_Sold__c=0; } //TheaterT__r-par

  • What is the impact on system performance when kernel debug info is enabled?June 16

    In order to debug kernel or use tools like stap (https://en.wikipedia.org/wiki/SystemTap) kernel must be compiled with full debugging information included - namely option CONFIG_DEBUG_INFO has to be enabled. I was wondering how does it impact system

  • System.Null pointer exception error on an empty line of code?December 23

    I am getting below error in a test class when I want to save another class. System.NullPointerException: Attempt to de-reference a null object at line 32 The problem is that I commented line 32, but was still getting the error on this line. Now even

  • roamingSettings = null (An exception of type 'System.NullReferenceException' occurred in WindowsPhone.exe but was not handled in user code)

    roamingSettings = null (An exception of type 'System.NullReferenceException' occurred in WindowsPhone.exe but was not handled in user code) January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • Detecting System Mode in Debug LogDecember 6

    I'm attempting to ascertain if a block of code is running in System mode via the Debug Logs I have log entries which appear as such: 21:26:05.741 (2741491000)|SYSTEM_MODE_ENTER|true .. .. 21:26:05.741 (2741491000)|SYSTEM_MODE_EXIT|true And 21:26:05.7

  • Should one really set pointers to `NULL` after freeing them?December 10

    There seem to be two arguments why one should set a pointer to NULL after freeing them. Avoid crashing when double-freeing pointers. Short: Calling free() a second time, by accident, doesn't crash when it's set to NULL. Almost always this masks a log

  • An unhandled exception of type 'System.NullReferenceException' occurred Kinect Application C#

    An unhandled exception of type 'System.NullReferenceException' occurred Kinect Application C# January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • Null Reference Exception from C# List.Find T

    Null Reference Exception from C# List.Find T January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • System.NullReferenceException: Object reference not set to an instance of an object. at Oracle.DataAccess.Client.OracleConnection.Open()

    System.NullReferenceException: Object reference not set to an instance of an object. at Oracle.DataAccess.Client.OracleConnection.Open() January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • Null reference exception mvc 4

    Null reference exception mvc 4 January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • Returning null value in Model members causing object reference error

    Returning null value in Model members causing object reference error January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • C# System.NullReferenceException error

    C# System.NullReferenceException error January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • Hidden field null reference exception when calling webmethod from javascript

    Hidden field null reference exception when calling webmethod from javascript January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • Entity Framework code first null reference exception after check for null or empty string

    Entity Framework code first null reference exception after check for null or empty string January 11

    I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do about it? --------------Solutions------------- What is the cause? Bottom L

  • My Event-handling systemNovember 26

    I have written about 180 lines of code that implements an Event system. I would like a general review about the code and I'd love comments about the usability of it. (Is the code useful? Would you like to use it or a similar system like it?) Summary

  • Debugging Spring Configuration doesn't workJanuary 15

    I add Security problems in may Spring application so I wanted to debug Spring thru log4j. Log4j is working because my services are logging debug information to the console. But Spring does not log anything to my console Here is my log4j.xml <?xml ver

  • Debug log shows data in list but Visualforce page is not displaying anythingFebruary 3

    In order to help HR manage license allocations for our HCM app, I worked up a proof of concept for a simple vf page that shows which users are assigned a specific permission set, and also are provisioned access to the HCM via a lookup to user on work

  • Spring-batch Entity Manager becomes null after initFebruary 15

    I'm currently implementing a Spring-batch that reads and writes to files BUT also needs to do CRUD operations on a database. I've tried to simply define an Entity manager in my xml configuration, and use it in my DAO class. However, right after the i

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 1.190 s.