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
Yoshinori MoriYoshinori Mori 

System.NullPointerException: Attempt to de-reference a null object

I'm now testing by running 'Runtests' of  the test class I created.

However, I'm now facing an error explained as follows.

I would like to know  the reason of the error.

I appreciate if  somebody could  help me with the issue

 

(******** is an indication of abbreviation)

 

I wrote in the test class as follows;

 

@isTest
private class (test class name) {

  static testMethod void myUnitTest() {

**************************************

**************************************

(class name) controller = new (classname) ();

 

controller.setIntervalFlg();

 }

}

 

 

the 'class name' above is referenced to the

class to be tested below.

It seems like the error message indicates something wrong with the code written in the line of  ' if(interval.equals('none')) ' below

 

 

public with sharing class (classname) {

****************

****************

****************

public String interval { get; set; }
 public Boolean interval15Flg { get; set; }
 public Boolean interval30Flg { get; set; }
 public Boolean interval60Flg { get; set; }
 public Boolean interval300Flg { get; set; }
 public Boolean interval600Flg { get; set; }
 
 public String nowDatetime { get; set; }
 
 Integer count = 0;
    public PageReference intervalCounter() {
        count++;
        setIntervalFlg();
        refresh();
        nowDatetime = Datetime.now().format('yyyy/MM/dd HH:mm:ss');
        return null;
    }
    public Integer getCount() {
        return count;
    }

 

public void setIntervalFlg(){
  if(interval.equals('none')) {
   interval15Flg = false;
   interval30Flg = false;
   interval60Flg = false;
   interval300Flg = false;
   interval600Flg = false;
  }
  else if (interval.equals('15')) {
   interval15Flg = true;
   interval30Flg = false;
   interval60Flg = false;
   interval300Flg = false;
   interval600Flg = false;
  }
  else if (interval.equals('30')) {
   interval15Flg = false;
   interval30Flg = true;
   interval60Flg = false;
   interval300Flg = false;
   interval600Flg = false;
  }
  else if (interval.equals('60')) {
   interval15Flg = false;
   interval30Flg = false;
   interval60Flg = true;
   interval300Flg = false;
   interval600Flg = false;
  }
  else if (interval.equals('300')) {
   interval15Flg = false;
   interval30Flg = false;
   interval60Flg = false;
   interval300Flg = true;
   interval600Flg = false;
  }
  else if (interval.equals('600')) {
   interval15Flg = false;
   interval30Flg = false;
   interval60Flg = false;
   interval300Flg = false;
   interval600Flg = true;
  }

}

Best Answer chosen by Admin (Salesforce Developers) 
Afzal MohammadAfzal Mohammad

In that case, your test case should look something like this.

 

@isTest
private class (test class name) {

  static testMethod void myUnitTest() {

**************************************

**************************************

(class name) controller = new (classname) ();

 

controller.Interval = 'none';

controller.setIntervalFlg();

 

controller.Interval = '15';

controller.setIntervalFlg();

 

controller.Interval = '30';

controller.setIntervalFlg();

 

controller.Interval = '60';

controller.setIntervalFlg();

 

controller.Interval = '300';

controller.setIntervalFlg();

 

controller.Interval = '600';

controller.setIntervalFlg();

}

}

 

Hope that helps.

 

Afzal

All Answers

Afzal MohammadAfzal Mohammad

I see the problem.

 

You may want to set some value to interval variable, before calling the setIntervalFlg method in your test case.

 

Your new test case should look something like this.

 

@isTest
private class (test class name) {

  static testMethod void myUnitTest() {

**************************************

**************************************

(class name) controller = new (classname) ();

 

controller.Interval = 'none';

controller.setIntervalFlg();

 }

}

 

Hope that helps.

 

Afzal

Yoshinori MoriYoshinori Mori

Dear Afzal

 

Thank you very  much for your help.

It works.

 

May I ask you one more question relating to your advice?

I added one line according to your advice as follows.

controller.interval = 'none'; 

I tried adding others like

controller.interval = '15';

controller interval = '30';

controller.interval = '60';

controller.interval = '300';

controller.interval = '600';

However, 'None' and '600' were succeeded in the runtest.

Other 30,60,300 failed.

It seems like the last line, which is '600' in this case, succeeds except for 'none'.

If I delete controller.interval = '600', then 300 succeeds.

Do I need to add some codes to initialize previous ones before each interval?

Could it be possible to let me know how I should add those codes?

 

Mori

 

Yoshinori MoriYoshinori Mori

Dear Afzal

 

Please ignore the last mail I sent to you.

I'm a beginner in programming.

Please pardon me for asking you very fundamental questions.

 

Mori

Afzal MohammadAfzal Mohammad

In that case, your test case should look something like this.

 

@isTest
private class (test class name) {

  static testMethod void myUnitTest() {

**************************************

**************************************

(class name) controller = new (classname) ();

 

controller.Interval = 'none';

controller.setIntervalFlg();

 

controller.Interval = '15';

controller.setIntervalFlg();

 

controller.Interval = '30';

controller.setIntervalFlg();

 

controller.Interval = '60';

controller.setIntervalFlg();

 

controller.Interval = '300';

controller.setIntervalFlg();

 

controller.Interval = '600';

controller.setIntervalFlg();

}

}

 

Hope that helps.

 

Afzal

This was selected as the best answer
Yoshinori MoriYoshinori Mori

Thank you again for helping  me our of this problem

 

Mori