Solved

HELP!!! Urgent .. Inner class error in jsp

Posted on 2004-04-25
18
1,513 Views
Last Modified: 2008-03-10
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>
0
Comment
Question by:zamirjalil
  • 9
  • 5
  • 4
18 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 10916072
It's the line:

          HttpSession logSession = request.getSession(false);

that will be causing the problems...basically, with anonymous inner classes, you can only access variables from outside the class declaration (but local to a method) if they are declared final.

As you cannot control the modifiers for "request" in this method, you will have to declare your own class like so:

<%!
  class CatData extends DatasetProducer
  {
      HttpSession logSession ;

      public CatData( HttpSession session )
      {
           logSession = session ;
      }

      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;
                }
        }
%>

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

0
 
LVL 35

Expert Comment

by:TimYates
ID: 10916077
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
0
 

Author Comment

by:zamirjalil
ID: 10916269
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
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 35

Expert Comment

by:TimYates
ID: 10916295
you do have:

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

?

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

Hmmm... odd...
0
 

Author Comment

by:zamirjalil
ID: 10916308
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...
0
 

Author Comment

by:zamirjalil
ID: 10916322
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]                                ^

0
 
LVL 35

Expert Comment

by:TimYates
ID: 10916376
Ahhhhh!

right

  class CatData extends DatasetProducer

should be:

  class CatData implements DatasetProducer

:-)
0
 

Author Comment

by:zamirjalil
ID: 10916390
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 ....
0
 
LVL 7

Expert Comment

by:searlas
ID: 10916430
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.

0
 

Author Comment

by:zamirjalil
ID: 10916458
should I replace 'session' with 'logSession'?? I really have no idea for this.. pls help..
0
 
LVL 7

Expert Comment

by:searlas
ID: 10916483
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");
0
 
LVL 7

Expert Comment

by:searlas
ID: 10916484
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");
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10916551
searlas' last comment is the right way to do it...

I am an idiot :-(
Sorry
0
 

Author Comment

by:zamirjalil
ID: 10916751
no.. you help me a lot Tim to get me thru the idea...
0
 

Author Comment

by:zamirjalil
ID: 10925744
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>
0
 

Author Comment

by:zamirjalil
ID: 10925759
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)
        ---
0
 
LVL 7

Expert Comment

by:searlas
ID: 10926236
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




0
 

Author Comment

by:zamirjalil
ID: 10926248
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..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apache server configuration 7 88
Best practices to work on a spring MVC project 1 126
spring JDBC Template example error 26 290
going to wrong jsp page 2 34
Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question