0

When I'm trying to change name of "Screen" I'm getting the error :

Attempt to de-reference a null object Error is in expression '{!save}' in component in page demo: Class.MyController.save: line 11, column 1

The following is my controller page :

public with sharing class MyController {  
   public List<Shortcut__c> Shortcuts{ get{  
   return [SELECT Id, Name, Screen__r.Name, CreatedById FROM Screen__c LIMIT 1000];  
   } set;}
   public Screen__c  NewBox { get; set; }
    public void getValues(){
    NewScreen = [Select Id, Name from Screen__c where Id=:ApexPages.currentpage().getparameters().get('Id')];
 }
   public PageReference save()
{
  update NewScreen;
  return null;
 }
 }

The following is my VF Page :

<apex:page controller="MyController">  
<apex:form >
 <apex:pageBlock > 
 <h1>Edit Screen name</h1>
 <br> Enter Screen Name : <apex:inputText value="{!NewScreen.Name}"/>
  <apex:commandButton action="{!save}" value="Update Name"/></br>
  <apex:pageBlockTable value="{!Shortcuts}" var="c">  
        <apex:column value="{!c.Name}"/>   
        <apex:column value="{!c.Screen__r.Name}"/>  
        <apex:column value="{!c.CreatedById}"/>  
      </apex:pageBlockTable>   
    </apex:pageBlock>   
    </apex:form>
 </apex:page>

thank you for your help

2
  • Which is line no. 11 in your controller ?
    – Jarvis
    Jun 15, 2018 at 10:35
  • update NewScreen;
    – Denis
    Jun 15, 2018 at 11:09

1 Answer 1

3

You need to change the code to initialize NewScreen as follows.

From the constructor, call getValues() which will return NewScreen object.

public with sharing class MyController {  
   public List <Shortcut__c> Shortcuts{ get{  
   return [SELECT Id, Name, Screen__r.Name, CreatedById FROM Screen__c LIMIT 1000];  
   } set;}
   public Screen__c  NewScreen { get; set; }


public MyController()
{
    getValues();
}

public void getValues(){
    NewScreen = [Select Id, Name from Screen__c where Id=:ApexPages.currentpage().getparameters().get('Id')];
 }
   public PageReference save()
{
  update NewScreen;
  return null;
 }
 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .