• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Preventing Cast Exception

The code below throws an error at the line:

owner = (Owner) session.load(Owner.class, new Long(id));

whenever an int value is passed to the getOwner method.

How do I modify this method to accept both int and long primitives in the method.
public static Owner getOwner(long id)
    {
    	Owner owner = null;
        if (id > 0)
        {
            PersistorSession session = null;
            try
            {
                session = PersistorSessionFactory.getInstance().getPersistorSession();
                owner = (Owner) session.load(Owner.class, new Long(id));
            }
            catch (PersistenceException pe)
            {
                logger.error("Persistence error loading the following Owner object id from memory: " + id, pe);
                pe.printStackTrace();
            }
            catch (Exception e)
            {
                logger.error("Error loading the following Owner object id from memory: " + id, e);
                e.printStackTrace();
            }
            
            finally
            {
                if (session != null)
                {
                    try
                    {
                        session.close();
                        session = null;
                    }
                    catch (PersistenceException pe)
                    {
                        logger.error("Error occured while closing persistor session", pe);
                        pe.printStackTrace();
                    }
                }
            }
        }
        return owner;
    }

Open in new window

0
onaled777
Asked:
onaled777
2 Solutions
 
sweetfa2Commented:
What is the full text of the exception you are getting.  Are you sure the exception is on the int or the return value from session.load?
0
 
stachenovCommented:
Int value is perfectly convertible to long. Moreover, it is converted as soon as it's passed to the getOwner() method, so once inside, it is a full-fledged long already. sweetfa2 is right, the only place in that line where a cast exception could occur is the cast to Owner. If you think it has anything to do with ints, perhaps session.load() returns something incompatible with Owner when passed a small enough value?
0
 
onaled777Author Commented:
Thank you. the issue was found by stepping into the session.load.  The cast that was causing the error was found in there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now