+ Start a Discussion
SFmaverickSFmaverick 

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); } }

 

 

SuperfellSuperfell

because you've said DayDate is static, but your BedDate variable is not, so its not in scope for the static initialization.

SargeSarge

 

I agree with Simon. Basically static variables cannot access instance variables.

SFmaverickSFmaverick

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.

SFmaverickSFmaverick

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.

 

System.NullPointerException: Attempt to de-reference a null object

Class.ScheduleViewController.stringToDate: line 21, column 29 Class.ScheduleViewController: line 6, column 27 Class.ScheduleViewController: line 1, column 14

 

 

My Page code is:

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
<apex:form>
<apex:inputText value="{!BegDate}"/>
</apex:form>

<apex:outputPanel >
<apex:dataList value="{!TheList}" var="item"> <!-- Gets the list of shifts -->
<table border="0" bordcolor="00000" cellspacing="0" width="80%" bgcolor ="#000000"> <!-- Table that contains 7 day of weektables -->
<tr>
<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>
</tr>
</table>
</apex:dataList>
</apex:outputPanel>
</apex:page>

 

 

 

and my controller code is (with the lines giving the error highlighted in red:

 

 

public class ScheduleViewController {
//Initializes and provides a get method for the user inputted date
public String BegDate { get; set; }

//Initalizes a date field to put the string into for use as a filter criteria
Public 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 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);
}
}

 

 

 

Appreciate all the help!

SuperfellSuperfell

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.

SFmaverickSFmaverick

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?

SuperfellSuperfell

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.

SFmaverickSFmaverick

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!