zamirjalil
asked on
HELP!!! Urgent .. Inner class error in jsp
Hello experts out there,
I try to generate line graph based on cewolf open source graph software.
What I want to do is I want to get some data from 'GraphByFruit' class that passed in object of array. This object is stored in a session attribute "GraphData".
My below code produce error msg : "local variable request is accessed from within inner class; needs to be declared final"
How should I get the value from "GraphByFruits" class??
pls help...
my code in jsp:
DatasetProducer categoryData = new DatasetProducer() {
HttpSession logSession = request.getSession(false);
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])session.ge tAttribute ("GraphDat a");
String state = "";
String fruit = "";
float[] qty = null;
final String[] arrayOfFruit = new String[graph.length];
System.out.print("arrayOfF ruit ="+arrayOfFruit.length+"\n ");
final String[] categories= { "a", "s", "d", "f", "g", "h", "j", "k", "r"};
for (int i=0; i < graph.length; i++) {
state=graph[i].getStateID( );
qty = graph[i].getProdQty();
fruit = ""+graph[i].getFruitID();
if (i<graph.length-1) fruit = fruit+",";
arrayOfFruit [i] = fruit;
}
//final String[] categories = { "apples", "pies", "bananas", "oranges" };
//final String[] seriesNames = { "Peter", "Helga", "Franz", "Olga" };
final Integer[][] startValues = new Integer[arrayOfFruit.lengt h][categor ies.length ];
final Integer[][] endValues = new Integer[arrayOfFruit.lengt h][categor ies.length ];
for (int series = 0; series < arrayOfFruit.length; series++) {
for (int i = 0; i < categories.length; i++) {
int y =0;
startValues[series][i] = new Integer(y);
endValues[series][i] = new Integer(y + (int) (Math.random() * 10));
}
}
DefaultIntervalCategoryDat aset ds =
new DefaultIntervalCategoryDat aset(serie sNames, categories, startValues, endValues);
return ds;
}
public String getProducerId() {
return "CategoryDataProducer";
}
public boolean hasExpired(Map params, Date since) {
return false;
}
};
pageContext.setAttribute(" categoryDa ta", categoryData);
%>
<html>
<head>
<link href="cewolf.css" rel="stylesheet" type="text/css"></head>
<BODY bgcolor="#DDE8F2">
<H1>Cewolf Chart Set</H1>
<p>
<cewolf:chart id="lineChart" title="LineChart" type="line" xaxislabel="Fruit" yaxislabel="favorite">
<cewolf:data>
<cewolf:producer id="categoryData" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="lineChart" renderer="cewolf" width="300" height="300"/>
</body>
</html>
I try to generate line graph based on cewolf open source graph software.
What I want to do is I want to get some data from 'GraphByFruit' class that passed in object of array. This object is stored in a session attribute "GraphData".
My below code produce error msg : "local variable request is accessed from within inner class; needs to be declared final"
How should I get the value from "GraphByFruits" class??
pls help...
my code in jsp:
DatasetProducer categoryData = new DatasetProducer() {
HttpSession logSession = request.getSession(false);
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])session.ge
String state = "";
String fruit = "";
float[] qty = null;
final String[] arrayOfFruit = new String[graph.length];
System.out.print("arrayOfF
final String[] categories= { "a", "s", "d", "f", "g", "h", "j", "k", "r"};
for (int i=0; i < graph.length; i++) {
state=graph[i].getStateID(
qty = graph[i].getProdQty();
fruit = ""+graph[i].getFruitID();
if (i<graph.length-1) fruit = fruit+",";
arrayOfFruit [i] = fruit;
}
//final String[] categories = { "apples", "pies", "bananas", "oranges" };
//final String[] seriesNames = { "Peter", "Helga", "Franz", "Olga" };
final Integer[][] startValues = new Integer[arrayOfFruit.lengt
final Integer[][] endValues = new Integer[arrayOfFruit.lengt
for (int series = 0; series < arrayOfFruit.length; series++) {
for (int i = 0; i < categories.length; i++) {
int y =0;
startValues[series][i] = new Integer(y);
endValues[series][i] = new Integer(y + (int) (Math.random() * 10));
}
}
DefaultIntervalCategoryDat
new DefaultIntervalCategoryDat
return ds;
}
public String getProducerId() {
return "CategoryDataProducer";
}
public boolean hasExpired(Map params, Date since) {
return false;
}
};
pageContext.setAttribute("
%>
<html>
<head>
<link href="cewolf.css" rel="stylesheet" type="text/css"></head>
<BODY bgcolor="#DDE8F2">
<H1>Cewolf Chart Set</H1>
<p>
<cewolf:chart id="lineChart" title="LineChart" type="line" xaxislabel="Fruit" yaxislabel="favorite">
<cewolf:data>
<cewolf:producer id="categoryData" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="lineChart" renderer="cewolf" width="300" height="300"/>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi TimYates...
I follow you way but it produce this error.. pls consult...
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St andalone/l ocalhost/z amirr/epla n/graph/gr aph_jsp.ja va:73: expected
[javac] pageContext.setAttribute(" categoryDa ta", data);
[javac] ^
[javac] 1 error
I follow you way but it produce this error.. pls consult...
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St
[javac] pageContext.setAttribute("
[javac] ^
[javac] 1 error
you do have:
<%
CatData data = new CatData( session ) ;
pageContext.setAttribute(" categoryDa ta", data );
%>
?
Check the "{" and "}" match in the jsp...
Hmmm... odd...
<%
CatData data = new CatData( session ) ;
pageContext.setAttribute("
%>
?
Check the "{" and "}" match in the jsp...
Hmmm... odd...
ASKER
ok... as you said I call the intance of data in different block.. then the error resolve... BUT...
this error appear:
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St andalone/l ocalhost/z amir/eplan /graph/gra ph_jsp.jav a:30: cannot resolve symbol
[javac] symbol : variable session
[javac] location: class org.apache.jsp.graph_jsp.C atData
[javac] GraphByFruit[] graph = (GraphByFruit[])session.ge tAttribute ("GraphDat a");
[javac] ^
[javac] /usr/local/tomcat4/work/St andalone/l ocalhost/z amir/eplan /graph/gra ph_jsp.jav a:39: incompatible types
[javac] found : int
[javac] required: java.lang.String
[javac] state=graph[i].getStateID( );
[javac] ^
pls help...
this error appear:
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St
[javac] symbol : variable session
[javac] location: class org.apache.jsp.graph_jsp.C
[javac] GraphByFruit[] graph = (GraphByFruit[])session.ge
[javac] ^
[javac] /usr/local/tomcat4/work/St
[javac] found : int
[javac] required: java.lang.String
[javac] state=graph[i].getStateID(
[javac] ^
pls help...
ASKER
sorry TimYates... this is should be error before previous post... sorry again..
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St andalone/l ocalhost/a gribazaar/ eplan/grap h/graph_js p.java:19: no interface expected here
[javac] class CatData extends DatasetProducer
[javac] ^
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St
[javac] class CatData extends DatasetProducer
[javac] ^
Ahhhhh!
right
class CatData extends DatasetProducer
should be:
class CatData implements DatasetProducer
:-)
right
class CatData extends DatasetProducer
should be:
class CatData implements DatasetProducer
:-)
ASKER
ohhh no... then come another error..
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St andalone/l ocalhost/a gribazaar/ eplan/grap h/graph_js p.java:30: cannot resolve symbol
[javac] symbol : variable session
[javac] location: class org.apache.jsp.graph_jsp.C atData
[javac] GraphByFruit[] graph = (GraphByFruit[])session.ge tAttribute ("GraphDat a");
pls help ....
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] /usr/local/tomcat4/work/St
[javac] symbol : variable session
[javac] location: class org.apache.jsp.graph_jsp.C
[javac] GraphByFruit[] graph = (GraphByFruit[])session.ge
pls help ....
Note: Declaring a class inside a JSP and then putting in the session is a REALLY BAD IDEA. Especially when developing, you will find that if you put an instance in the session, and then change the JSP, you will get a ClassCastException when you get the instance back out again.
The latest problem you are encountering is because you mix and match use of logSession and session.
I think you probably want to use logSession. But rememeber, you are now putting an object into a session that contains a reference to the session itself. This is horrible.
The latest problem you are encountering is because you mix and match use of logSession and session.
I think you probably want to use logSession. But rememeber, you are now putting an object into a session that contains a reference to the session itself. This is horrible.
ASKER
should I replace 'session' with 'logSession'?? I really have no idea for this.. pls help..
That will get it to compile, yes. From your original question, you could also just have changed:
DatasetProducer categoryData = new DatasetProducer() {
HttpSession logSession = request.getSession(false);
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])session.ge tAttribute ("GraphDat a");
To:
final logSession = request.getSession();
DatasetProducer categoryData = new DatasetProducer() {
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])logSession .getAttrib ute("Graph Data");
DatasetProducer categoryData = new DatasetProducer() {
HttpSession logSession = request.getSession(false);
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])session.ge
To:
final logSession = request.getSession();
DatasetProducer categoryData = new DatasetProducer() {
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])logSession
That will get it to compile, yes. From your original question, you could also just have changed:
DatasetProducer categoryData = new DatasetProducer() {
HttpSession logSession = request.getSession(false);
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])session.ge tAttribute ("GraphDat a");
To:
final HttpSession logSession = request.getSession();
DatasetProducer categoryData = new DatasetProducer() {
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])logSession .getAttrib ute("Graph Data");
DatasetProducer categoryData = new DatasetProducer() {
HttpSession logSession = request.getSession(false);
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])session.ge
To:
final HttpSession logSession = request.getSession();
DatasetProducer categoryData = new DatasetProducer() {
public Object produceDataset(Map params) {
GraphByFruit[] graph = (GraphByFruit[])logSession
searlas' last comment is the right way to do it...
I am an idiot :-(
Sorry
I am an idiot :-(
Sorry
ASKER
no.. you help me a lot Tim to get me thru the idea...
ASKER
guys.. by following Tim ways, I am able to call data from GraphByFruit object... but I am not able to implement generateLink and tooltip tag in categoryLink attribute dan catTG.. pls help
below is the update code
<%@page contentType="text/html"%>
<%@taglib uri='../../WEB-INF/cewolf. tld' prefix='cewolf' %>
<%@ taglib uri="/WEB-INF/struts-html. tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean. tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic .tld" prefix="logic" %>
<%@page import="eplan.graph.busine ss.*" %>
<%@page import="java.util.*"%>
<%@page import="de.laures.cewolf.* "%>
<%@page import="org.jfree.data.*"% >
<%@page import="org.jfree.data.tim e.*"%>
<%@page import="org.jfree.chart.to oltips.*"% >
<%@page import="org.jfree.chart.*" %>
<%@page import="org.jfree.chart.pl ot.*"%>
<%@page import="java.lang.*"%>
<%@page import="org.apache.commons .logging.L og"%>
<%@page import="org.apache.commons .logging.L ogFactory" %>
<%@page import="org.jfree.chart.re nderer.Lin eAndShapeR enderer" %>
<%!
class CatData implements DatasetProducer
{
GraphByFruit[] graph;
String[] arrayOfFruit;
public CatData( GraphByFruit[] _graph)
{
graph=_graph ;
String temp[] = new String[graph.length];
arrayOfFruit=temp;
}
public Object produceDataset(Map params)
{
int month = 0;
String state = "";
String fruit = "";
float[] qty = null;
double lastY=0;
final String[] arrayOfFruit = new String[graph.length];
final String[] categories= { "Jan", "Feb", "Mac", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"};
DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
/**
* @see java.lang.Object#finalize( )
*/
protected void finalize() throws Throwable {
super.finalize();
}
};
//put result into array
String fruitName="";
for (int series = 0; series < arrayOfFruit.length; series ++) {
Integer fruitID = new Integer(graph[series].getF ruitID());
arrayOfFruit[series] = fruitID.toString();
System.out.print("\narrayO fFruit["+s eries+"]=" +arrayOfFr uit[series ]+"\n");
state= ""+graph[series].getStateI D();
qty = graph[series].getProdQty() ;
System.out.print("\nCatego riesLength = "+categories.length+"\n");
for (int i = 0; i < categories.length; i++) {
final double y = qty[i];
//System.out.print("\nValu e Y ="+y+"\n");
//System.out.print("arrayO fFruit["+s eries+"]=" + arrayOfFruit[series]+"\n") ;
//System.out.print("catego ries["+i+" ]="+ categories[i]+"\n");
lastY = y;
dataset.addValue((double)y , arrayOfFruit[series], categories[i]);
}
}
return dataset;
}
public String getProducerId() {
return "CategoryDataProducer";
};
public boolean hasExpired(Map params, Date since) {
return false;
};
CategoryItemLinkGenerator categoryLink = new CategoryItemLinkGenerator( ) {
public String generateLink(Object dataset, int series, Object category) {
return "test";//arrayOfFruit[seri es];
}
};
CategoryToolTipGenerator catTG = new CategoryToolTipGenerator() {
public String generateToolTip(CategoryDa taset dataset, int series, int index) {
return String.valueOf(dataset.get Value(seri es, index));
}
};
%>
} // end class
<%
HttpSession getSession=request.getSess ion(false) ;
GraphByFruit[] getGraph = (GraphByFruit[]) session.getAttribute("Grap hData");
CatData data = new CatData( getGraph ) ;
pageContext.setAttribute(" categoryDa ta", data );
pageContext.setAttribute(" categoryLi nk", data.categoryLink); <!-- CANNOT CALL THIS ATTRIBUTE -->
pageContext.setAttribute(" categoryTo olTipGener ator", data.catTG); <!-- CANNOT CALL THIS ATTRIBUTE -->
%>
<html:html locale="true">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<cewolf:chart id="lineChart" title="LineChart" type="line" xaxislabel="Month"
yaxislabel="Total">
<cewolf:data>
<cewolf:producer id="categoryData" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550"/>
<cewolf:map tooltipgeneratorid="catego ryToolTipG enerator" linkgeneratorid="categoryL ink"/><!-- ERROR HERE -->
</cewolf:img>
</div>
</body>
</html:html>
below is the update code
<%@page contentType="text/html"%>
<%@taglib uri='../../WEB-INF/cewolf.
<%@ taglib uri="/WEB-INF/struts-html.
<%@ taglib uri="/WEB-INF/struts-bean.
<%@ taglib uri="/WEB-INF/struts-logic
<%@page import="eplan.graph.busine
<%@page import="java.util.*"%>
<%@page import="de.laures.cewolf.*
<%@page import="org.jfree.data.*"%
<%@page import="org.jfree.data.tim
<%@page import="org.jfree.chart.to
<%@page import="org.jfree.chart.*"
<%@page import="org.jfree.chart.pl
<%@page import="java.lang.*"%>
<%@page import="org.apache.commons
<%@page import="org.apache.commons
<%@page import="org.jfree.chart.re
<%!
class CatData implements DatasetProducer
{
GraphByFruit[] graph;
String[] arrayOfFruit;
public CatData( GraphByFruit[] _graph)
{
graph=_graph ;
String temp[] = new String[graph.length];
arrayOfFruit=temp;
}
public Object produceDataset(Map params)
{
int month = 0;
String state = "";
String fruit = "";
float[] qty = null;
double lastY=0;
final String[] arrayOfFruit = new String[graph.length];
final String[] categories= { "Jan", "Feb", "Mac", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"};
DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
/**
* @see java.lang.Object#finalize(
*/
protected void finalize() throws Throwable {
super.finalize();
}
};
//put result into array
String fruitName="";
for (int series = 0; series < arrayOfFruit.length; series ++) {
Integer fruitID = new Integer(graph[series].getF
arrayOfFruit[series] = fruitID.toString();
System.out.print("\narrayO
state= ""+graph[series].getStateI
qty = graph[series].getProdQty()
System.out.print("\nCatego
for (int i = 0; i < categories.length; i++) {
final double y = qty[i];
//System.out.print("\nValu
//System.out.print("arrayO
//System.out.print("catego
lastY = y;
dataset.addValue((double)y
}
}
return dataset;
}
public String getProducerId() {
return "CategoryDataProducer";
};
public boolean hasExpired(Map params, Date since) {
return false;
};
CategoryItemLinkGenerator categoryLink = new CategoryItemLinkGenerator(
public String generateLink(Object dataset, int series, Object category) {
return "test";//arrayOfFruit[seri
}
};
CategoryToolTipGenerator catTG = new CategoryToolTipGenerator()
public String generateToolTip(CategoryDa
return String.valueOf(dataset.get
}
};
%>
} // end class
<%
HttpSession getSession=request.getSess
GraphByFruit[] getGraph = (GraphByFruit[]) session.getAttribute("Grap
CatData data = new CatData( getGraph ) ;
pageContext.setAttribute("
pageContext.setAttribute("
pageContext.setAttribute("
%>
<html:html locale="true">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<cewolf:chart id="lineChart" title="LineChart" type="line" xaxislabel="Month"
yaxislabel="Total">
<cewolf:data>
<cewolf:producer id="categoryData" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550"/>
<cewolf:map tooltipgeneratorid="catego
</cewolf:img>
</div>
</body>
</html:html>
ASKER
Error generated :
java.lang.NullPointerExcep tion
at org.apache.jsp.graph_jsp$C atData.(gr aph_jsp.ja va:33)
at org.apache.jsp.graph_jsp._ jspService (graph_jsp .java:238)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:137 )
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:2 10)
at org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:295)
at org.apache.jasper.servlet. JspServlet .service(J spServlet. java:241)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.catalina.core.A pplication FilterChai n.internal DoFilter(A pplication FilterChai n.java:247 )
---
java.lang.NullPointerExcep
at org.apache.jsp.graph_jsp$C
at org.apache.jsp.graph_jsp._
at org.apache.jasper.runtime.
at javax.servlet.http.HttpSer
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
at org.apache.catalina.core.A
---
Things that could be null:
getGraph from:
HttpSession getSession=request.getSess ion(false) ;
GraphByFruit[] getGraph = (GraphByFruit[]) session.getAttribute("Grap hData");
CatData data = new CatData( getGraph ) ;
this would cause a NullPointerException in the CatData constructor at the line:
String temp[] = new String[graph.length];
Why bother setting three attributes in the page when they are all contained within your CatData object anyway?
pageContext.setAttribute(" categoryDa ta", data );
pageContext.setAttribute(" categoryLi nk", data.categoryLink); <!-- CANNOT CALL THIS ATTRIBUTE -->
pageContext.setAttribute(" categoryTo olTipGener ator", data.catTG); <!-- CANNOT CALL THIS ATTRIBUTE -->
Why is your closing bracket for your inner class OUTSIDE of the <%! %> markers?
%>
} // end class
getGraph from:
HttpSession getSession=request.getSess
GraphByFruit[] getGraph = (GraphByFruit[]) session.getAttribute("Grap
CatData data = new CatData( getGraph ) ;
this would cause a NullPointerException in the CatData constructor at the line:
String temp[] = new String[graph.length];
Why bother setting three attributes in the page when they are all contained within your CatData object anyway?
pageContext.setAttribute("
pageContext.setAttribute("
pageContext.setAttribute("
Why is your closing bracket for your inner class OUTSIDE of the <%! %> markers?
%>
} // end class
ASKER
yes..now it is working.. just a silly mistake :))
just change
from :
<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550"/>
<cewolf:map tooltipgeneratorid="catego ryToolTipG enerator" linkgeneratorid="categoryL ink"/><!-- ERROR HERE -->
</cewolf:img>
to:
<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550">
<cewolf:map tooltipgeneratorid="catego ryToolTipG enerator" linkgeneratorid="categoryL ink"/>
</cewolf:img>
Thanks a lot to TimYates..
just change
from :
<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550"/>
<cewolf:map tooltipgeneratorid="catego
</cewolf:img>
to:
<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550">
<cewolf:map tooltipgeneratorid="catego
</cewolf:img>
Thanks a lot to TimYates..
Hope this makes sense (and works) ;-)
Hee hee
Tim