For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2006 > Error setting property 'myFile' in bean of type null









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Error setting property 'myFile' in bean of type null
gbattine

2006-06-17, 8:30 am

Excuse me guys,
I've this question.
My Jsf Application allow user to upload a txt file thanks to a Myfaces
component and then elaborate it calculating the number of rows and
columns and showing them to user.
When my application run in eclipse i select the file to upload and i
submit.
At this point i've this error

Error setting property 'myFile' in bean of type null

Why?
Please help me,i'm a newbie..excuse me for my english .

I show you my MyBean.java, web.xml and faces-config.xml

MyBean.java

package com.devsphere.articles.jsfupload;

import org.apache.myfaces.custom.fileupload.UploadedFile;


import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.HashMap;

import java.io.*;

public class MyBean {
private UploadedFile myFile;
private Map <String, double[]>dataMap=3Dnull;
private int Nrows=3D0;
private int Ncol=3D0;
private int numberOfNumericColumns =3D0;

public UploadedFile getMyFile() {
return myFile;
}

public void setMyFile(UploadedFile myFile) {
this.myFile =3D myFile;
}

public static void main(String[] args) throws IOException {
new MyBean();
}

public MyBean() throws IOException { //constructor
// Get a reference to the input file.

BufferedReader br=3D new BufferedReader(new
InputStreamReader(myFile.getInputStream()));


// Create the empty map.
this.dataMap =3D new HashMap<String, double[]>();
//Constructs an empty HashMap with the default initial capacity
(16) and the default load factor (0.75).
// Populate the map from the input file.
populateMap(br);
//int Dim=3D(dataMap.size());//include the first string row
//System.out.println("Il size del datamap e'"+Dim);
Ncol=3D(numberOfNumericColumns+1);
System.out.println("Il numero di colonne e'"+(Ncol));
//System.out.println("Il numero di colonne del file
=E8"+dataMap.size());
// Display the contents of the map.
displayMap(this.dataMap);
// Close the reader.
br.close();
}



public Map populateMap(BufferedReader bufferedReader) throws
IOException
{
System.out.println("Caricamento dell'array di double in
corso.....");
// Store each line of the input file as a separate key and
entry in the Map.
String line =3D null;
while ((line =3D bufferedReader.readLine()) !=3D null) {
line =3D line.replace (',', '.');
Nrows++;
// Create a tokenizer for the line.
StringTokenizer st =3D new StringTokenizer(line);
// Assuming that the first column of every row is a
String and
//the remaining columns are numbers, count the number
of numeric columns.

//int ColNumber=3Dst.countTokens();
// int numberOfNumericColumns =3D ColNumber- 1;
numberOfNumericColumns =3D (st.countTokens()-1);
//System.out.println("Il numero di colonne del file
=E8"+dataMap.size());


// Get the first token from the line. It will be a
String and
//its value will be a unique key for the rest of the
row.

String key =3D st.nextToken().trim();
// Create the array for the numbers which make up the
rest of the line.
double[] array =3D new double[numberOfNumericColumns];
// Populate the array by parsing the rest of the line.
for (int column =3D 0; column < numberOfNumericColumns;
column++){
array[column] =3D
Double.parseDouble(st.nextToken().trim());
}
// Store the first column as the key and the array as
the entry.

this.dataMap.put(key, array); //Associates the
specified value with
//the specified key in this map.

}
System.out.println("Il numero di colonne numeriche del file
e'"+numberOfNumericColumns);
System.out.println("Il numero di righe del file e'"+Nrows);

//System.out.println("Il numero di colonne del file
=E8"+dataMap.size());
return this.dataMap;
}


public void displayMap(Map<String,double[]> myMap) {
// Iterate through the values of the map, displaying each key
and
//its corresponding array.
for (Map.Entry<String, double[]> entry : myMap.entrySet()) {
// Get and display the key.
System.out.print(entry.getKey() + " : ");
// Get the array.
double[] myArray =3D entry.getValue();

// Display each value in the array. Put a semicolon after
each
//value except the last.
// Keep all the values for a given key on a single line.
//
for (int ix =3D 0; ix < myArray.length; ix++) {
if (ix < myArray.length - 1) { //the value is not the
last one in the array ,allora metti ;
System.out.print(myArray[ix] + "; ");
} else { //the value is the last one in the array ,la
linea finisce cos=EC
System.out.println(myArray[ix]);
}
}
}
}
}


Web.xml

<?xml version=3D"1.0" encoding=3D"UTF-8"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>

<filter>
<filter-name>ExtensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.component.html.util.ExtensionsFilter
</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>ExtensionsFilter</filter-name>
<servlet-name>FacesServlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


faces-config.xml

<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD
JavaServer Faces Config 1.1//EN"

"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>MyBean</managed-bean-name>

<managed-bean-class>com.devsphere.articles.jsfupload.MyBean</managed-bean-c=
lass>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/pages/MyForm.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/MyResult.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com