java.lang.NullPointerException about the list.add(Int , Object)

I want to get a list of the table VwOdaycount ,the list should contain the fday (taken as the key word of the list) and the fcount(taken as the object of the list).

the HQL is ""select model.fday,model.fcount from VwOdaycount as model where model.fitemId="+itemID +

"and model.fyear=" +year +"and model.fmonth="+month+"order by model.fday asc"; "

using the Hibernate version:3.01

failure Trace is folowing:

java.lang.NullPointerException

at library.dao.hibernate.OCounterDaoHibernateImpl$2.doInHibernate(OCounterDaoHibernateImpl.java:58)

at library.dao.hibernate.OCounterDaoHibernateImpl$2.doInHibernate(OCounterDaoHibernateImpl.java:1)

at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)

at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:334)

at library.dao.hibernate.OCounterDaoHibernateImpl.getDayCounts(OCounterDaoHibernateImpl.java:46)

at library.test.testOCounter.testGetOCounter(testOCounter.java:42)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at junit.framework.TestSuite.runTest(TestSuite.java:208)

at junit.framework.TestSuite.run(TestSuite.java:203)

at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

the code is following:

public List getDayCounts(int itemID, int year, final int month) {

final String queryString = "select model.fday,model.fcount from VwOdaycount as model where model.fitemId="+itemID +

"and model.fyear=" +year +"and model.fmonth="+month+"order by model.fday asc";

return (List) this.getHibernateTemplate().execute(

new HibernateCallback(){

public List doInHibernate(Session session)throws HibernateException{

Query queryObject = session.createQuery(queryString);

Iterator it=queryObject.iterate();

List<Object> list =null;//int i=0;

while(it.hasNext())

{

Object[] result =(Object[]) it.next();

//System.out.println(result[0]);

//System.out.println(result[1]);

int i=Integer.parseInt(result[0].toString());

list.add(i,result[1]);

}

return list;

}

});}

The problem is on the "list.add(i,result[1])",I am sure that the result is not null,but I don't know the reason that it don't work. thanks!

Message was edited by:

wang_11o1

Message was edited by:

wang_11o1

[3555 byte] By [wang_11o1a] at [2007-11-27 0:30:54]
# 1
'list' is null.
hiwaa at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 2
You have declaredList<Object> list =null;But you should also need to initializelist = new ArrayList<Object>();
rym82a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks a lot !I have change it !but it's Failure Trace is following :java.lang.IndexOutOfBoundsException: Index: 1, Size: 0at java.util.ArrayList.add(Unknown Source)what should I do for that?
wang_11o1a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 4
> java.lang.IndexOutOfBoundsException: Index: 1, Size: 0You had accessed your list which was empty.
hiwaa at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 5
The error message told you that you are trying to access an element of an array which outside the range.I think you should check the size of an array before you use it.The array you need to check is result.
rym82a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 6

the result[0]=[1 2],reslut[1]=[2 1]

I change the code to

System.out.println(result[0]);

//list.add(i,result[1]);

the result print [1 2] and have no failure;

if I delelt the "//"before the list ,that is :

System.out.println(result[0]);

list.add(i,result[1]);

the result only print [1] and have a failure of indexoutofboundexception.

why ?are there some problem with list.add()?

wang_11o1a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 7
What do you get if you add this line, which becomesSystem.out.println("result.length=[" + result.length + "]");System.out.println(result[0]);list.add(i,result[1]);
rym82a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 8
1result.length=[2]
wang_11o1a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...
# 9

I find the point:

it's the int i=Integer.parseInt(result[0].toString());

if I change it to int i=0;list.add(i,result[1]);i++;

it's ok!

now it's how to change the object reslut[0] to int.

but how?

wang_11o1a at 2007-7-11 22:34:51 > top of Java-index,Java Essentials,Java Programming...