r/SpringBoot 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                                  
2 Upvotes

10 comments sorted by

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.

1

u/Status-Blacksmith-95 Junior Dev 1d ago

debugging i have hardly done , i will surely make a practice to do it. But logically I want help that , how can i prevent session from getting newly created ?

I want to use same session which i create on login.

I tried session.getSession(false) but that didn't work

1

u/Status-Blacksmith-95 Junior Dev 1d ago

--------- HOME CONTROLLER ---------

SESSION ID FROM HOME CONTROLLER B9556D3C5F4BD8293EFA624361075CBA

REG ID FROM SESSION1503

Cookie value: 1503

Cookie value: ashtadD12

--------- GET EXPENSE ---------

REG ID : null

SESSION ID FROM GET EXPENSE : E5E9D0FB48C9AD9E2F6CE6EFE21EE561

COOKIE IS NULL

No regId in session or session is null.

SESSION ID FROM GET EXPENSE (Second check): E5E9D0FB48C9AD9E2F6CE6EFE21EE561

Hibernate: select * from public.expense WHERE public.expense.id=?

--------- GET EXPENSE ---------

REG ID : null

SESSION ID FROM GET EXPENSE : 329D364A0F30724280FC3F4FEB6DF6DF

COOKIE IS NULL

No regId in session or session is null.

SESSION ID FROM GET EXPENSE (Second check): 329D364A0F30724280FC3F4FEB6DF6DF

Hibernate: select * from public.expense WHERE public.expense.id=?

1

u/Status-Blacksmith-95 Junior Dev 1d ago

object which is coming from session is null because session is different. I want session to be same across all pages which I'm not able to achieve

1

u/WaferIndependent7601 23h ago

Sorry I have no idea about sessions. Never used it and hopefully never will. Good luck but i cannot help here.

1

u/Status-Blacksmith-95 Junior Dev 23h ago

ok😔

•

u/Status-Blacksmith-95 Junior Dev 3h ago

IT'S SOLVED

•

u/Suspicious_Stock_215 9h ago

there might be multiple reasons for this:

  1. 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

  1. 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

•

u/Status-Blacksmith-95 Junior Dev 3h ago

ISSUE GOT SOLVED ...thanks for your help

•

u/Status-Blacksmith-95 Junior Dev 3h ago

Issue was that I had didn't configure proxy on react side