Why am I getting “Static method cannot be referenced from a non static context: String String.valueOf(Object)”?
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?
@AuraEnabled public static void generatePDF(myRec__c rec, string selquarter){ string selqenddate = selquarter.substringBetween('(', ')'); date startdate = (selqenddate.valueOf(selqenddate)).addMonths(-3).startofMonth; myPDF(rec.id, '', ''); }
Why getting error “java non static method cannot be referenced from a static context”?
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 = (Date.valueOf(selqenddate)) .addMonths(-3) .toStartOfMonth();
Note, there’s no property startOfMonth on Date class.