I have this static class called from my lightning component, but am getting the error

"Static method cannot be referenced from a non static context: String String.valueOf(Object)"

on the line where I try and calculate a start date from the string passed. What do I need to do to fix this?

public static void generatePDF(myRec__c rec, string selquarter){
    string selqenddate = selquarter.substringBetween('(', ')');
    date startdate = (selqenddate.valueOf(selqenddate)).addMonths(-3).startofMonth;
    myPDF(rec.id, '', '');

The string class's valueOf() method is a static method.

Static methods need to be called like this: Class.staticMethodName() i.e. String.valueOf()

What you're currently doing is using an instance of a string to try to call a static method, which (as the error indicates) is not allowed.





Of course, you don't need to use String.valueOf() at all here because selquarter is a string, and substringBetween() also returns a string.

Instead, you need to be using a method that takes a String as input, and gives you a Date as output such as Date.parse()


As the error suggests, you are trying to use a static method valueOffrom String class on an instance of a String named selqenddate, which is not allowed.

You are most likely are trying to construct a date from a string value, and that you will need to utilize the Date.valueOf()instead. Your code should look like something as below:

Date startdate = 

Note, there’s no property startOfMonth on Date class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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