3

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.

Aleksei Budiak
  • 871
  • 5
  • 16
Lacters
  • 33
  • 3
  • you need to debug using some breakpoints.... its look like this: ***vivienda.getName();*** is returning null – ΦXocę 웃 Пepeúpa ツ Aug 29 '17 at 05:27
  • 1
    What is inside of your `repository` class? This behaviour could be explained if you're comparing strings with `==` operator inside of `repository`. – Aleksei Budiak Aug 29 '17 at 05:36
  • 2
    "_But, remember the exception?_" Well, no since you didn't provide it. ;)Are you sure there is no space or anything ? Please provide a [mcve] for this exception. Use `vivienda.getName().trim()` just in case. – AxelH Aug 29 '17 at 05:38
  • The exception I'm talking about is in both pieces of code I provided, it's a simple throw new 404. And yup, that's right, the problem was in using == instead of equals, totally forgot about that. – Lacters Aug 29 '17 at 17:30

1 Answers1

1

If this code works

repository.getViviendas("vivienda1");

while the following doesn't

repository.getViviendas(name); // name is "vivienda1"

it could mean that String comparison is done incorrectly inside of repository.

If you're declaring two Strings as literals then comparison using == operator will be successful since they'll be stored in a String pool. But if the value of one of those strings comes in dynamically (like from your RESTful API client) then it will not work.

Please read What is String pool in Java? for more details on String pool in Java.

The correct way to compare Strings in Java is .equals() method:

someString.equals(otherString);
Aleksei Budiak
  • 871
  • 5
  • 16