I'm doing a Rest integration project, and I'm trying a PUT api request for an API I'm implementing. This is part of the code that is on the start of the PUT operation implementation.
String name = vivienda.getName();
Vivienda oldVivienda = repository.getViviendas(name);
if (oldVivienda == null) {
throw new NotFoundException("No se ha encontrado ninguna vivienda con el nombre="+ vivienda.getName());
}
Thing is, when I do the request, it returns me the NotFoundException
declared there, so it didn't work.
Playing with the code I localised that the error was that, certainly, the object oldVivienda
was null
, as if I tried to do something with its getName
, it would throw NullPointerException
.
But here's where I start not understanding anything. I changed the code to try and find where the error was, putting the name string directly like this:
Vivienda oldVivienda = repository.getViviendas("vivienda1");
if (oldVivienda == null) {
throw new NotFoundException("No se ha encontrado ninguna vivienda con el nombre="+ vivienda.getName());
}
This works perfectly. This would lead to thinking that the problem is with the parameter object vivienda
, that it's or its name is empty or isn't "vivienda1". But, remember the exception? In the exception I use vivienda.getName()
, and when I see the error thrown in the response, it actually says "vivienda1". So I sent fine the object, and when you do vivienda.getName()
it returns "vivienda1". And when you put the string "vivienda1" in the repository.getViviendas("vivienda1")
, it works perfectly.
So, where is the problem? Is there any problem with types? But getName
is a simple get method that returns a String. And I've checked several times if there was a space somewhere or something. The error is in those 2 first lines of code, but I have no idea what's going on.