How to retrieve DTO from wrapper method?

I have one getter in method and I’m wrapping getName with nameTransform method to make it check for validation that I need

public InvoiceRequestDTO orderTransformer(OrderDTO orderDTO)  {

        return new InvoiceRequestDTO(nameTransform(orderDTO.getName());

    }

And one method for checking

private String nameTransform(String name) {
        if (orderDTO.getName().length() < 1) {
            orderDTO.setName("0000" + orderDTO.getName());
        }
        if (orderDTO.getName().length() < 2) {
            orderDTO.setName("000" + orderDTO.getName());
        }
        if (orderDTO.getName().length() < 3) {
            orderDTO.setName("00" + orderDTO.getName());
        }
        if (orderDTO.getName().length() < 4) {
            orderDTO.setName("0" + orderDTO.getName());
        }
        else orderDTO.getName();

        return nameTransform(name);
    }

How to retrieve orderDTO in the method nameTransofm? When I’m doing

OrderDTO orderDTO = new OrderDTO();

It gives me null pointer exception

When i have understood your intents correctly, this should do the trick:

private String nameTransform(OrderDTO orderDTO) {
    String name = orderDTO.getName();
    String newName;
    int length = name.length();

    if (length < 1) {
        newName = "0000" + name;
    }
    if (length < 2) {
        newName = "000" + name;;
    }
    if (length < 3) {
        newName = "00" + name;
    }
    if (length < 4) {
        newName = "0" + name;
    } else {
        return name;
    }
    orderDTO.setName(newName);
    return nameTransform(orderDTO);
}

but i am not sure why you need the recursion in the last line

Your orderTransform would now look like this:

public InvoiceRequestDTO orderTransformer(OrderDTO orderDTO)  {
    return new InvoiceRequestDTO(nameTransform(orderDTO);
}

when you use new OrderDto(), it has no name field thats why you get nullpointer.

return new InvoiceRequestDTO(OrderDto.nameFormatted());

class OrderDto {
.... 
    public String nameFormatted(){
        // check name
        if(this.name == null) {
             return "";
        }

        return String.format("%4s", this.name).replace(' ', '0');
    }
}