• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Spring AOP @Around and H2 database, strange behaviour

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm facing a weird problem using Spring-AOP, Spring-Data and H2 in memory database.

This is my aspect:



this is my joinpoint:



Well, the problem is that, the joinpoint is working, matching the findById method, the problem is that playerRepository.findById is returning null (even with an id of an entity that really exists) and I'm facing a null pointer exception.

This is what I see from the logs:

- [ACCESS] data FIND operation: CrudRepository.findById(..) | args: [1]

- [END] method: CrudRepository.findById(..) | result: Optional[MyEntity my properties bla bla bla]

- java.lang.NullPointerException: null


So it's really strange, because I see that the Aspect is working and is retrieving correctly the entity, but when it proceed to the real method then playerRepository.findById return null.

Can you help me to solve this problem?

Thanks!
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand it correctly:



You say that this fragment produces a non-null value and assigns it to the result variable. Am I right?

And then you say that some other code tries to use some other value, and it's null. Right?

Well, the result variable is local to that try-block and it's not visible anywhere else in any other code. And when the try-block terminates, that variable disappears entirely. So that other code with the NPE can't be using that result variable. It must be using something else, and then maybe it's not that surprising that it's null.


 
Kirk James
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it seems I found the problem, I need to return the result in my aspect:



Can someone please explain me what happens under the hood?
 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear that!

As for "what happens under the hood", that isn't what you need to know. You need to know how variables work in Java. Here's a link to a pretty decent tutorial which tells you about that: Variable Scope in Java.
 
Kirk James
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Good to hear that!

As for "what happens under the hood", that isn't what you need to know. You need to know how variables work in Java. Here's a link to a pretty decent tutorial which tells you about that: Variable Scope in Java.



And then you say that some other code tries to use some other value, and it's null. Right?



Well no, it's not what I'm saying or what I tryied to say.

Thanks for your help but I think you misunderstood the point. The behaviour I was facing was not inside the aspect but inside the method that is matching the aspect:

playerRepository.findById(id) was returning null as result, even if the entity exists, so I was facing a null pointer exception: playerRepository.findById(id).orElse(null);.

I'm not sure that it's related to the variables scope, but if you say that for some reason the variable result is needed by the getById method, so probably it could be related to that variable (not specifically about the scope you're telling me), then if it is I want to know the reason why.
 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kirk James wrote:Thanks for your help but I think you misunderstood the point. The behaviour I was facing was not inside the aspect but inside the method that is matching the aspect:



I'm sure I did misunderstand. I assumed that, since you posted the code with the variable which disappeared immediately, you must be using that code for something. Sorry about that.
 
This tiny ad will self destruct in five seconds.
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic