I have a simple for with a text input field, submit button and select
components
in the form.

There is a corresponding ActionForm and Action class.

If I don't populate the ActionErrors object in validate, everything works
great.

If I do populate the ActionErrors object, I get a NullPointerException in
the browser like so:

java.lang.NullPointerException
        at
org.apache.struts.taglib.html.OptionsTag.getIterator(OptionsTag.java:366)

Not that it matters, but I am validating the text input field only.

If I comment out the select component in the JSP page and put an invalid
entry in
the text field, the error shows up fine.

I don't see the relationship between these two things.

Here is the code in its entirety.

Much obliged.

David

----------------------------------------------------------------------------
-------------------------------------------

<struts-config>

  <!-- ========== Form Bean Definitions ===================================
-->
  <form-beans>

    <!-- meter form bean -->
    <form-bean      name="meterForm"
                    type="rwebstruts.MeterForm"/>

  </form-beans>


  <!-- ========== Global Forward Definitions ==============================
-->
  <global-forwards>
    <forward   name="meter"                path="/meter.jsp"/>
  </global-forwards>


  <!-- ========== Action Mapping Definitions ==============================
-->
  <action-mappings>

    <!-- Process a MeterHost addition/removal -->
    <action    path="/meter"
               type="rwebstruts.MeterAction"
               name="meterForm"
              scope="request"
              input="/meter.jsp">
    </action>

  </action-mappings>

</struts-config>

----------------------------------------------------------------------------
---------------------------------------------------------

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html:html locale="true">
<head>
<title><bean:message key="logon.title"/></title>
<html:base/>
</head>
<body bgcolor="white">

<html:errors/>

<html:form action="/meter">
<table border="0" width="100%">

  <tr>
    <th align="right">
      <bean:message key="prompt.host"/>
    </th>
    <td align="left">
      <html:text property="host" size="16" maxlength="16"/>
    </td>
  </tr>

  <tr>
    <th align="right">
      <bean:message key="prompt.port"/>
    </th>
    <td align="left">
      <html:text property="port" size="6" maxlength="6"/>
    </td>
  </tr>

  <tr>
    <td align="right">
      <html:submit property="add" value="Add"/>
    </td>
    <td align="left">
      <html:submit property="remove" value="Remove"/>
    </td>
  </tr>

  <tr>
        <td align="right">
                <html:select property="selectedHost" size="10">
                        <html:options property="list"/>
                </html:select>
        </td>
  </tr>

</table>

</html:form>

</body>
</html:html>



----------------------------------------------------------------------------
-------------------------------------------


package rwebstruts;


import javax.servlet.http.HttpServletRequest;
import java.util.*;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public final class MeterForm extends ActionForm {

    private String host = null;

    private String port = null;

    private ArrayList list;

        private String selectedHost = null;


        public MeterForm() {
                list = new ArrayList();
                ConfProp props = new ConfProp();
                Vector v = props.getMeterHosts();
                for (int i=0; i<v.size(); i++)
                        list.add(v.elementAt(i));
                setList(list);
        }


    public String getHost() {

        return (this.host);

    }


    public void setHost(String host) {

        this.host = host;

    }


    public String getPort() {

        return (this.port);

    }

    public void setPort(String port) {

        this.port = port;

    }

    public ArrayList getList() {
                return list;
    }

    public void setList(ArrayList list) {
                this.list = list;
    }

    public String getSelectedHost() {
                return selectedHost;
    }

    public void setSelectedHost(String selectedHost) {
                this.selectedHost = selectedHost;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {

        this.port = null;
        this.host = null;
        this.list = null;

    }


    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

                if (request.getParameter("add") != null) {
                if (!validatePort(port)) {
                        errors.add("port", new
ActionError("error.port.invalid"));
                }
        }

        return errors;

    }

        public boolean validatePort(String inPort) {

                boolean valid = true;

                try {

                        int value = Integer.parseInt(inPort);
                        if (value > 65535 || value < 1) valid = false;


                } catch (NumberFormatException nfe) {

                        valid = false;
                        
                }

                return valid;

        }


}



----------------------------------------------------------------------------
----------------------------------------------

package rwebstruts;


import java.io.IOException;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;

public final class MeterAction extends Action {

        ConfProp props = null;

        public MeterAction() {
                super();
                props = new ConfProp();
        }


    public ActionForward perform(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws IOException, ServletException {

        MeterForm mfrm;
        if (form==null)
                mfrm = new MeterForm();
        else
                mfrm = (MeterForm)form;

        String action = request.getParameter("add");
        if (action != null) {
                String meterPort = mfrm.getPort().trim();
                String newHost;

                // the port is optional
                if (meterPort.equals(""))
                        newHost = mfrm.getHost().trim();
                else {
                        newHost = mfrm.getHost().trim() + ":" + meterPort;
                }

                newHost = newHost.toLowerCase();

                boolean found = false;

                Vector mMeterHosts = props.getMeterHosts();
                for (int i=0; i<mMeterHosts.size(); i++)
                        if
(((String)mMeterHosts.elementAt(i)).equals(newHost)) {
                        
                                found = true;
                                
                        }

                        if (!found) {

                                props.addMeterHost(newHost);

                                mfrm.setHost("");
                                mfrm.setPort("80");

                        }

        }

        action = request.getParameter("remove");
        if (action != null) {

                String goneHost = mfrm.getSelectedHost();

                props.removeMeterHost(goneHost);

        }


        // populate text area
        ArrayList al = new ArrayList();
        Vector v = props.getMeterHosts();
        for (int i=0; i<v.size(); i++)
                al.add(v.elementAt(i));
        mfrm.setList(al);
        
        
        return (mapping.findForward("meter"));

    }


}

Reply via email to