转发时出错[java.lang.NullPointerException]

时间:2012-02-01 16:59:40

标签: java jsp struts

我在struts1.3.8中使用Netbeans 6.9和java脚本,点击提交按钮后出现错误 这是netbeans输出:

[WARNING: Unhandled Exception thrown: class java.lang.NullPointerException].

错误消息:

javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

这是我的代码

jsp页面:

<html:form action="/adminTask" onsubmit="return checkCheckBoxes(this);" >

        <table>
            <tr>
            <td align="left">Select your task:</td></tr>
            <tr>
                <td align="left">
                    <input type="checkbox" id="taskElement1" name="task" value="add" onchange="toggleStatus()"/>Add<br/>
                    <input type="checkbox" id="taskElement2" name="task" value="edit" onchange="toggleStatus()"/>Edit<br/>
                    <input type="checkbox" id="taskElement3" name="task" value="delete" onchange="toggleStatus()"/>Delete<br/>
                </td>
            </tr>
            <tr>
                <td align="left"><br/>Select what you want to do the task on:</td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" id="objectElement4" name="object" value="customer" onchange="objectStatus()"/>Customer<br/>
                    <input type="checkbox" id="objectElement5" name="object" value="supplier" onchange="objectStatus()"/>Supplier<br/>
                    <input type="checkbox" id="objectElement6" name="object" value="category" onchange="objectStatus()"/>Category<br/>
                    <input type="checkbox" id="objectElement7" name="object" value="brand" onchange="objectStatus()"/>Brand<br/>
                    <input type="checkbox" id="objectElement8" name="object" value="product" onchange="objectStatus()"/>Product<br/>
                </td>
            </tr>
            <tr>
                <td><br/><html:submit value="Go" /></td>
            </tr>
        </table>

    </html:form>

豆子:

public class adminTaskForm extends org.apache.struts.action.ActionForm {

private String task;
private String object;

public String getTask() {
    return task;
}

public String getObject() {
    return object;
}

public void setObject(String object) {
    this.object = object;
}

/**
 * @param string
 */
public void setTask(String task) {
    this.task = task;
}


public adminTaskForm() {
    super();
    // TODO Auto-generated constructor stub
}

动作类:

公共类checkadminTaskAction扩展org.apache.struts.action.Action {

private static final String FAILURE = "failure";

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    //Initilized Action error
    ActionErrors errors = new ActionErrors();
    // set the session on in the page
    HttpSession session = request.getSession(true);
    //Call LoginForm Instance here
    adminTaskForm formBean = (adminTaskForm) form;
    // extract user data
    String task = formBean.getTask();
    String object = formBean.getObject();
    System.out.println(task);
    System.out.println(object);

    //Set up flag message
    boolean adminFlag = false;
    String taskFlag;
    String objectFlag;

    //set up address & adminTask
    String address = null;
    String adminTask = null;

    // perform validation
    if (task == null || // task parameter does not exist
            task.length() < 1) // task parameter is empty
    {
        errors.add("task", new ActionMessage("errors.task.required"));

    }
    if (object == null || // object parameter does not exist
            object.length() < 1) // object parameter is empty
    {
        errors.add("object", new ActionMessage("errors.object.required"));

    }

    //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
    saveErrors(request, errors);
    //Check- is errors empty
    if (errors.isEmpty()) {
        adminFlag = true;
    } else {
        adminFlag = false;
    }

    if (adminFlag == true) {

        // start admin tasks
        System.out.println("////////////////////////// start checkadminTasksAction /////////////////////////");

        /// if the admin selected add task
        if (task.equals("add")) {
            taskFlag = "add";
            /// if the admin selected add customer
            if (object.equals("customer")) {
                objectFlag = "customer";
            } /// if the admin selected add supplier
            else if (object.equals("supplier")) {
                objectFlag = "supplier";
            } /// if the admin selected add category
            else if (object.equals("category")) {
                objectFlag = "category";
            } /// if the admin selected add brand
            else if (object.equals("brand")) {
                objectFlag = "brand";
            } /// if the admin selected add product
            else {
                objectFlag = "product";
            } 
            /// set the session to pass values
            session.setAttribute("task", task);
            session.setAttribute("object", object);
            System.out.println(task + "ing " + object);

        }/// if the admin selected edit task
        else if (task.equals("edit")) {
            taskFlag = "edit";
            /// if the admin selected edit customer
            if (object.equals("customer")) {
                objectFlag = "customer";
            } /// if the admin selected edit supplier
            else if (object.equals("supplier")) {
                objectFlag = "supplier";
            } /// if the admin selected edit category
            else if (object.equals("category")) {
                objectFlag = "category";
            } /// if the admin selected edit brand
            else if (object.equals("brand")) {
                objectFlag = "brand";
            } /// if the admin selected edit product
            else {
                objectFlag = "product";
            } 

            /// set the session to pass values
            session.setAttribute("task", task);
            session.setAttribute("object", objectFlag);

        }/// if the admin selected delete task
        else if (task.equals("delete")) {
            taskFlag = "delete";
            /// if the admin selected delete customer
            if (object.equals("customer")) {
                objectFlag = "customer";
            } /// if the admin selected delete supplier
            else if (object.equals("supplier")) {
                objectFlag = "supplier";
            } /// if the admin selected delete category
            else if (object.equals("category")) {
                objectFlag = "category";
            } /// if the admin selected delete brand
            else if (object.equals("brand")) {
                objectFlag = "brand";
            } /// if the admin selected delete product
            else {
                objectFlag = "product";
            } /// if the admin selected nothing

            /// set the session to pass values
            session.setAttribute("adminTask", taskFlag);
            session.setAttribute("taskObject", objectFlag);

        }
        if(adminTask.equals("add")){
            request.getRequestDispatcher("/insert" + object + ".jsp").forward(request, response);
        }
        else if(adminTask.equals("edit") ||
                adminTask.equals("delete")){
            request.getRequestDispatcher("/updateObjects.jsp").forward(request, response);
        }
        else {
            errors.add("forward", new ActionMessage("errors.forward"));
                System.out.println("error forwarding");
        }

    }

    return mapping.findForward(FAILURE);
}

}

0 个答案:

没有答案