0

I am trying to get data from LivEx table as list, and then save that data into two dimensional array called array. My method is returning object[][].

I made up this code from googling here and there and taking bits from every example, however I am doing something wrong which I can't quite seem to put my finger on. It gives me an exception which is a NullPointerException whenever I call getLivExList(currentPlan).

Here is my code:

public static Object[][] getLivExList(String currentPlan) throws Exception {
    Session s = null;

    try {
        s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();
        String query = "select F.item, F.category, F.amount from LivEx F where F.planName=?";
        Query q = s.createQuery(query);
        System.out.println(query);
        List fp = q.list();
        s.getTransaction().commit();
        Object array[][] = new Object[fp.size()][3];
        for (int i = 0; i < fp.size(); i++) {
            for (int j = 0; j < 3; j++) {
                array[i][j] = fp.get(j +(3 * i));
            }
        }
         System.out.println("getLivExdata in networthentitymanager OKAY");
        return array;
    } catch (Exception e) {
         System.out.println("problem at getLivExdata in networthentitymanager");
         throw e;
    } finally {
        if (s != null) {
            HibernateUtil.closeSession();
        }
    }

Please help me find out where the problem is coming from.

This is the exception I am getting:

    SEVERE: null
    org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [select F.item, F.category, F.amount from LivEx F where F.planName=?]
    0 problem 
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:319)
    at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:275)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:75)
    at com.elitconsultancy.finplanner.entity.NetWorthEntityManager.getLivExList(NetWorthEntityManager.java:213)
5
  • We aren't psychic... Please indicate which line of the code you posted you are getting the NPE, eg insert a line before saying // NPE on next line Commented Oct 24, 2011 at 5:13
  • @Bohemian check the edit please that is the exception i am getting Commented Oct 24, 2011 at 5:32
  • 2
    To be brutally honest: if you build this by "googling here and there and taking bits from every example" then that part is what you're doing wrong: You must learn about the technology you are using and not simply string together parts of (probably half-wrong) examples all around the web. That's a surefire way to disaster. Commented Oct 24, 2011 at 6:31
  • 1
    Query.list() will return a List<Object[]>. fp.get(j +(3 * i)) is wrong. It should be ((Object[]) fp.get(i))[j] Commented Oct 24, 2011 at 6:33
  • I agree with Joachim. Hibernate has an extremely well written reference documentation, which is exhaustive and full of examples. Read it. docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single Commented Oct 24, 2011 at 6:35

2 Answers 2

2

You're not setting the plan name for the query, even 'though you've specified a positional parameter (?).

Put this before your call to list:

query.setString(0, currentPlan);
Sign up to request clarification or add additional context in comments.

Comments

0
List fp = q.list();         
s.getTransaction().commit();         
Object array[][] = new Object[fp.size()][3]; 

you are getting size of list. If list returned by q.list() is null, that is no matching rows found then you will get Null pointer exception.

Thats the first thing i see that might be wrong in the code

before invoking methods on list, u can check whether it is null or not.

2 Comments

-1. Query.list() never returns null, as should any method returning a List. And this is not the cause of the exception. Just read the message from the exception.
@JBNizet stack trace was not available when I answered the question...anyways thanks for pointing out

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.