Link to home
Start Free TrialLog in
Avatar of nubee
nubee

asked on

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

Avatar of kiranhk
kiranhk

>>>>>public void setMulticol_lst[](String Value) {

should be
public void setMulticol_lst(String[] Value)
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;
      }
SOLUTION
Avatar of kiranhk
kiranhk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Manish
String str[]=request.getparameterValues("Multicol_lst");
String str[]=request.getParameterValues("Multicol_lst");
Avatar of nubee

ASKER

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
can you post your jsp also....
i think there is problem with your jsp
Avatar of nubee

ASKER

*******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;
   }
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.....
Avatar of nubee

ASKER

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>
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nubee

ASKER

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
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.