• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

SpringBoot Application Throws NullPointerException

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Not sure why I'm getting NullPointerException when I try to run my SpringBoot application. Everything else seems to be on point.
 
Marshal
Posts: 80294
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you initialise userService?
 
Mayank Rawat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I do that?
I just wanted to go about creating a DB where new user details would be created, stored, manipulated.

Here is the UserService interface:


Here is the implementation class:
 
Mayank Rawat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As much as I'm ashamed to be posting this query here, I really would like to understand what is going wrong. Could be some configuration issue with my IDE.
 
Mayank Rawat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive used Lombok annotations for constructor injection.

 
Campbell Ritchie
Marshal
Posts: 80294
434
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us the annotations. Moving you to our Spring forum, since this appears not to be a direct Java® problem.
 
Saloon Keeper
Posts: 28494
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a peek at my flagship Spring Boot app, and I think this is how it needs to be (from my code)

The @Inject annotation does the initialisation. Without it, categoryRepository is null, just as all Java object references are by default.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Inject is JEE / JakartaEE / Quarkus. In Spring (Boot) you use @Autowired, but constructor injection should also work, even with Lombok's @AllArgsConstructor. If it doesn't I'd check to see if Lombok actually runs. You can easily test this by making the field final; without a constructor that sets it (which Lombok should provide) the compiler will complain that it's not set.
 
Tim Holloway
Saloon Keeper
Posts: 28494
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:@Inject is JEE / JakartaEE / Quarkus. In Spring (Boot) you use @Autowired, but constructor injection should also work, even with Lombok's @AllArgsConstructor. If it doesn't I'd check to see if Lombok actually runs. You can easily test this by making the field final; without a constructor that sets it (which Lombok should provide) the compiler will complain that it's not set.


Spring Boot hosts the JEE Tomcat server, so I'd recommend @Inject over @Autowired. I believe that Spring Boot is providing that function as a built-in via Weld, as it's not part of Tomcat.

That's assuming I didn't get it as an automatic add-on when I built a Spring Boot JavaServer Faces app.

Traditonally, I'd be using @Autowired alongside JSF's @ManagedBean, but @Inject can deal with both now.
 
Mayank Rawat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Issue resolved, thank god.

I changed the Spring configuration from 3.4.1 to 3.0.0.
Still, am not clear about why this helped run the application now. I am providing the old pom.xml(No Success) and the latest pom.xml(SUCCESS). Could somebody please help me out as to how and why this change worked my way?


Old pom.xml(No Success) -



New pom.xml(SUCCESS) -
 
Bartender
Posts: 2445
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I heard we are not recommended to use milestone versions because those versions are not stable.
Have you try those stable versions without the milestone?

Do you put @Autowired to UserRepository?
 
Mayank Rawat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. @Autowired not used.
 
Himai Minh
Bartender
Posts: 2445
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean @Autowired is an annotation in Spring for dependency injection.
@Inject is from a Java library to do dependency injection as well.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic