0

I am working on a Spring-MVC appplication with Hibernate and PostgreSQL in which these days we are experiencing a very strange problem. Sometimes when an object is being persisted, I am returning it's ID from the DAO layer to the service layer. This ID is then being broadcasted via our PUSH framework, after which point the object is retrieved by a call. At this point I am getting a NPE, although the object is saved, primary-key ID is non-zero, session is also flushed before returning the ID. What might be going wrong?

Example :

@Override
public void addNestedGroupNoteComment(String commentText, int noteId, int commentId){
    int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId);

    if(saveId!=0) {
        notification.setCommentId(saveId);
        this.chatService.sendNotification(notification, groupMembers.getMemberid());
    }

DAO method :

@Repository
@Transactional
public class GroupNoteHistoryDAOImpl implements GroupNoteHistoryDAO {

    private final SessionFactory sessionFactory;


    @Autowired
    public GroupNoteHistoryDAOImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public int addGroupNoteComment(GroupNoteHistory noteHistory, int noteId) {
        Session session = this.sessionFactory.getCurrentSession();
        GroupNotes notes = (GroupNotes) session.get(GroupNotes.class, noteId);
        if(notes!=null) {
            notes.getGroupNoteHistorySet().add(noteHistory);
            noteHistory.setMhistory(notes);
            int saveId = (Integer) session.save(noteHistory);
            session.flush();
            return saveId;
        }
    }
}

Now, after broadcasting the ID, this method is called :

@Override
public GroupNoteHistory getGroupNoteHistoryById(int historyId) {
    GroupNoteHistory groupNoteHistory = this.groupNoteHistoryDAO.getGroupNoteHistoryById(historyId);
    // Above object is many times null, tried System.out, it was with non-zero values.
}

DAO method :

@Override
public GroupNoteHistory getGroupNoteHistoryById(int historyId) {
    Session session = this.sessionFactory.getCurrentSession();
    return (GroupNoteHistory) session.get(GroupNoteHistory.class, historyId);
}

Any thoughts?

Update

Error log :

HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

root cause

java.lang.NullPointerException
com.project_name.spring.service.GroupNoteHistoryServiceImpl.getGroupNoteHistoryById(GroupNoteHistoryServiceImpl.java:156)
sun.reflect.GeneratedMethodAccessor647.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
4
  • Is the entity in the database? Commented Oct 25, 2016 at 8:57
  • @PeterGelderbloem : That's the problem, it's supposed to be there as I am getting its ID back, but that's not how it seems to be working. Commented Oct 25, 2016 at 9:24
  • @WeareBorg Could you post the exception and GroupNoteHistory entity? At which line is the NPE thrown? Commented Oct 25, 2016 at 10:32
  • @Atul : Please check bottom of main post, its thrown at service layer after getting the object and trying to process it. Commented Oct 25, 2016 at 10:39

2 Answers 2

1

Here int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId); you already have a GroupeNoteHistory.

After this you manipulate it and save it

        noteHistory.setMhistory(notes);
        int saveId = (Integer) session.save(noteHistory);
        session.flush();
        return saveId;`

So you must check if the GroupNoteHistory already exists and is not null before using it. That's why you get a NPE.

Sign up to request clarification or add additional context in comments.

2 Comments

You must investigate on the reason why the GroupeNoteHistory in argument of int saveId = this.groupNoteHistoryDAO.addGroupNoteComment(groupNoteHistory, noteId); is null
Actually, the object is getting saved, that's why we are getting non-zero ID back from the DAO method, but later when sometimes its called, we are getting a null object. Only sometimes... strange behavior...This is what we are trying to inspect. Any ideas?
0

Finally this worked for me :

  @Override
    public int addGroupNoteComment(GroupNoteHistory noteHistory, int noteId) {
        Session session = this.sessionFactory.openSession();
        try {
            session.beginTransaction();
            GroupNotes notes = (GroupNotes) session.get(GroupNotes.class, noteId);
            if(notes!=null) {
                notes.getGroupNoteHistorySet().add(noteHistory);
                noteHistory.setMhistory(notes);
                 session.save(noteHistory);
                session.flush();
                session.getTransaction().commit();
                return noteHistory.getMhistoryid();
            }
        } catch (Exception e) {
          e.printStackTrace();
        }finally {
            session.close();
        }
        return 0;
    }

4 Comments

You should not catch all Exceptions...It's a bad practice. You should test the nullity and log an error message. And for sure manipulate the object noteHistory only if it's not null ;-)
Or you can throw an IllegalArgumentException if noteHistory is null for example.
@Ruddy : The problem is we need assurance that once the user adds a comment, its persisted, no matter what.... We need to know any errors happening as soon as possible to fix...Also, this is just for test instance, for live instance, we are pushing it to our monitoring tool.. :-)
Ok I understand ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.