Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

setting any object as property in java bean

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ... I am trying to set an object(DOG) as parameter in my bean ... but i am getting NullPointerException.. here is the code
// BeanTest.JSP


//ProcessBean.JSP


//test package and inside tht two classes named Test and Test1




now at runtime i am getting NullPointerException.... i have read in book that for only Strings and primitives automatically casting occurs not for any others....but what if i would like to do that ???
 
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is there Java code in your JSP?
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because there is no other way to put instance of the class Test2 in the request..
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this article for information on properly structuring web apps. Any data prep should happen in the page's servlet controller. Java code in JSPs has been discredited for over 9 years now.
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i read the article and got understood that we should not mix scripts with JSP .......... but my question here is what if i use bean and that bean has property that is an object...obviously i can not take that object as input, i have to construct it.. so here i want to know that how can i use it with bean ??...
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I got lost there. Are you asking how to construct an object in Java? Or how to pass it to a setter method of a JavaBean? Or what?

And is this relevant to JSP? If you're using JSTL -- and you should be -- then it can use bean properties if the bean's class is designed with suitable getter methods. But JSTL can't call the setter methods because that isn't what a JSP is for. Was that part of your question?
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One correction: the JSTL <c:set> tag can be used to set bean property values, but it's certainly something that's a rare need in a proper JSP page.
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my question is i want to set an object of some type as the property in my bean and also i want to get it from bean to show it.. [see code above. from that you will come to know what i am trying to achieve]

now here on the other page i am trying to set the property of the bean to that object type .... i know that implicit casting is done in case of String and primitives only while bean's setters and getters are called..but if there is any bean which has another object as a property then how can we use it because here we have to explicitly provide cast....

 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a cast? If the bean setter accepts a certain type, and the object is of that type, what's the problem?

And again, why isn't this something being handled in the controller before the page is invoked?
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



i think you are not getting what i want to do ... i am doing this to learn this i am not an expert. so i have not used any controller like thing....
here i just want to set and get the property "sample".....sample is an object of Test2 class.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying to learn, why are you not trying to learn the proper ways of doing in things rather than how things were done 10 years ago? It's the equivalent of trying to learn electronics by studying vacuum tubes.

Scriptlets and <jsp:setProperties> are dinosaurs. Much better to learn modern JSTL and EL.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

The first thing to understand is that the only communication between client and server is via strings.
The client sends request parameters to the server.
The server sends back a text response. Can be HTML, XML, JSON, whatever.

In your use case there are two request/response transactions

Request#1: BeanTest.jsp - the server sends back an html page with a form on it.
Request#2: generated from submitting the form on BeanTest.jsp, and goes to ProcessBean.jsp

Do you see your problem yet?
You are setting a request attribute as part of BeanTest.jsp
You are trying to retrieve that request attribute as part of ProcessBean.jsp.

They are two seperate requests, and so the request attribute is not available.

If you want a java object to be retained across multiple requests, you should be using a Session attribute rather than Request.

Otherwise you have to work out a method to translate from Object --> String --> Object and pass it as a request parameter.

That is where a framework can come in handy, as it can do the heavy lifting involved there.

So in short:
To pass a java object in this manner you either need to
- use the session
- convert it to string, retrieve that string as a parameter, and convert back to java object.

 
Alas, poor Yorick, he knew this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic