here i want to insert some names which is having semicolon(;) in between two names iam trying to splitting code but am getting null

List<Contactsample__c> cammemlist=new List<Contactsample__c>();
Contactsample__c cs=new Contactsample__c();
String[] testArray;
if(cs !=null)
    String data=cs.CampaignAttended__c;
    if(data !=null)

there data is showing null how resolve that issue

  • value is not their i have to insert Photography Seminar 2018;Protecting Gabon; that type of data – divya Jan 10 '19 at 9:21
  • 2
    Your code will work if cs.CampaignAttended__c has values – Reshma Jan 10 '19 at 9:28

Before you can address the issue, you must understand the issue.

Some simple debugging is all it takes to understand the issue. Just start at the point where you have a known state, and work backwards until you find out why data is null.

Your known state here is that data is null. You know this because of the System.debug(data); line.

Now, start working backwards. Where does data get set?
It gets set on the line above, String data=cs.CampaignAttended__c;
Because data is null, cs.CampaignAttended__c must be null.

Why is cs.CampaignAttended__c null?
Continue to work backwards through your code.

Eventually, you reach the declaration and initialization of the cs variable, Contactsample__c cs=new Contactsample__c();

You don't set cs.CampaignAttended__c anywhere between initializing the cs variable, and when you set your data variable using cs.CampaignAttended__c. It's only natural that this variable would be null.

How you would go about making this data variable not be null really depends on where this snippet of code is used (and the purpose it's for). The solution here is different if you're using this in a Visualforce controller, compared to if it's being used in a trigger.

About all we can say with the information that you've given us is that your second line, Contactsample__c cs=new Contactsample__c();, is not what you want to do here. This variable should either be fed from the results of a query, input from a Visualforce page/Lightning component, or from a trigger context variable.

For sake of completeness, you're getting the Null Pointer Exception because data = null, and null contains no methods, attributes, or properties.

| improve this answer | |

do a null check, your code will work;

String data;
if(cs.CampaignAttended__c != null){
| improve this answer | |
  • that code is also not working it is not showing values in debug log – divya Jan 10 '19 at 11:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.