Error setting property 'myFile' in bean of type null

G

gbattine

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. Can you say me an italian book about
JSF?
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=null;
private int Nrows=0;
private int Ncol=0;
private int numberOfNumericColumns =0;


public UploadedFile getMyFile() {
return myFile;
}


public void setMyFile(UploadedFile myFile) {
this.myFile = 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= new BufferedReader(new
InputStreamReader(myFile.getInputStream()));


// Create the empty map.
this.dataMap = 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=(dataMap.size());//include the first string row
//System.out.println("Il size del datamap e'"+Dim);
Ncol=(numberOfNumericColumns+1);
System.out.println("Il numero di colonne e'"+(Ncol));
//System.out.println("Il numero di colonne del file
è"+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 = null;
while ((line = bufferedReader.readLine()) != null) {
line = line.replace (',', '.');
Nrows++;
// Create a tokenizer for the line.
StringTokenizer st = 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=st.countTokens();
// int numberOfNumericColumns = ColNumber- 1;
numberOfNumericColumns = (st.countTokens()-1);
//System.out.println("Il numero di colonne del file
è"+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 = st.nextToken().trim();
// Create the array for the numbers which make up the
rest of the line.
double[] array = new double[numberOfNumericColumns];
// Populate the array by parsing the rest of the line.
for (int column = 0; column < numberOfNumericColumns;
column++){
array[column] =
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
è"+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 = 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 = 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ì
System.out.println(myArray[ix]);
}
}
}
}



}


Web.xml

<?xml version="1.0" encoding="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="1.0" encoding="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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top