0

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

java.lang.NullPointerException

i have created 3 class. registraton(bean class) registrationService(jdbc code) registrationaction

Registration.java

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;

// getter & setter

RegistrationService.java

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("---------->"+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>0){
                                return true;
                                 }
                return false;       
        }
        }

RegistrationAction.java

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;
    }
}

welcome.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <s:property value="user.email"/>
        <s:property value="user."/>
        <h1>Welcome</h1>
        <s:property value="user.user_name"/>
        <s:form action="logoutAction">
        <s:submit value="logout"/>
        <%
    String s=(String)request.getContextPath();
    String path=s+"/Images/"+user.email+".jpg";


%>        

<img src="<%=path%>" width="200" height="200" />

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

Glass fish server

Severe:   Exception occurred during processing request: null
java.lang.NullPointerException
    at java.io.FileInputStream.<init>(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)        
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
sufiyan ali
  • 71
  • 1
  • 1
  • 9
  • Java code for registration action, jsp code for logout action ? And which is the line with the NPE ? Also Remove `=new Registration()` from your model declaration – Andrea Ligios Aug 07 '15 at 09:56
  • @Andrea Ligios- this is the line of NPE -->package_registration.RegistrationService.filledRegistration(RegistrationService.java:53) – sufiyan ali Aug 07 '15 at 09:58
  • Where is the registration data coming from ? You've added only welcome jsp with a pointer to logout action – Andrea Ligios Aug 07 '15 at 10:08
  • Check the value of this....registration.getFphoto()...it looks like null to me. – Amit Bhati Aug 07 '15 at 10:18
  • @Amit --> its true that registration.getFphoto() is null ,so what should i do so that registration.getFphoto() get value in it...i have created the bean class (registration.java) – sufiyan ali Aug 07 '15 at 10:26
  • Check my answer. Hope that helps – Amit Bhati Aug 07 '15 at 10:33
  • @Amit-->ur suuggestion helped me..but my question is how to store the path of image in database and how to retrieve the image path into proper image in jsp page.? – sufiyan ali Aug 07 '15 at 10:50
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Mark Rotteveel Aug 07 '15 at 16:31

2 Answers2

1

Right design for your requirement is:-

1) Store the image path to Database.
2) Store the images in server, and access these images using the path.

Now your fphoto of Registration class should be of type String. Because you are storing the image path and not the actual image in db.

Once you get the file path, create a File object and pass it to FileOutputStream which can be used to show the image on jsp.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
0

Change the private File fphoto; to String type.. Then store path and access image by using path.

gopal gupta
  • 119
  • 11