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
lapaullapaul 

build a custom edit page using apex class and vForce page

HI,

I'm trying to create a custom edit page using Apex class to retrieve the fields information and then display

the fields in vForce page. However I got the following error message in the process. Any help would be

appreciated. It's very helpful with some custom edit page sample codes. Thanks

 

Paul

 

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

 

 

 

 

 


GoodGrooveGoodGroove

Paul,

 

please post your code, so we can help. 

 

Rdgs 

kool_akool_a
You probably forgot to initialize a variable and you tried to assign a value to it.... post your code to get a more reliable answer.
lapaullapaul

Thanks for replying. here's my code. I'm not sure if I did this right or not. please advice. 

 

 public PageReference edit_action()

{

 

Time_Tracking__c Ed = [SELECT OwnerId, Project__c, Status__c, Date__c, Department__c, Start_Time__c, Stop_Time__c, Time_Code__c from Time_Tracking__c WHERE id =: ApexPages.currentPage().getParameters().get('id') limit 1];

 

timetr.OwnerId = Ed.OwnerId;

timetr.Project__c = Ed.Project__c;

timetr.Status__c = Ed.Status__c;

timetr.Date__c = Ed.Date__c;

timetr.Department__c = Ed.Department__c;

timetr.Start_Time__c = Ed.Start_Time__c;

timetr.Stop_Time__c = Ed.Stop_Time__c;

timetr.Time_Code__c = Ed.Time_Code__c;

 

PageReference pg = Page.TimeTrackEditPage;

pg.setRedirect(true);

 

return pg;

 

cpetersoncpeterson

Do you declare the variable timetr anywhere in your code? If not that would seem to be the problem.

 

Try:

 

public PageReference edit_action(){ Time_tracking__c timetr = new Time_tracking__c();//the rest of your code

 It also looks like timetr isn't being saved or passed anywhere. Is that intended?

 

 

kool_akool_a

If timetr was not declared the error would be different, in fact it would not compile. I think the issue might be that your query is not returning any records, hence its null. You might want to validate the id before using it in the query or make sure the result is not null before you start using it.

 

Not sure why you are passing the id in as a query string. There cleaner ways of getting the id if the link or button is from a detail page of the sobject.

kool_akool_a

I took a further look at your code and "Ed" seems like a throw away. why not just

 

 

public PageReference edit_action() { timetr = new Time_Tracking__c(); //assuming timetr already declared somewhere if((ApexPages.currentPage().getParameters().get('id') != null) && (ApexPages.currentPage().getParameters().get('id') != '')) { timetr = [SELECT OwnerId, Project__c, Status__c, Date__c, Department__c, Start_Time__c, Stop_Time__c, Time_Code__c from Time_Tracking__c WHERE id =: ApexPages.currentPage().getParameters().get('id') limit 1]; } PageReference pg = Page.TimeTrackEditPage; pg.setRedirect(true); return pg;

 

 

 

 

lapaullapaul

Thanks for all your suggestions.

I have tried all your suggestions above and it's still not working. I'm surprised that there are no

sample codes similar to my situation here. I can find sample codes easily for .Net but not for

Salesforce. I think there will be more people is going to run into the custom edit page issues down

the road.

 

thanks

 

kool_akool_a

The error you get "System.NullPointerException: Attempt to de-reference a null object" has nothing to do with what you are trying to do. This error is due to that fact that some where in your code you are trying to use an object that is null, maybe the object is not initialized yet or you are assigning this object to null. 

 

You need to debug this problem properly, what line does this error happen on? post area around this line. 

 

This seems like a simple bug to fix. Don't get discouraged.