Link to home
Start Free TrialLog in
Avatar of zamirjalil
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.getAttribute("GraphData");
                          String state = "";
                          String fruit = "";
                          float[] qty = null;
                          final String[] arrayOfFruit = new String[graph.length];
                          System.out.print("arrayOfFruit ="+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.length][categories.length];
                        final Integer[][] endValues = new Integer[arrayOfFruit.length][categories.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));
                                }
                        }
                        DefaultIntervalCategoryDataset ds =
                                new DefaultIntervalCategoryDataset(seriesNames, categories, startValues, endValues);
                        return ds;
                }
                public String getProducerId() {
                        return "CategoryDataProducer";
                }
                public boolean hasExpired(Map params, Date since) {
                        return false;
                }
        };
        pageContext.setAttribute("categoryData", 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>
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So you can see, the "<%!" block at the top declare your class, and then you use an instance of it in the "<%" block :-)

Hope this makes sense (and works) ;-)

Hee hee

Tim
Avatar of zamirjalil
zamirjalil

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/Standalone/localhost/zamirr/eplan/graph/graph_jsp.java:73:  expected
    [javac]         pageContext.setAttribute("categoryData", data);
    [javac]                                 ^
    [javac] 1 error
you do have:

<%
    CatData data = new CatData( session ) ;
    pageContext.setAttribute("categoryData", data );
%>

?

Check the "{" and "}" match in the jsp...

Hmmm... odd...
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/Standalone/localhost/zamir/eplan/graph/graph_jsp.java:30: cannot resolve symbol
    [javac] symbol  : variable session
    [javac] location: class org.apache.jsp.graph_jsp.CatData
    [javac]                GraphByFruit[] graph = (GraphByFruit[])session.getAttribute("GraphData");
    [javac]                                                       ^
    [javac] /usr/local/tomcat4/work/Standalone/localhost/zamir/eplan/graph/graph_jsp.java:39: incompatible types
    [javac] found   : int
    [javac] required: java.lang.String
    [javac]                                     state=graph[i].getStateID();
    [javac]                                                              ^


pls help...
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/Standalone/localhost/agribazaar/eplan/graph/graph_jsp.java:19: no interface expected here
    [javac]        class CatData extends DatasetProducer
    [javac]                                ^

Ahhhhh!

right

  class CatData extends DatasetProducer

should be:

  class CatData implements DatasetProducer

:-)
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/Standalone/localhost/agribazaar/eplan/graph/graph_jsp.java:30: cannot resolve symbol
    [javac] symbol  : variable session
    [javac] location: class org.apache.jsp.graph_jsp.CatData
    [javac]               GraphByFruit[] graph = (GraphByFruit[])session.getAttribute("GraphData");


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.

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.getAttribute("GraphData");

To:
final logSession = request.getSession();
 DatasetProducer categoryData = new DatasetProducer() {
                public Object produceDataset(Map params) {
                         GraphByFruit[] graph = (GraphByFruit[])logSession.getAttribute("GraphData");
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.getAttribute("GraphData");

To:
final HttpSession logSession = request.getSession();
 DatasetProducer categoryData = new DatasetProducer() {
                public Object produceDataset(Map params) {
                         GraphByFruit[] graph = (GraphByFruit[])logSession.getAttribute("GraphData");
searlas' last comment is the right way to do it...

I am an idiot :-(
Sorry
no.. you help me a lot Tim to get me thru the idea...
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.business.*" %>
<%@page import="java.util.*"%>
<%@page import="de.laures.cewolf.*"%>
<%@page import="org.jfree.data.*"%>
<%@page import="org.jfree.data.time.*"%>
<%@page import="org.jfree.chart.tooltips.*"%>
<%@page import="org.jfree.chart.*"%>
<%@page import="org.jfree.chart.plot.*"%>
<%@page import="java.lang.*"%>
<%@page import="org.apache.commons.logging.Log"%>
<%@page import="org.apache.commons.logging.LogFactory"%>
<%@page import="org.jfree.chart.renderer.LineAndShapeRenderer" %>  



<%!
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].getFruitID());
      
                  arrayOfFruit[series] = fruitID.toString();
                  System.out.print("\narrayOfFruit["+series+"]="+arrayOfFruit[series]+"\n");
                  state= ""+graph[series].getStateID();
                    qty = graph[series].getProdQty();
                  System.out.print("\nCategoriesLength = "+categories.length+"\n");
                      for (int i = 0; i < categories.length; i++) {
                            final double y = qty[i];
                        //System.out.print("\nValue Y ="+y+"\n");                  
                        //System.out.print("arrayOfFruit["+series+"]="+ arrayOfFruit[series]+"\n");
                                //System.out.print("categories["+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[series];                              
                }
        };

        CategoryToolTipGenerator catTG = new CategoryToolTipGenerator() {
                     public String generateToolTip(CategoryDataset dataset, int series, int index) {
                            return String.valueOf(dataset.getValue(series, index));
                        }
             };
%>

} // end class



<%

HttpSession getSession=request.getSession(false);
GraphByFruit[] getGraph = (GraphByFruit[]) session.getAttribute("GraphData");

CatData data = new CatData( getGraph ) ;
pageContext.setAttribute("categoryData", data );
pageContext.setAttribute("categoryLink", data.categoryLink); <!-- CANNOT CALL THIS ATTRIBUTE -->
pageContext.setAttribute("categoryToolTipGenerator", 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="categoryToolTipGenerator" linkgeneratorid="categoryLink"/><!-- ERROR HERE -->
</cewolf:img>
</div>
</body>
</html:html>
Error generated :

java.lang.NullPointerException
      at org.apache.jsp.graph_jsp$CatData.(graph_jsp.java:33)
      at org.apache.jsp.graph_jsp._jspService(graph_jsp.java:238)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
        ---
Things that could be null:

getGraph from:
HttpSession getSession=request.getSession(false);
GraphByFruit[] getGraph = (GraphByFruit[]) session.getAttribute("GraphData");

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("categoryData", data );
pageContext.setAttribute("categoryLink", data.categoryLink); <!-- CANNOT CALL THIS ATTRIBUTE -->
pageContext.setAttribute("categoryToolTipGenerator", data.catTG); <!-- CANNOT CALL THIS ATTRIBUTE -->


Why is your closing bracket for your inner class OUTSIDE of the <%! %> markers?

%>

} // end class




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="categoryToolTipGenerator" linkgeneratorid="categoryLink"/><!-- ERROR HERE -->
</cewolf:img>

to:

<cewolf:img chartid="lineChart" renderer="cewolf" width="800" height="550">
    <cewolf:map tooltipgeneratorid="categoryToolTipGenerator" linkgeneratorid="categoryLink"/>
</cewolf:img>


Thanks a lot to TimYates..