Question

Here i come with a problem to insert data in struts2 so i have tried with my level as a beginner but it showing some error i cant able to identify the error could some one can help on this???

error

HTTP Status 500 - java.lang.NullPointerException

type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
root cause

java.lang.NullPointerException
    Action.Testiue.add(Testiue.java:18)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)

Testiue.java

package Action;

import com.opensymphony.xwork2.ActionSupport;
import dao.UserDao;
import dbBean.UseBean;

public class Testiue 
{
    private UserDao dao;


    public String add()
    {

        UseBean bean = new UseBean();
        System.out.println(bean.getName());
        dao.addUser(bean);
        return ActionSupport.SUCCESS;

    }

}

Userdao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import dbBean.UseBean;
import Dbconnect.*;

public class UserDao
{

    private Connection conn;

    public UserDao()
    {
        conn=Dbconnect.getConnection();
    }
    public void addUser(UseBean bean)
    {
        try
        {
            String sql="insert into senthil (name,pass,phoneno,emailid) values(?,?,?,?)";
            PreparedStatement ps=conn.prepareStatement(sql);    
            ps.setString(1,bean.getName());
            ps.setString(2,bean.getPassword());
            ps.setString(3,bean.getPhoneo());
            ps.setString(4,bean.getEmailID());
            ps.executeUpdate();

        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }

insert.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<a href="<s:url action="view.action"/>">Display Records</a>
<br><br>
<b><font color="#5d8122" face="verdana">Insert Details</font></b>
    <s:form action="adduser">   

            <s:textfield label="id" name="ID" cssClass="bord"/>
            <s:textfield label="Name" name="Name" cssClass="bord"/>
            <s:textfield label="Password" name="password" cssClass="bord"/>
            <s:textfield label="phoneno" name="Phoneo"></s:textfield>
            <s:textfield label="Emailid" name="Emailid"></s:textfield>


    <s:submit value="Insert" />

</s:form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>insert.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struct.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="struts-default.xml" />
    <package name="a" extends="struts-default">

        <action name="adduser" class="Action.Testiue" method="add">
            <result name="success">insert.jsp</result>
        </action>

</package>
</struts>
Was it helpful?

Solution

you have just made a referrence here UserDao dao but not initialized

do like this

public class Testiue 
{
    private UserDao dao;


    public String add()
    {
dao=new UserDao();
        UseBean bean = new UseBean();
        System.out.println(bean.getName());
        dao.addUser(bean);
        return ActionSupport.SUCCESS;

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top