Return a copy of "customItems". - Vulnerability - Minor | Community
Skip to main content
Level 2
September 25, 2024
Solved

Return a copy of "customItems". - Vulnerability - Minor

  • September 25, 2024
  • 1 reply
  • 526 views

For the below code getting Vulnerability, can someone assist with what to write instead:

@9944223

    public List<OptionItem> getCustomItems() {

        return customItems;

    }

 

Passing this

  @9944223
  public List<OptionItem> getCustomItems() {
    return List.copyOf(customItems);
  }

  @9944223
  public List<OptionItem> getCustomItems() {
    return customItems != null ? List.copyOf(customItems) : List.of();
  }

 

getting null pointer exception

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

try like this:

 

@9944223
public List<OptionItem> getCustomItems() {
return customItems != null ? new ArrayList<>(customItems) : new ArrayList<>();
}

1 reply

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
September 25, 2024

try like this:

 

@9944223
public List<OptionItem> getCustomItems() {
return customItems != null ? new ArrayList<>(customItems) : new ArrayList<>();
}