aspose file tools*
The moose likes Servlets and the fly likes why is the List returning empty? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why is the List returning empty?" Watch "why is the List returning empty?" New topic
Author

why is the List returning empty?

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi folks ,
Im developing a web app where im trying to pass the value of a list from the database through a DAO class onto a servlet which displays it in a jsp when called , however im not being able to call the function which generates the List in the servlet as a result a null List is returned .any ideas why this is happening ?
My servlet class generating the list.

Thanks in advance

Learning and Learning!-- Java all the way!
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi,
Did you try printing the contents of the "domList" in the servlet?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

I did by doing this

However it threw a NPE while accessing domList..
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi,
That does mean your DataObject needs to be debugged as it sends you the List.

Regards,
Amit
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

well my DataObject is the DAo object and the associated function for getting the records is as below

Am I missing something here?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

It's not obvious to me why you need the method setDomList() since you always create a new list in the getDomList(). Also, why does domList need to be an instance variable?


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hello,
What does this print ?
System.out.println("The size of domList is"+domList.size());
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

James Sabre wrote:It's not obvious to me why you need the method setDomList() since you always create a new list in the getDomList(). Also, why does domList need to be an instance variable?

Well I was using domList as a bean instance variable and hence the getter and setters.I wanted to access it in my jsp through a bean.
And for amit's q.. thats not being printed , I guess the control never reaches that statement
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Yeah.
You debug that thing and you should be fine after fix.

Regards,
Amit
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

amit punekar wrote:Yeah.
You debug that thing and you should be fine after fix.

Regards,
Amit

Im trying to figure out how to debug here , any help would be appreciated. THE DAO object"DataObject ' is created and called , thats pretty starightforward. Ive inserted logging pretty much everywhere i could be going wrong, checked the DB however the List is still empty
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hello,
I can think of two things here -
1) Check if the query that is printed from your program if it returns you any results.
2) If yes then then check your DB connection details to confirm if you are connecting to the same database on which you ran the query #1.

Regards,
Amit
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

I also have a question about your the following Servlet code

Did you mean to do the following?

It seemed weird that you were trying to set the domainType of a variable that is not show where it is declared or what it does. It is also strange that you change its value after you send the request dispatch forward.


OCJP6, OCWCD5
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Mike Zal wrote:

It seemed weird that you were trying to set the domainType of a variable that is not show where it is declared or what it does. It is also strange that you change its value after you send the request dispatch forward.

Well Mike,
I wouldnt be able to directly implement your code snippet as I cannot access the setDomainType_Oid through DataObject as SetDomainType_Oid is setter method of the instance variable bean , so i would need to do something like
DataObject.bean.setDomainType_Oid(domainType_Oid);
which is syntatically wrong . Any ideas on how I could go about doing it?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi,
All Mike is trying to point out possible error in the way you are calling the function setXXX() and not syntactically.
You seems to be using "domainType_Oid" as part of string concatenation in your SQL. If this is true then shouldn't you be setting this value before you invoke "getDomList()" which uses this value as mentioned above.

Regards,
Amit
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Have you verified any of the following:
domainType_Oid is not equals to 0 or negative one
Are you successfully connecting to the database
Did the query return any value?

I think your problem is that you are not setting the domainType_Oid variable (it explains why you are receiving a NullPointerException). Can you add some debug to print the value of domainType_Oid to the system log before the first if statement?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477


hi Mike and Amit,
Thank you for your replies. Iwill try to explain where i could be going wrong here..


amit punekar wrote:Hi,
All Mike is trying to point out possible error in the way you are calling the function setXXX() and not syntactically.
You seems to be using "domainType_Oid" as part of string concatenation in your SQL. If this is true then shouldn't you be setting this value before you invoke "getDomList()" which uses this value as mentioned above.

Regards,
Amit

I didnt quite understand this.
Mike Zal wrote:Have you verified any of the following:
domainType_Oid is not equals to 0 or negative one
Are you successfully connecting to the database
Did the query return any value?

I think your problem is that you are not setting the domainType_Oid variable (it explains why you are receiving a NullPointerException). Can you add some debug to print the value of domainType_Oid to the system log before the first if statement?

Well , I did the check for domaintype_oid in my DAO class , however that code is not executed . I do know that im being able to connect to the database .Because I populate a drop down in my jsp through a dynamic list from the database.But As far as getting this list goes it returns only a NULL , im not being able to figure out why , imo its not even reaching the DB code ,ie. DAO class.
Awaiting your replies.
venkat prathap
Greenhorn

Joined: Jun 06, 2011
Posts: 7

Hey,

Can you share me exception log you are getting...
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

There was no exception log . Because the list is empty
Joseph Mokenela
Ranch Hand

Joined: Jan 18, 2011
Posts: 60

All the statements that are inside the

is there any one of them which is printing?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Nope. They are not being printed either.
Joseph Mokenela
Ranch Hand

Joined: Jan 18, 2011
Posts: 60

The problem could also be that your ResultSet is empty, have you checked that your query returns the results from the database?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Tried the query in the DB , and works perfectly there .returns the expected result set.
Joseph Mokenela
Ranch Hand

Joined: Jan 18, 2011
Posts: 60

what about the very first if statement, .
Did you check whether your domainType_Oid satisfies the conditions in the if statement?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Yes im printing the value of domainType_Oid in the servlet and the if condition is satsfied before entering the loop.
Joseph Mokenela
Ranch Hand

Joined: Jan 18, 2011
Posts: 60

Did you check that your connection is not null? does this statement print?

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Yes it does print!But Any idea where im going wrong?
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Vic, could you please post exactly which debug statements are being printed and which ones are not being printed. After reading your posts, it seems like you are seeing the following debug

After the echoed query there is no other output. Is this correct?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Mike, the exact debug statements are as follows
the various points where these statements occur are--

Hope that helps
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Not really since none of the debug you printed seems to match what is inside the getDomList() method. Either your System.out.print statements are printing somewhere else or the code is never being executed. Can you try making a small change to your code and see if you see any of the new debug.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Mike Zal wrote:Not really since none of the debug you printed seems to match what is inside the getDomList() method. Either your System.out.print statements are printing somewhere else or the code is never being executed. Can you try making a small change to your code and see if you see any of the new debug.

Yep you'd guessed right.The domList is set to null
Heres the debug code..Please help on how I could get it to work


Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Since "Skipped All Processing in DataObject class" appeared in your debug, it means that domainType_Oid equals 0 or -1. You need to set that variable after you instantiate the DataObject but before you call getDomList().
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Mike,
You were right . The value of Domaintypeoid is being set to zero(checked by logging) . The thing is im being able to retrieve the required value of domaintypeoid in my servlet , how do I pass the same value to my DAO class , so that i can set the value of domaintype-oid before the if loop is executed?
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

In the Servlet, you could set the DAO object as a request attribute. In your JSP, you could just use the request.getAttribute instead of instantiating a new DAO object.
rav pad
Greenhorn

Joined: Jun 07, 2011
Posts: 3
Vic,
Can you suppply the log of the whole thing starting from the first if
if(domainType_Oid!=0 && domainType_Oid!=-1){} because I see that the log which you have already supplied does not match with the System.Out.Println given in the code.

Also place the execute query in a System.out.println so that we will know whether a resultset is getting generated or not.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Folks,
Solved this for now .
The thing is i was creating a local variable to generate the List , however was not instantiating it with the value returned from the DAO class.
 
 
subject: why is the List returning empty?