java.lang.NullPointerException in JSF validator

时间:2016-02-12 20:56:29

标签: java jsf jsf-2

here is the code for my jsf managed bean:

for (Row row: rows) {
   HasbroIssuanceDateData data1 = extractedDateData.get(row);

   if (data.getOpeningBankReference().equals(data1.getOpeningBankReference())) {
     for (Row anotherRow: anotherRows) {
          //here you have all the rows you need.
     }
   }

here is my jsf file:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.activation.MimetypesFileTypeMap;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.imageio.ImageIO;
import javax.persistence.Transient;
import javax.servlet.http.Part;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ADITYA
 */
@ManagedBean
@SessionScoped
public class entersite {
String link,location;
int code,noot;
@Transient
Part file;

    public Part getFile() {
        return file;
    }

    public void setFile(Part file) {
        this.file = file;
    }

    public int getNoot() {
        return noot;
    }

    public void setNoot(int noot) {
        this.noot = noot;
    }
byte[] image;

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }




    public entersite() {
    }
    public boolean ver()
    {
    boolean d=false;
    Session s1=new Configuration().configure("hibernate.cfg.xml")  
                        .buildSessionFactory().openSession();  
    Transaction tx=s1.beginTransaction();
    int cd1=this.getCode();
    Query q1=s1.createQuery("from site.entersite where CODE=:var");
    q1.setInteger("var",cd1);
    tx.commit();
    List<entersite> lst=new ArrayList<>();
    for(Iterator itr=q1.iterate();itr.hasNext();)
    {
    entersite es1=(entersite)itr.next();
    if(es1.getCode()==cd1)
        d=true;
    }
    return d;
    }

    public String saving() throws Exception
    {
        if(ver()==true)
            return "err1";
        else
        {
        InputStream is1;        
        is1=file.getInputStream();
        byte[] buffer = new byte[(int)file.getSize()];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = is1.read(buffer)) != -1)
    {
        output.write(buffer, 0, bytesRead);
    }
    Session s1=new Configuration().configure("hibernate.cfg.xml")  
                        .buildSessionFactory().openSession();  
    Transaction tx=s1.beginTransaction();        
    entersite es=new entersite();
    es.setCode(this.getCode());
    es.setLink(this.getLink());
    es.setLocation(this.getLocation());
    es.setNoot(this.getNoot());
    es.setImage(output.toByteArray());
    s1.save(es);
    tx.commit();
    return "admin41";
        }
    }
    /*
    public File convertFile(Part file) throws IllegalStateException, IOException 
{
    InputStream is1;        
        is1=file.getInputStream();
        byte[] buffer = new byte[(int)file.getSize()];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = is1.read(buffer)) != -1)
    {
        output.write(buffer, 0, bytesRead);
    }
        File convFile = new File(file.getName());
    convFile.createNewFile(); 
    FileOutputStream fos = new FileOutputStream(convFile); 
    fos.write(output.toByteArray());
    fos.close(); 
    return convFile;
}
    */
    public void validateFile(FacesContext fc,UIComponent uc,Object value) throws Exception
    {
        Part file1=(Part)value;

        if(file1.getName()!=null)        
        {
    try(InputStream input = file1.getInputStream())
    {

        ImageIO.read(input).toString();
        // It's an image (only BMP, GIF, JPG and PNG are recognized).
    } catch (Exception e) {
        // It's not an image.
        throw new ValidatorException(new FacesMessage("please select image file only"));
    }    
        }
    /*
    File f = (File)convertFile(file1);
        String mimetype= new MimetypesFileTypeMap().getContentType(f);
        String type = mimetype.split("/")[0];
        if(type.equalsIgnoreCase("image")) {
    } else {
            throw new ValidatorException(new FacesMessage("please select image file only!"));
    }
            */

}
    public String move2()
    {
    return "adminmenu";
    }

}

here is my stack trace:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <ui:composition template="mytemp1_2.xhtml">
            <ui:define name="content">
                <table>
                    <tr><td> <a  href="/Last_project_work_1_1/faces/admin/admin2.xhtml">Enter new admin</a></td><td><a href="/Last_project_work_1_1/faces/admin/admin4.xhtml">Enter new site</a></td></tr>
                        <tr><td><a href="Last_project_work_1_1/faces/admin/addtheater.xhtml">Add Theater</a></td><td><a href="Last_project_work_1_1/faces/admin/deletesite.xhtml">Delete A Site</a></td></tr>
                        <tr><td><a href="listall.jsp">List all sites</a></td><td> <a href="Last_project_work_1_1/faces/admin/editdetails.xhtml">Edit Site Details</a></td></tr>
                </table>
            </ui:define>
        </ui:composition>        
    </h:body>
</html>

Now when i click on back commandButton with value back the control is supposed to go to adminmenu.xhtml but instead it gives the above null pointer exception.This happens when i leave the form fields empty. i have tried the silly small tricks to avoid npe but in vain please help me figure out the problem. thanks in advance...

0 个答案:

没有答案