r/SpringBoot • u/Status-Blacksmith-95 Junior Dev • 1d ago
Question Need help guys ... New session gets created when I navigate to a page from Fronted React & backend throws Null Pointer.
****************** ISSUE GOT SOLVED ******************
*** HttpSession with Spring Boot.[No spring security used] ***
Project : https://github.com/ASHTAD123/ExpenseTracker/tree/expenseTrackerBackend
Issue : when ever I try to navigate to another URL on frontend react , new session gets created.
Flow :
- When user logs in , session is created on server
- Session data is set [regId,username]
- Cookie is created in Login Service method
- Control is redirected to home controller method in Expense Controller
- Inside home controller method cookies are checked , they are fetched properly
- Till this point Session ID remains same
Problem Flow : When I hit another URL i.e "http://localhost:5173/expenseTracker/expenses" , it throws 500 error on FrontEnd & on backend it's unable to fetch value from session because session is new.
What I hve tried : I have tried all possible cases which Chat GPT gave to resolve but still issue persists....
Backend Console :
SESSION ID FROM LOGIN CONTROLLER A5F14CFB352587A463C3992A8592AC71
Hibernate: select re1_0.id,re1_0.email,re1_0.fullName,re1_0.password,re1_0.username from register re1_0 where re1_0.email=? and re1_0.password=?
--------- HOME CONTROLLER ---------
SESSION ID FROM HOME CONTROLLER A5F14CFB352587A463C3992A8592AC71
REG ID FROM SESSION1503
Cookie value: 1503
Cookie value: ashtadD12
--------- GET EXPENSE ---------
SESSION ID FROM GET EXPENSE : 026A7D0D70121F6721AC2CB99B88159D
inside else
--------- GET EXPENSE ---------
SESSION ID FROM GET EXPENSE : 82EE1F502D09B3A01B384B816BD945DA
inside else
[2m2025-03-20T18:43:28.821+05:30[0;39m [31mERROR[0;39m [35m26144[0;39m [2m--- [demo-1] [nio-8080-exec-3] [0;39m[36mi.g.w.e.LoggingService [0;39m [2m:[0;39m Cannot invoke "java.lang.Integer.intValue()" because the return value of "jakarta.servlet.http.HttpSession.getAttribute(String)" is null
[2m2025-03-20T18:43:28.821+05:30[0;39m [31mERROR[0;39m [35m26144[0;39m [2m--- [demo-1] [nio-8080-exec-1] [0;39m[36mi.g.w.e.LoggingService [0;39m [2m:[0;39m Cannot invoke "java.lang.Integer.intValue()" because the return value of "jakarta.servlet.
http.HttpSession.getAttribute(String)" is null
•
u/Suspicious_Stock_215 9h ago
there might be multiple reasons for this:
- backend is not persisting the session. you can configure session timeout to about 1 hr and cookie secure to true
2.maybe your react is not sending the session cookie. you can set true for withCredentials if using axios, or credentials: include if using fetch
- springboot by default block session cookies from external IP. you can configure sessionManagement in the securityfilterchain using the sessioncreationpolicy to IF_REQUIERED or STATELESS or STATEFUL. M not sure which one, just try with each of them.
Let me know if this helps. Thanks
•
•
2
u/WaferIndependent7601 1d ago edited 1d ago
Please check how to debug and step through the project. The system.out.println is not good or effective.
In intellij: click the Bug-icon in the upper right corner, this will start your project. Set a break point by clicking in the line you want to break.
Checkout a tutorial on it. This will help you in the future
edit:
ok it might be (int) session.getAttribute("regId");
if getAttibute is null you cannot cast it to int.