NullPointer Exception in jsf for image upload

I have an issue with the file uploading in jsf. i have done the following code ih jsf . index.xhtml(View) User Id Username Profile Picture User.java(model) package com.profileapp.models; import java.io.Serializable; import org.apache.myfaces.custom.fileupload.UploadedFile; public class User implements Serializable { private String userId; private String userName; private UploadedFile photo; public User() { } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public UploadedFile getPhoto() { return photo; } public void setPhoto(UploadedFile photo) { this.photo = photo; } } UserController.java(Controller) package com.profileapp.controllers; import com.profileapp.models.User; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import org.apache.commons.io.FilenameUtils; @ManagedBean(name="controller") @RequestScoped public class UserController implements Serializable { private User user=new User(); public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String submit() throws IOException ,SQLException ,ClassNotFoundException, InstantiationException, IllegalAccessException { String fileName = FilenameUtils.getName(user.getPhoto().getName()); byte[] bytes = user.getPhoto().getBytes(); int index=fileName.indexOf('.'); String extension=fileName.substring(index); File file; String path; path = "C:/Users/Manohar/Documents/NetBeansProjects/ProfileApplication/build/web/uploads/"+user.getUserName()+(float)Math.random()+"/"; if(extension.equalsIgnoreCase(".jpg")||extension.equalsIgnoreCase(".jpeg")||extension.equalsIgnoreCase(".png")||extension.equalsIgnoreCase(".gif")||extension.equalsIgnoreCase(".tif")) { file=new File(path); if(!file.exists()) { file.mkdir(); } path=file+"/"+fileName; FileOutputStream outfile=new FileOutputStream(path); outfile.write(bytes); outfile.close(); PreparedStatement stmt; Connection connection; String url="jdbc:mysql://localhost:3306/userprofile"; Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(url, "root", "mysql"); stmt = connection.prepareStatement("insert into table_profile values('"+user.getUserName()+"','"+user.getUserId()+"','"+path+"')"); stmt.executeUpdate(); connection.close(); return "SUCCESS"; } else { return "FAIL"; } } } when i run this code i gets a nullpointer execption for field inputFileUpolad element i.e. when i debug i observed that setter method of User.photo field not gets called i got the following error (component tree) Facelet Title User Id Username Profile Picture

以上就是NullPointer Exception in jsf for image upload的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏