-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: java.lang.NullPointerException about the list.add
PostPosted: Mon Apr 09, 2007 9:13 pm 
Newbie

Joined: Mon Apr 09, 2007 8:54 pm
Posts: 3
Hibernate version:3.01
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"; "

Full stack trace of any exception that occurs:
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!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 09, 2007 11:38 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi wang_11o1,

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

this should be
List<Object> list =nee ArrayList<Object>();

Try to learn Java then come to Hibernate

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 2:04 am 
Newbie

Joined: Mon Apr 09, 2007 8:54 pm
Posts: 3
thanks a lot!
Dharmendra Pandey

I have change it ,but failure trace is changing too,as following :
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(Unknown Source)
at library.dao.hibernate.OCounterDaoHibernateImpl$2.doInHibernate(OCounterDaoHibernateImpl.java:60)
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:47)
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 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)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 6:51 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi wang_11o1,

check the size of your Object[] result array before handling with content. The size (result.length) might be 0 and you're accessing result[0] and result[1].

chucky

Don't forget to rate if this helps


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 7:32 pm 
Newbie

Joined: Mon Apr 09, 2007 8:54 pm
Posts: 3
I find the point:
it's the
[code]int i=Integer.parseInt(result[0].toString());[/code]
if I change it to
[code]int i=0;list.add(i,result[1]); i++;[/code]
it's ok!
why?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 11, 2007 5:35 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi wang_11o1,

The exception
Code:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0


tells us that you are trying to insert an element in the list at index 1 right after you've created it (size 0). So the list doesn't have the index 1 yet.
That means the code
Code:
int i=Integer.parseInt(result[0].toString());

was returning 1 and the next line:
Code:
list.add(i,result[1]);

will try to insert the content of result[1] at index 1 (not yet available).

But if you do it like you've done:
Code:
int i=0;
list.add(i,result[1]);
i++;

the content of result[1] at index 0 (available) and you increment i (so next index will be also available).

Hope you understand now what you're doing.

chucky

Don't forget to rate if this helps
--------------------------------------


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.