0

当我尝试显示我上传的图像时,我得到了空指针。

我按照以下示例进行操作:Richfaces Showcase

错误:javax.el.E​​LException:...[路径]... createContent="#{imageBean.paint}":java.lang.NullPointerException

使用的源代码:

ImageBean.java

@ManagedBean
@SessionScoped
public class ImageBean implements Serializable{
  private ImageData imageData= new ImageData();

  public void listener(FileUploadEvent event) throws Exception {
    UploadedFile item = event.getUploadedFile();
    imageData = new ImageData();
    imageData.setName(item.getName());
    imageData.setData(item.getData());
  }

  public void paint(OutputStream stream, Object object) throws IOException {
    stream.write(getFile().getData());
    stream.close();
  }

  // getters and setters for file

上传图片.xhtml

<h:form>
        <h:panelGrid columns="2" columnClasses="top,top">
            <rich:fileUpload fileUploadListener="#{imageBean.listener}" id="upload" acceptedTypes="jpg, gif, png, bmp"
                ontyperejected="alert('Only JPG, GIF, PNG and BMP files are accepted');" maxFilesQuantity="1">
                <a4j:ajax event="uploadcomplete" execute="@none" render="info" />
            </rich:fileUpload>
            <h:panelGroup id="info" layout="block">
                <rich:panel bodyClass="info">
                    <f:facet name="header">
                        <h:outputText value="Uploaded Files Info" />
                    </f:facet>
                    <h:outputText value="Geen foto beschikbaar" rendered="#{imageBean.size==0}" />
                    <a4j:mediaOutput element="img" mimeType="image/jpeg" createContent="#{imageBean.paint}"
                        value="#{imageBean.file.name}" style="width:100px; height:100px;" cacheable="false">
                    </a4j:mediaOutput>
                    <h:panelGrid columns="2">
                        <h:outputText value="File Name:" />
                        <h:outputText value="#{imageBean.file.name}" />
                    </h:panelGrid>
                </rich:panel>
                <br />
            </h:panelGroup>
        </h:panelGrid>
        </h:form>

根据我的调试器: imageDate 在上传监听器()中获取数据,但在paint() 中为空

我的想法是,我应该把我的范围扩大。所以我把它设置为@SessionScoped

网页.xml

<context-param>
  <param-name>createTempFiles</param-name>
  <param-value>false</param-value>
 </context-param>

真假我都试过了

使用 Richfaces:4.3.2

4

1 回答 1

0

您正在尝试在没有上传文件时渲染图像。<a4j:mediaOutput>需要有条件地呈现。

于 2013-09-18T08:49:20.280 回答