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
Carly Probasco 14Carly Probasco 14 

Illegal assignment from Set<String> to String

I'm trying to return either a string of text or a date field to display on a visualforce page.  When i compile it i get the error above. If i take out RatingRelease = new Set<String>(); i get an error System.NullPointerException: Attempt to de-reference a null object.  I'm not sure if i have initialized RatingRelease properly. 
public String RatingRelease {get;set;}

//Returns the Rating Release date or the 'rating pending'
        RatingRelease = new Set<String>();

        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = 'Rating Pending';
        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
        } else {
            RatingRelease = '';
        }
Best Answer chosen by Carly Probasco 14
PRAKASH JADA 13PRAKASH JADA 13
Initial code:
----------------------

public Set<String> RatingRelease {get;set;} ==> Modified Line
02 
03//Returns the Rating Release date or the 'rating pending'
04        RatingRelease = new Set<String>();
05 
06        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
07            RatingRelease.add('Rating Pending'); ==> Modified Line
08        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
09            RatingRelease.add(currentQRpCase.Rating_Externally_Visible_Date__c.format()); ==> ModifiedLine
10        } else {
11            RatingRelease.add(''); ==> Modified Line
12        }

If you are declaring set then you have to use set.add();


Second code:
----------------------------

public String RatingRelease {get;set;} ==> Modified Line
02 
03        //Returns the Rating Release date or the rating pending
04        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
05            RatingRelease = 'Rating Pending';
06        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
07            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
08        } else {
09            RatingRelease = '';
10        }

Now it won't need Set because RatingRelease is a String.


Best use: In your case, you don't need Set because you are using the if-else condition to assign the variable after execution you'll get only one string. So to store that you only need String. 


I hope this helps.

Thanks,

All Answers

PRAKASH JADA 13PRAKASH JADA 13
public String RatingRelease {get;set;} --> It is individual string.

RatingRelease = new Set<String>(); --> Here you are assigning the set. So please change this line

public String RatingRelease {get;set;} --> public Set<String> RatingRelease{get; set;}
Carly Probasco 14Carly Probasco 14
I added the following and i'm still missing something. Getting error on the RatingRelease variable.
public Set<String> RatingRelease {get;set;}

        //Returns the Rating Release date or the rating pending
        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = 'Rating Pending';
        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
        } else {
            RatingRelease = '';
        }
PRAKASH JADA 13PRAKASH JADA 13
If you check the code you deleted the line RatingRelease=new set<String>();
PRAKASH JADA 13PRAKASH JADA 13
Initial code:
----------------------

public Set<String> RatingRelease {get;set;} ==> Modified Line
02 
03//Returns the Rating Release date or the 'rating pending'
04        RatingRelease = new Set<String>();
05 
06        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
07            RatingRelease.add('Rating Pending'); ==> Modified Line
08        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
09            RatingRelease.add(currentQRpCase.Rating_Externally_Visible_Date__c.format()); ==> ModifiedLine
10        } else {
11            RatingRelease.add(''); ==> Modified Line
12        }

If you are declaring set then you have to use set.add();


Second code:
----------------------------

public String RatingRelease {get;set;} ==> Modified Line
02 
03        //Returns the Rating Release date or the rating pending
04        IF (Today < currentQRpCase.Rating_Externally_Visible_Date__c){
05            RatingRelease = 'Rating Pending';
06        } else IF(Today > currentQRpCase.Rating_Externally_Visible_Date__c){
07            RatingRelease = currentQRpCase.Rating_Externally_Visible_Date__c.format();
08        } else {
09            RatingRelease = '';
10        }

Now it won't need Set because RatingRelease is a String.


Best use: In your case, you don't need Set because you are using the if-else condition to assign the variable after execution you'll get only one string. So to store that you only need String. 


I hope this helps.

Thanks,
This was selected as the best answer