title: Unable to store and retrieve image data from database via jdbc in struts2
body: <p>I am using struts2 . i m trying to store the image's path to database and to store the image in server and then want to retireve those image data  in jsp page. i need guidance.
  but i get an error</p>
  
  <blockquote>
    <p>java.lang.NullPointerException</p>
  </blockquote>
  
  <p>i have created 3 class.
  registraton(bean class)
  registrationService(jdbc code)
  registrationaction </p>
  
  <p>Registration.java</p>
  
  <pre><code>public class Registration{
  
    private  String first_name;
    private String last_name;
    private String gender;
    private String email_id;
    private String password;
    private  String dob;
    private String father_name;
    private  String mother_name;
    private    String contact;
     private   String mobile;
     private  String address;
     private  String city;
     private  String country;
     private  String graduation;
     private  String graduate_school_mark;
     private  String graduation_year;
     private    String inter_school;
    private   String inter_school_mark;
    private   String inter_year;
    private   String high_school;
    private   String high_school_mark;
    private   String high_school_year;
    private   String role;
    private   String salary;
    private   File fphoto;
    private File fresume;
    private   String pre_company;
  </code></pre>
  
  <p>// getter &amp; setter</p>
  
  <p>RegistrationService.java</p>
  
  <pre><code>import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import org.apache.struts2.ServletActionContext;
  
  public class RegistrationService {
  public boolean filledRegistration(Registration registration) throws SQLException, ClassNotFoundException, FileNotFoundException, IOException{
  
    Class.forName("com.mysql.jdbc.Driver");
    Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/erp","root","ali$");
    String sql = "insert into registration(first_name,last_name,password,gender,email_id,dob,"
                   + "father_name,mother_name,contact,mobile,address,city,country,graduation,"
                   + "graduation_marks,graduation_year,inter,inter_marks,inter_year,high_school,"
                   + "high_marks,high_year,role,salary,resume,photo,pre_comp) value(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
      PreparedStatement p= con.prepareStatement(sql);
                           p.setString(1,registration.getFirst_name());
                          p.setString(2,registration.getLast_name());
                           p.setString(3,registration.getPassword());
                          p.setString(4,registration.getGender());
                          p.setString(5,registration.getEmail_id()); 
                          p.setString(6,registration.getDob());   
                         p.setString(7,registration.getFather_name());
                          p.setString(8,registration.getMother_name());
                          p.setString(9,registration.getContact());
                          p.setString(10,registration.getMobile());
                          p.setString(11,registration.getAddress());
                          p.setString(12,registration.getCity());
                          p.setString(13,registration.getCountry());
                          p.setString(14,registration.getGraduation());
                          p.setString(15,registration.getGraduate_school_mark());
                          p.setString(16,registration.getGraduation_year());
                          p.setString(17,registration.getInter_school());
                          p.setString(18,registration.getInter_school_mark());
                          p.setString(19,registration.getInter_year());
                          p.setString(20,registration.getHigh_school());
                          p.setString(21,registration.getHigh_school_mark());
                          p.setString(22,registration.getHigh_school_year());
                          p.setString(23,registration.getRole());
                          p.setString(24,registration.getSalary());
  
                          FileInputStream fis1 = new FileInputStream(registration.getFphoto());
                          p.setBinaryStream(25,fis1);
  
                          FileInputStream fis=new FileInputStream(registration.getFphoto());
                          p.setBinaryStream(26, fis);
  
                          p.setString(27,registration.getPre_company());
  
                         String s= ServletActionContext.getRequest().getRealPath("");
                          System.out.println(s);
                          int temp= s.lastIndexOf("\\");
                          String s1= s.substring(0, temp);
  
                       //   System.out.println("----------&gt;"+s1);
  
                          int temp1 = s1.lastIndexOf("\\");
                          String s2= s1.substring(0, temp1);
  
                           FileOutputStream fos=new FileOutputStream(s2+"\\web\\Images\\image"+registration.getEmail_id()+".jpg");
                            byte buffer[]=new byte[1024];
                              int read=0;
                                     while((read=fis.read(buffer, 0, 1024))!=-1){
                                        fos.write(buffer);
                                        }
                                     fis.close();
                          fos.close();
  
                           int i= p.executeUpdate();
                            if(i&gt;0){
                                  return true;
                                   }
                  return false;       
          }
          }
  </code></pre>
  
  <p>RegistrationAction.java</p>
  
  <pre><code>import com.opensymphony.xwork2.ActionSupport;
  import com.opensymphony.xwork2.ModelDriven;
  import java.io.IOException;
  import java.sql.SQLException;
  public class RegistrationAction extends ActionSupport implements ModelDriven{
      Registration registration=new Registration();
  
      public Registration getRegistration() {
          return registration;
      }
  
      public void setRegistration(Registration registration) {
          this.registration = registration;
      }
      @Override
      public String execute() throws ClassNotFoundException, SQLException, IOException{
      RegistrationService rs= new RegistrationService();
      if( rs.filledRegistration(registration)){
          return "registered";
      } else {
      }
      return "failed";
  }
  
      @Override
      public Object getModel() {
         return registration;
      }
  }
  </code></pre>
  
  <p>welcome.jsp</p>
  
  <pre><code>&lt;%@taglib uri="/struts-tags" prefix="s"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;JSP Page&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;s:property value="user.email"/&gt;
          &lt;s:property value="user."/&gt;
          &lt;h1&gt;Welcome&lt;/h1&gt;
          &lt;s:property value="user.user_name"/&gt;
          &lt;s:form action="logoutAction"&gt;
          &lt;s:submit value="logout"/&gt;
          &lt;%
      String s=(String)request.getContextPath();
      String path=s+"/Images/"+user.email+".jpg";
  
  
  %&gt;        
  
  &lt;img src="&lt;%=path%&gt;" width="200" height="200" /&gt;
  
          &lt;/s:form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Glass fish server</p>
  
  <pre><code>Severe:   Exception occurred during processing request: null
  java.lang.NullPointerException
      at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:130)
      at package_registration.RegistrationService.filledRegistration(RegistrationService.java:53)
      at package_registration.RegistrationAction.execute(RegistrationAction.java:25)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
      at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)        
  </code></pre>