Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program is written in struts and I couldn't find the answer anywhere in stackoverflow. While running the program , it is giving 'nullpointerexception' .This is a simple struts login program created in netbeans enviornment.

The code is as follows:


What I have tried:

web.xml
-------

***

*

<welcome-file-list>
<welcome-file>login.jsp


*

login.jsp
---------

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>

<html:errors/>

<html:form action="/Login">
User Name :<html:text name="LoginForm" property="userName"/>
Password :<html:password name="LoginForm" property="password"/>
<html:submit value="Login" />
</html:form>
</body>
</html>


struts-config.xml
=================

<struts-config>
<form-beans>
<form-bean name="LoginForm" type="com.vaannila.LoginForm"/>
</form-beans>

<action-mappings>
<action input="/login.jsp" name="LoginForm" path="/Login" scope="session" type="com.vaannila.LoginAction">
<forward name="success" path="/success.jsp">
<forward name="failure" path="/failure.jsp">




LoginForm.java
--------------

public class LoginForm extends ActionForm{
private String userName;
private String password;
public void setuserName(String user)
{
this.userName=user;
}
public String getuserName()
{
return userName;
}
public void setpassword(String pass)
{
this.password=pass;
}
public String getpassword()
{
return password;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (userName == null || userName.length() < 1) {
errors.add("userName", new ActionMessage("error.userName.required"));
}
if (password == null || password.length() < 1) {
errors.add("password", new ActionMessage("error.password.required"));
}
return errors;
}

ApplicationResource.properties
------------------------------

error.userName.required = User Name is required.
error.password.required = Password is required.

LoginAction.java
----------------

public class LoginAction extends org.apache.struts.action.Action {

private final static String SUCCESS = "success";
private final static String FAILURE = "failure";

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LoginForm loginForm = (LoginForm) form;
if (loginForm.getUserName().equals(loginForm.getPassword())) {
return mapping.findForward(SUCCESS);
} else {
return mapping.findForward(FAILURE);
}
}
}

HTTP Status 500 - Internal Server Error
________________________________________
type Exception report
messageInternal Server Error
descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NullPointerException
root cause
java.lang.NullPointerException
note : The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.
________________________________________
GlassFish Server Open Source Edition 4.1

stacktrace
----------

Launching GlassFish on Felix platform
Aug 09, 2016 11:16:34 AM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner createBundleProvisioner
INFO: Create bundle provisioner class = class com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner.
Aug 09, 2016 11:16:34 AM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner$DefaultCustomizer getLocations
WARNING: Skipping entry because it is not an absolute URI.
Aug 09, 2016 11:16:34 AM com.sun.enterprise.glassfish.bootstrap.osgi.BundleProvisioner$DefaultCustomizer getLocations
WARNING: Skipping entry because it is not an absolute URI.
Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishRuntime@8bf20b in service registry.
Found populator: com.sun.enterprise.v3.server.GFDomainXml
#!## LogManagerService.postConstruct : rootFolder=C:\Program Files\glassfish-4.1\glassfish
#!## LogManagerService.postConstruct : templateDir=C:\Program Files\glassfish-4.1\glassfish\lib\templates
#!## LogManagerService.postConstruct : src=C:\Program Files\glassfish-4.1\glassfish\lib\templates\logging.properties
#!## LogManagerService.postConstruct : dest=C:\Users\OM\AppData\Roaming\NetBeans\8.0.1\config\GF_4.1\domain1\config\logging.properties
Info: Running GlassFish Version: GlassFish Server Open Source Edition 4.1 (build 13)
Info: Server log file is using Formatter class: com.sun.enterprise.server.logging.ODLLogFormatter
Info: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
Info: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
Info: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
Info: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
Info: Authorization Service has successfully initialized.
Info: Grizzly Framework 2.3.15 started in: 31ms - bound to [/0.0.0.0:8080]
Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:8181]
Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:4848]
Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:3700]
Info: visiting unvisited references
Info: Java security manager is disabled.
Info: Entering Security Startup Service.
Info: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper.
Info: Security Service(s) started successfully.
Info: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080
Info: Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181
Info: Created HTTP listener admin-listener on host/port 0.0.0.0:4848
Info: Created virtual server server
Info: Created virtual server __asadmin
Info: Setting JAAS app name glassfish-web
Info: Virtual server server loaded default web module
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: Initializing Mojarra 2.2.7 ( 20140610-1547 https://svn.java.net/svn/mojarra~svn/tags/2.2.7@13362) for context '/login'
Info: Create ActionListener wrapping instance of type 'com.sun.faces.application.ActionListenerImpl'
Info: HV000001: Hibernate Validator 5.0.0.Final
Info: Loading chain catalog from jar:file:/C:/Users/OM/Documents/NetBeansProjects/login/build/web/WEB-INF/lib/struts-core-1.3.10.jar!/org/apache/struts/chain/chain-config.xml
Info: Tiles definition factory loaded for module ''.
Info: Loading validation rules file from '/WEB-INF/validator-rules.xml'
Info: Loading validation rules file from '/WEB-INF/validation.xml'
Info: Loading application [login] at [/login]
Info: Loading application login done in 7,785 ms
Info: GlassFish Server Open Source Edition 4.1 (13) startup time : Felix (7,304ms), startup services(8,352ms), total(15,656ms)
Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:7676]
Info: Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@1b88914 as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@11a03bf.
Info: JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://niit-PC:8686/jndi/rmi://niit-PC:8686/jmxrmi
Info: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080
Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:8080]
Info: Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181
Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:8181]
Info: Tiles definition factory found for request processor ''.
Warning: Form 'LoginForm' not found for locale 'en_US'
Warning: Resource org/apache/struts/action/ActionResources_en_US.properties Not Found.
Warning: Resource org/apache/struts/action/ActionResources_en.properties Not Found.
Warning: Unhandled Exception thrown: class java.lang.NullPointerException
Warning: StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at com.vaannila.LoginAction.execute(LoginAction.java:26)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Posted
Updated 10-Aug-16 23:11pm

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900