File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting List class from session

 
shaf maff
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I create a List<MyClass>, do whatever I need to do and then insert it into a session:



My question is how do I retrieve it ? I have tried the following and it doesnt work:





.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's "doesn't work" mean?

And yes, you should be able to retrieve the scoped variable in that way.
 
shaf maff
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, should have been more specific. I do the following to get my Lis<class> session:



The first problem here is the complier gives me a note saying "java uses unchecked or unsafe operations" on the get session bit.

When I compile using javac class.java -Xlint it gives me the following error:



The second problem is the for each loop doesn't work. Everything compiles but I keep getting a 500 interal error.

[ September 07, 2008: Message edited by: shaf maff ]

[ September 07, 2008: Message edited by: shaf maff ]

[ September 07, 2008: Message edited by: shaf maff ]
[ September 07, 2008: Message edited by: shaf maff ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compilation warnings are irrelevant. What are the details of the 500 error?
 
shaf maff
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the 500 error:

java.lang.NullPointerException
Get.doGet(Get.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

This is line 140:

for(MailSession mail : test) {

BTW, what does the note/warning mean and why ignore it ?



.
 
li bei
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpSession getMailSession = request.getSession(false);
List<MailSession> test = ( List<MailSession> ) checkSession.getAttribute("MailSession");


You typed it , or just copy and paste?

You named the reference "getMailSession", but use another reference "checkSession".
 
li bei
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(MailSession mail : test) {
StringBuilder xml = new StringBuilder();
xml.append(mail.getLetter());
}

And this, maybe there is something wrong.

you defined the StringBuilder "xml" in the loop.

it should be outside, I guess.
 
shaf maff
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by li bei:
HttpSession getMailSession = request.getSession(false);
List<MailSession> test = ( List<MailSession> ) checkSession.getAttribute("MailSession");


You typed it , or just copy and paste?

You named the reference "getMailSession", but use another reference "checkSession".
I typed it. Ignore the small errors. The main message was to outline the struture of the code/how Im doing things..
 
shaf maff
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by :
[QB][/QB]
Sorry it was a typo when typing up a more summerised version. Im sure you wouldnt want the full 600-700 line code.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shaf maff:
I typed it. Ignore the small errors. The main message was to outline the struture of the code/how Im doing things..

Please don't waste people's time with sloppy posting. The "small errors" as you call them can be the source of your errors or not. People trying to help you don't know what's real and what's just a result of your unwillingness to put any effort into your questions.

People who volunteer their time to help others will quickly grow tired of such sloppiness and just go on to help others who are putting more effort into their posts.
 
Sam Aran
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shaf maff:
Here is the 500 error:

java.lang.NullPointerException
Get.doGet(Get.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

This is line 140:

for(MailSession mail : test) {

BTW, what does the note/warning mean and why ignore it ?



.

What this suggests to me is that for some reason the session is not returning a List (that is, the attribute returned is null, because mail being null would not throw an NPE) Test is probably null. This may be because it is a different Session object somehow. Your best bet is to store some kind of identifier value into the session (as a string) and then look for this value at this line with a debugger. If that value is not a Session attribute, then you do indeed have a different session for the current request and as such may not have the information you require.

As for the warnings, the reason you *have* to ignore this is that the Servlet API has been here for awhile, especially before the Java 5 change. Therefore, it is backwards compatible (for the most part) with previous versions. getAttribute returns Object, which is what it has to return, naturally (you can store anything) but the compiler now warns you that you're doing a potentially unsafe cast (casting it to your List<special> type) because the only safe cast in that situation is a method defined as returning (directly) a List<special> instance.

The other one is a SerializableUID warning, which Eclipse can solve instantly for you (add default serializable id) which creates a private static final long serialVersionUID = 1L; This is because your class implements the Serializable interface, which recommends that a class provide an identifier for its version when being serialized (otherwise, this will be generated by the JVM).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic