-2

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;
    System.debug(data);
    if(data !=null)
    {
      testArray=data.split(';');  
      System.debug(testArray);
    }
}

there data is showing null how resolve that issue

closed as off-topic by Martin Lezer, Raul, Reshma, codeyinthecloud, Jayant Das Jan 10 at 14:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Martin Lezer, Raul, Reshma, codeyinthecloud, Jayant Das
If this question can be reworded to fit the rules in the help center, please edit the question.

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

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.

-2

do a null check, your code will work;

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

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