Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
NPE when using map {} on ArrayList containing NULLs

I'm getting

java.lang.NullPointerException: Attempt to invoke virtual method 'float java.lang.Number.floatValue()' on a null object reference

on following code:

val localHistory: ArrayList<Float> = ArrayList<Float>()
...    
val strHistory = localHistory.map { value -> decoration.decoratedValue(ref.format, value) }

I've just learned that ArrayList may contain nulls (oookay). That would mean that the value in the map transforming closure may be NULL, no?

But this cannot be since the value is not an optional type and the compiler says that if (value != null) will be always true.

So the question is how to avoid NPE in this case?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I suggest marking every parameter in a Collection you receive from Java as nullable (unless of course you know it can never be null:

val floats: List<Float?> = someJavaClass.getFloats()

Then you can filter out the nulls and do your map operation:

val newFloats: List<Float> = floats
    .filter { it != null }
    .map { it + 1 }

Keep in mind that every filter, map (and so on) operation will iterate the whole collection and create a new on everytime. You're better of using Sequences, which are iterated lazily.

As mentioned in the comments one may use filterNotNull to directly filter out all null values:

val newFloats: List<Float> = floats
    .filterNotNull()
    .map { it + 1 }

And mapNotNull to only keep values which map-function doesn't return null, this also eliminates the filterNotNull:

val newFloats: List<Float> = floats
    .mapNotNull { it?.plus(1) }

Another construct I can think of which works with your example though it is highly unreadable and just complex. But here it is:

val strHistory = localHistory
    .mapNotNull { decoration.decoratedValue(ref.format, it ?: return@mapNotNull null) }
over 4 years ago · Santiago Trujillo Relatório

0

If you just want to exclude the null values in the ArrayList, call mapNotNull instead of map. See here for details.

You then also need to handle the case where the value is null inside your lambda, which you can do with the ?.let as shown below:

localHistory.mapNotNull { value -> value?.let { decoration.decoratedValue(ref.format, it) } }
over 4 years ago · Santiago Trujillo Relatório

0

One more with mapNotNull:

val strHistory = localHistory.mapNotNull { it ?: return@mapNotNull null
    decoration.decoratedValue(ref.format, it) }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda