#NullPointerException when checking for null value

1 messages · Page 1 of 1 (latest)

thorny pawn
#

private void verwerkKeuze1() { List<ArtikelDTO> artikelDTOS = domeinController.geefArtikelsUitBestellingMetBestelNummer( geefKeuze("Geef bestelnummer: ",1000, 10000)); if (artikelDTOS != null) { drukAfOpScherm(artikelDTOS); } else { System.out.print("Geen artikels aanwezig"); } }

Why am I getting a NullPointerException when I'm checking for a null value. This Exception is only obviously only thrown when artikelDTOS is null.

nimble snowBOT
#

<@&987246399047479336> please have a look, thanks.

viscid stirrup
#

you ar echeking if the list is null or not, maybe its elements are null or it is empty?

thorny pawn
viscid stirrup
#

just like an empty string "" is not null

#

you can do something like artikelDTOS != null && !artikelDTOS.isEmpty()

thorny pawn
viscid stirrup
#

yes so then it shouldnt execute drukAfOpScherm(artikelDTOS); but instead print the debug message. Does it not do that?

thorny pawn
viscid stirrup
#

can you send the full stacktrace of the exception?

thorny pawn
#

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.isEmpty()" because "dtos" is null
at cui.BestellingApplicatie.drukAfOpScherm(BestellingApplicatie.java:50)
at cui.BestellingApplicatie.verwerkKeuze1(BestellingApplicatie.java:45)
at cui.BestellingApplicatie.start(BestellingApplicatie.java:29)
at main.StartUp.main(StartUp.java:12)
Disconnected from the target VM, address: '127.0.0.1:50653', transport: 'socket'

Process finished with exit code 1

viscid stirrup
#

can you also send the drukAfOpScherm1 method?

thorny pawn
#

private void drukAfOpScherm(List<ArtikelDTO> dtos) { for (ArtikelDTO dto : dtos) { System.out.printf("Artikel %s in %c, padnr %d. Weegt %.3f kg. %s.%n", dto.artikelCode(), dto.gang(), dto.padnr(), dto.gewicht(), dto.status()); } }

#

I'll be back in 20m

viscid stirrup
#

seems really odd. Maybe you can workaround it by adding a null check in the drukAfOpScherm1 method, but it should work as it is now

thorny pawn
#

Thanks for taking the time though.

viscid stirrup
thorny pawn
viscid stirrup
#

oh well at least it works