I have a bit of an interesting problem, when I preform a rerender with a command button, variables get unset for some reason.

I have a Class named UploadedFile:

public with sharing class UploadedFile
public Blob FileData {Get;set;}
public String FileName {get;set;}

public UploadedFile(Blob newBlb, String newStr)
    FileData = newBlb;
    FileName = newStr;

public UploadedFile(){}

//gets the file extension from a file
//returns null if file does not have an extension
public string getFileExtension()
    if(FileName == null || !FileName.contains('.'))
        return null;
    String[] strParts = FileName.split('\\.');
    return(strParts[strParts.Size() - 1]);

public boolean hasFile()
    if(FileData == null || FileName == '' || FileName == null)
        return false;
    return true;

This is my Constructor for my visualforce page: ShowPicUploads is a global variable

public SubmitalInstalExt() {
    showPicUploads  = new UploadedFile[NUMBER_OF_UPLOADS];
    for(Integer i = 0; i < showPicUploads.Size(); i++)
        showPicUploads[i] = new UploadedFile();

Whenever I use the following command button:

<apex:actionRegion >
   <apex:commandButton value="Save" action="{!SaveIt}" reRender="PopUps" /> 

I get a null pointer reference error at the following code in SaveIt:

 for(UploadedFile file:showPicUploads)
      if(!checkUpload(file, SUPPORTED_FILE_TYPES))
            retVal = false;

Specifically, it says that the for statement has a null pointer exception:

for(UploadedFile file:showPicUploads)

The only way that could be true is if showPicUploads is null, but I set it in the constructor.

Am I not understanding something about reRender?


The issue was that I was using the Transient keyword for ShowPicUploads. Transient makes it so the variable is not saved to the viewstate. So I suppose when the rerender happened it was not part of the varriables SaveIt had access to, therefore causing the null pointer exception

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.