Solved

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

Posted on 2004-04-25
18
1,501 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ahhhhh!

right

  class CatData extends DatasetProducer

should be:

  class CatData implements DatasetProducer

:-)
0
 

Author Comment

by:zamirjalil
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

Expert Comment

by:searlas
Comment Utility
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
Comment Utility
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
Comment Utility
searlas' last comment is the right way to do it...

I am an idiot :-(
Sorry
0
 

Author Comment

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

Author Comment

by:zamirjalil
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
immutable object concept 5 104
struts hibernate integration example 28 113
equalIsNot  challenge 43 114
Java Script nested call 3 51
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now