same function working differently when called from two separate functions with same parameters [duplicate]

╄→гoц情女王★ 提交于 2019-12-13 23:51:41

问题


The same function originalStrutsPortletAction.processAction( originalStrutsPortletAction, portletConfig, actionRequest, actionResponse) is working fine when called from one function but not working when called from another function.Although the parameter values are same in both calls. Th code is as follows:-

public class ExampleLoginAction extends BaseStrutsPortletAction implements Serializable{

    public static int flag=0;
    public  static StrutsPortletAction originalStrutsPortletAction=null;
    public  static PortletConfig portletConfig=null;
    public  static ActionRequest actionRequest=null;
    public  static ActionResponse actionResponse=null;
    String email;
    @Override
    public void processAction(StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse) throws Exception {
        /**
         * This is the custom process action
         * Once you try to login this method will be invoked
         * We can write our own logic here
         * Invoke the original struts action at the end
         */
         ExampleLoginAction.actionRequest=actionRequest;
         ExampleLoginAction.originalStrutsPortletAction=originalStrutsPortletAction;
         ExampleLoginAction.portletConfig=portletConfig;
         ExampleLoginAction.actionResponse=actionResponse;

        System.out.println("#############ExampleLoginAction###############");
        //System.out.println(ParamUtil.getString(arg0, "login"));
        // TODO Auto-generated method stub
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        //SecurityManager security = System.getSecurityManager();
        //System.out.println("Security Manager" + security);
        Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("abhijain.cse@gmail.com","a17432475J@");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("adesh.pathak01@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(ParamUtil.getString(actionRequest, "login")));
            message.setSubject("Testing Subject");
            int otp=new Random().nextInt(10000);
            message.setText("Your OTP is " + otp);
            OTP otpdemo=OTPLocalServiceUtil.createOTP(CounterLocalServiceUtil.increment());
            otpdemo.setEmail(ParamUtil.getString(actionRequest, "login"));
            otpdemo.setPassword(String.valueOf(otp));
            OTPLocalServiceUtil.addOTP(otpdemo);
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        //portletConfig.getPortletContext().getRequestDispatcher("/jsp/otp.jsp").forward(actionRequest, actionResponse);
        //actionResponse.setRenderParameter("email",ParamUtil.getString(actionRequest, "login"));
        //actionRequest.setAttribute("password",ParamUtil.getString(actionRequest, "password"));
        String email=ParamUtil.getString(actionRequest, "login");
        this.email=email;
        actionRequest.getPortletSession().setAttribute("email",email);
        HttpServletRequest request=PortalUtil.getHttpServletRequest(actionRequest);
        HttpSession session1=request.getSession();
        session1.setAttribute("loginAction", this);
        actionResponse.sendRedirect("/otpsample-hook/jsp/otp.jsp?email="+email+"&originalStrutsPortletAction="+originalStrutsPortletAction+"&actionRequest="+actionRequest+"&actionResponse="+actionResponse+"&portletConfig="+portletConfig+"&ExampleLoginAction="+this);
        System.out.println("In processAction"+originalStrutsPortletAction);
        System.out.println("In processAction"+portletConfig);
        System.out.println("In processAction"+actionRequest);
        System.out.println("In processAction"+actionResponse);
        System.out.println("bye:::::::::");
        //Thread.sleep(1000);
        System.out.println(flag);
        if(flag==1)
        {
            System.out.println("hello:::::::::");
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, actionRequest,
           actionResponse);
        //flag=0;

        }
        //new MVCPortlet().getPortletContext().getRequestDispatcher("/jsp/otp.jsp").forward(actionRequest, actionResponse);


    }

    public String render(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws Exception {

        /**
         * Our own render method
         * This method is for rendering the view
         * At the end call the original struts action
         */

        System.out.println("##########Rendering view############");
        renderResponse.getWriter().println("custom rendering of page");
       // return null;
        return originalStrutsPortletAction.render(
           null, portletConfig, renderRequest, renderResponse);
       // return null;

    }

    public static void check() throws Exception
    {
        System.out.println(":::::hi::::::");
        System.out.println(originalStrutsPortletAction);
        System.out.println(portletConfig);
        System.out.println(actionRequest);
        System.out.println(actionResponse);
        originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                 portletConfig, actionRequest,
                        actionResponse);

    }
}

The function

originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                 portletConfig, actionRequest,
                        actionResponse)

when called from processAction() works fine but when it is called inside check() method I am getting null pointer exception although the parameter values for both are same. I have checked the parameter values by printing them in both functions.They are same. So where could be the mistake? I got stuck in it for one day but could not find the solution.The stack trace is follows:-

java.lang.NullPointerException
    at com.liferay.portlet.login.action.LoginAction.login(LoginAction.java:196)
    at com.liferay.portlet.login.action.LoginAction.processAction(LoginAction.java:93)
    at com.liferay.portal.struts.StrutsPortletActionAdapter.processAction(StrutsPortletActionAdapter.java:60)
    at com.liferay.portal.kernel.struts.BaseStrutsPortletAction.processAction(BaseStrutsPortletAction.java:44)
    at com.abhishek.ExampleLoginAction.check(ExampleLoginAction.java:196)
    at org.apache.jsp.jsp.otpCheck_jsp._jspService(otpCheck_jsp.java:90)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at sun.reflect.GeneratedMethodAccessor482.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
    at com.sun.proxy.$Proxy642.doFilter(Unknown Source)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:165)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.PortalClassLoaderFilter.doFilter(PortalClassLoaderFilter.java:74)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:204)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:109)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

回答1:


That's probably because there are some dependencies that are resolved differently in each case, depending on who is calling this method.

In some frameworks, you have something like a state, when request come then the server initializes some variables, for instance a session object for the particular user in which you can store some information. If you call the same method from other Thread without the session being resolved against any user then you have no session at all and it could fail.

But this is not restricted to session, there could be other variables you are using that are initialized (or not) depending on who is calling the method.



来源:https://stackoverflow.com/questions/39409739/same-function-working-differently-when-called-from-two-separate-functions-with-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!