You need to sign in to do that
Don't have an account?
SFmaverick
Error: Compile Error: Variable does not exist: BegDate at line 9 column 40
This is killing my brain, why is it telling me the variable doesn't exist? It's defined within the controller and used in the method before this!
Error: Compile Error: Variable does not exist: BegDate at line 9 column 40
public class ScheduleViewController { //Initializes and provides a get method for the user inputted date public String[] BegDate; public String[] getBegDate() { return BegDate; }
//Initalizes a date field to put the string into for use as a filter criteria
Static Date DayDate = stringToDate(BegDate);
//Adds one day to the current DayDate field in order to provide consecutive days' lists public void setDayDate() { DayDate = DayDate + 1; }
//Gets TheList - an array of records from Shift__c where the Date equals DayDate public List<Shift__c> getTheList() { List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = TODAY]; System.Debug('MY LIST:' + TheList); return TheList; }
//Converts a string from mm/dd/yyyy to a date public Static Date stringToDate(String s){ //Input Date String is in the format mm/dd/yyyy String[] stringDate = s.split('/'); Integer m = Integer.valueOf(stringDate[0]); Integer d = Integer.valueOf(stringDate[1]); Integer y = Integer.valueOf(stringDate[2]); return date.newInstance(y,m,d); } }
because you've said DayDate is static, but your BedDate variable is not, so its not in scope for the static initialization.
I agree with Simon. Basically static variables cannot access instance variables.
If I didn't initialize the DayDate as static, then it wouldn't work with the stringToDate function below. If I remove static from both of them, it throws me another error.
I took static out of both, and I'm now getting the follow errors. I posted my code after with the error'd lines highlighted in red.
My Page code is:
and my controller code is (with the lines giving the error highlighted in red:
Appreciate all the help!
When the class is constructed, BegDate is null, so when stringToDate gets called to initialize DayDate, the null gets passed to stringToDate, which will then end up throwing an NPE because it assumes s is not null.
Well how would I work around this? Of Course BegDate is null at first, it's going to be entered by the user. should I assign an initial value to BegDate?
You could do many things, have stringToDate handle a null string, not assign a value at construction time, etc. it'll depend what exactly you're trying to do.
The logic behind the visual force page and controller is this:
The user will go to the Page with the intent to view a weeks worth of the schedule. They will be prompted to enter a date that will serve as the date from which the view begins. So if they entered today's date, they would see the schedule starting from today and going 6 more days.
Since the date won't be saved to an SObject, the input field is just an input text area, I can't require a date. So, instead I'll require that the user enters a date in the form dd/mm/yyyy. That will be read as text and then converted with the stringToDate function.
As you see, the problem I'm having is that Salesforce is compiling the controller before the user gets a chance to be prompted and is therefore attempting to manipulate a null field throwing the null reference exception.
I'm not sure how I'd code around this, based on what I've said and the code above, could you give me a suggestion? Thanks!