• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

simple question: get,set methods using arrays.

I am using Beans and JSP to build a webpage.

The user selects more than one item from the listbox and clicks submit. For eg: If he selects vendor,Model,ID three different listboxes with Vendor, Model and ID should appear in the next page.

What I was trying to do is pass the selected values in the listbox and collect them in the bean and then query the database:
String Multicol_lst[]; ---- this is the name of the listbox from where the user makes multi search.

public void setMulticol_lst[](String Value) {
      for(int i=0;i<Multicol_lst.length;i++) {
      int j=1;
      String str[j] = request.getParameter(Multicol_lst(i));
      System.out.println("in array" +str[j]);
      j++;
      }
      this.Multicol_lst[i] = Value;
      }
      public String getMulticol_lst[](String Value) {
            return this.Multicol_lst[];
            }
When I compile this I am getting an error : '( ' expected
and pointing to the line---------- public void setMulticol_lst[](String Value) {

can you please tell me how I should get this to working and return the result?

Thanks,
Nubee

0
nubee
Asked:
nubee
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
kiranhkCommented:
>>>>>public void setMulticol_lst[](String Value) {

should be
public void setMulticol_lst(String[] Value)
0
 
kiranhkCommented:
String str[j] = request.getParameter(Multicol_lst(i));

public void setMulticol_lst(String[]  Value) {

     for(int i=0;i<value.length;i++)
     {
         System.out.println("in array" +str[i]);
         this.Multicol_lst[i] = Value[i];
     }
     public String[] getMulticol_lst()
     {
          return this.Multicol_lst;
      }
0
 
kiranhkCommented:
>>>>> String str[j] = request.getParameter(Multicol_lst(i));
sorry, this should be

String str[] = request.getParameter("Multicol_lst"));
bean.setMulticol_lst(str);

Assuming your list box name is "Multicol_lst" in the JSP page and also you need to pass this array to your setMulticol_lst () method of your bean
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ManishLeadCommented:
String str[]=request.getparameterValues("Multicol_lst");
0
 
ManishLeadCommented:
String str[]=request.getParameterValues("Multicol_lst");
0
 
nubeeAuthor Commented:
Thanks for your response,

String[] Multicol_lst;

public void setMulticol_lst(String[] Value) {
      String str[] = request.getParameterValues(Multicol_lst);
      for(int i=0;i<Value.length;i++) {
            System.out.println("in array" +str[i]);
      }
}
public String[] getMulticol_lst() {
      return this.Mutlicol_lst;
      }

And in MsearchResults.JSP:

<%
String str[] = request.getParameterValues("Multicol_lst");
     Simple_SearchBean.processRequest(request,session);
     Simple_SearchBean.setMulticol_lst(str);

I am able to compile the bean with no errors, but If I reach the page ResultPage.jsp, I am receiving nullpointerException.

java.lang.NullPointerException
        at PlanningBeans.Simple_SearchBean.setMulticol_lst(Simple_SearchBean.jav
a:51)
        at examples.jsp.Planning_0005fdatabaseSearch.MsearchResults_jsp_1._jspSe
rvice(MsearchResults_jsp_1.java:103)

I am wondering if I have to use Indexed property in the Bean to access the array? Am I using Indexed property or somethng else? PLease help

Thanks,
aruna
0
 
kiranhkCommented:
can you post your jsp also....
i think there is problem with your jsp
0
 
nubeeAuthor Commented:
*******There is nothing fancy in my jsp. There are hardly more than 2 lines of code in it.
<HTML>
<HEAD>
<TITLE>
MsearchRESULTS
</TITLE>
</HEAD>
<BODY>
<form method=GET action="MainPage.jsp">
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>

<jsp:useBean id="Simple_SearchBean" scope="session" class="PlanningBeans.Simple_SearchBean" />

<jsp:setProperty name="Simple_SearchBean" property="*" />

<%      
String str[] = request.getParameterValues("Multicol_lst");
     Simple_SearchBean.setMulticol_lst(str);
         %>

</BODY>
</HTML>
************BEANS***Before i posted wrong code in the beans. These are the ones for which I am getting the above posted error.*************

public void setMulticol_lst(String[] Value) {
 for(int i=0;i<Value.length;i++) {
     this.Multicol_lst[i] = Value[i];
}
}
public String[] getMulticol_lst() {
     return this.Multicol_lst;
   }
0
 
kiranhkCommented:
this code seems ok....
what is the jsp which is forwarding to this Jsp..i mean the jsp where u have defined your list box.....
0
 
nubeeAuthor Commented:
HTML>
<HEAD>
<TITLE>
Search PAGE
</TITLE>

</HEAD>
<BODY>
<form method=GET action="MainPage.jsp">

<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>

<jsp:useBean id="Simple_SearchBean" scope="session" class="PlanningBeans.Simple_SearchBean" />
<jsp:setProperty name="Simple_SearchBean" property="*" />


<%
     Simple_SearchBean.processRequest(request,session);
     Vector Logicalnm_vec = new Vector ((Vector) Simple_SearchBean.getLogicalnm_vec());
     Vector Col_vec = new Vector ((Vector) Simple_SearchBean.getCol_vec());

     ListIterator iter_ln = Logicalnm_vec.listIterator();
     ListIterator iter_col = Col_vec.listIterator();

     
%>

<b>Simple Search</b> <br/>
Search here by logical name and all information related to that logical name will be displayed.
                          <br />
                          <br />
Select the Logical Name from the list provided.                          
</br>                  
Logical name or server name:

<select name="Logicalnm_lst" >       
<%        
     while (iter_ln.hasNext()) {
          String value = (String)iter_ln.next();
%>
         <option value = "<%=value%>"><%=value%></option>
<%
     }
     
%>
</select>  

</br>
</br>
<INPUT TYPE="submit" name="simplesearch" value="Submit">
<input type=reset value="Reset" >

</br>
<Pre>

<b>Multiple Search</b>

<SELECT NAME="Multicol_lst" SIZE=3 MULTIPLE>
               <%        
                 while (iter_col.hasNext()) {
                      String value = (String)iter_col.next();
            %>
                     <option value = "<%=value%>"><%=value%></option>
            <%
                 }
                 
            %>
</select>  
         <INPUT TYPE="submit" name="multiplesearch" VALUE="Submit">
</pre>
</BODY>
</html>
0
 
kupra1Commented:
No... your code is not okay. You are getting NullPointerException because in your method
public void setMulticol_lst(String[] Value) {
//
}
you have a String[] Value which should be populated first. It is coming as null and then you are doing a cascade operation like
for(int i=0;i<Value.length;i++) { .... }
Since Value is coming as null and you are calculating the length of a null object, you are getting the NullPointerException.
To avoid it, first check for null and then do the loop. So add something like this:
if(Value!=null) {
 for(int i=0;i<Value.length;i++) {
 .....
 }
}

But this will solve your problem partially, I presume. The values you are setting and passing by introspection, so the names of the request parameters and that of bean class variables should exaclty match. Hence, use the actual request parameter name instead of Value or populate the String[] Value in the bean class first before calling the set method.


The easiet way to track your jsp error is to look into a folder named as work inside your application/web server. Inside the work folder, there will be a folder for your webapp, which will contain the corresponding java class of your jsp page. Open that java class of your jsp in a text editor and go to the required line of the exception.

If you have any questions, do ask me.
Thanks
0
 
nubeeAuthor Commented:
Hello kupra1,

I tried to check the null before attempting to loop, but I am still getting the same error.
So, I changed the bean to see if the value is returning at all .

String Multicol_lst;

public void setMulticol_lst(String Multicol_lst) { this.Multicol_lst=Multicol_lst;       }

public String getMulticol_lst() {      return this.Multicol_lst;      }

public void processRequest(HttpServletRequest request,HttpSession session)throws ServletException,IOException {
System.out.println("listbox results"+request.getParameter("Multicol_lst"));
}

***********In the JSP************
I jsut used this statement:
Simple_SearchBean.processRequest(request,session);
**********************************
In the list box there will ID, Vendor, Model etc and I selected the first three and hit the submit button. It took me to the nextpage and in the comand prompt it printed just the first item "ID". So this confirms that I am using exact variables in both Bean and JSP.
Can you tell how can I modify the get and set methods to read all the items selected in the listbox and return them .

Thanks,
Nubee
0
 
kupra1Commented:
first a little clarification: why r u doing this?
<jsp:setProperty name="Simple_SearchBean" property="*" />

 when you are explicitly setting like this.

String str[] = request.getParameterValues("Multicol_lst");
Simple_SearchBean.setMulticol_lst(str);

Just to check, print out the length of String str[]. Is it 3 or 1? If it's 1, then there is something wrong with the way values are getting passed on clicking the submit button.

Waiting for your reply.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now