1

This method is working fine. Here I'm fetching max bookid from book table but I'm getting null pointer exception when database is null. So, how to handle this exception.

public void insertData() {        
            Transaction tx = null;
            Session session = getSession();
            try {
                tx = session.beginTransaction();
                Book book = new Book();
                SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
                List<Book> rows = query.list();
                for (Object row : rows) {
                    book.setBookid(Integer.parseInt(row.toString()));
                    Integer id1 = book.getBookid() + 1;                
                    selbookid = id1;
                }        

                book.setBookname("Java");            
                book.setDateofcreation(new Date());
                book.setLastmodifieddate(new Date());           

                session.save(book);
                tx.commit();
                session.flush();
                session.close();
            } catch (RuntimeException e) {   
                e.printStackTrace();
            } 
        }
3
  • "when database is null", what more precisely do you mean by that? If you know what is null, tell us, if you don’t, try using your stack trace or your debugger to find out. Commented Apr 2, 2016 at 12:03
  • where occurs the NullPointerException? In the method getSession? Then you should check and handle it there? For me it is unclear, what your problem is... Commented Apr 2, 2016 at 12:05
  • What you mean database is null....books table is null? Can you update stack trace? Commented Apr 2, 2016 at 14:09

2 Answers 2

1

Your question is not clear but what can I see in your question you are saying if database is null then only you are getting execption.

I check this method in my system and found why you are getting exception because in SQL query you are fetching max value of bookid from books table but if database is null then how will be fetch max value so you need to do some changes in query. You should use IFNULL condition like:

select IFNULL(MAX(bookid),1) from books

It is working for me even database is null because inserting bookid 1 if books table is empty.

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

Comments

1

When your query returns a single row/column value, use uniqueResult() instead of list().

Hope the exception raises when there are no rows in table. I've made few changes to your code.

Try this

       public void insertData() 
       {        
        Transaction tx = null;
        Session session = getSession();
        int result =0;
        try {
            tx = session.beginTransaction();
            Book book = new Book();
            SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
           try
           {
            result = (int)query.uniqueResult();   
           }
           catch(Exception e)
           {               
           //do whatever you want with exception
           }
            book.setBookid(result+1);
            book.setBookname("Java");            
            book.setDateofcreation(new Date());
            book.setLastmodifieddate(new Date());           

            session.save(book);
            tx.commit();
            session.flush();
            session.close();
        } 
        catch (RuntimeException e) {   
            e.printStackTrace();
        } 
       }

Comments

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.