Error 500: Server caught unhandled exception from servlet [JSP 1.2 Processor]: 8

hi all,
 i am getting
Error 500: Server caught unhandled exception from servlet [JSP 1.2 Processor]: 8

i found in console is

java.lang.ArrayIndexOutOfBoundsException: 8
 


here is my query

//declaring the variable
String[] colLabels=new String[8];
pieStyle[1]="RED";
pieStyle[2]="BLUE";
pieStyle[3]="ORANGE";
pieStyle[4]="YELLOW";
pieStyle[5]="#00FF33";
pieStyle[6]="#996633";
pieStyle[7]="GREEN";
pieStyle[8]="violet";


<%
s = conn.createStatement();
rs= s.executeQuery("select * from tablename where flag = 'active' and id="+ request.getParameter("id"));
%>

// counting number of rows and storing in variable colNumber
<%while (rs.next())
{
colNumber++;
}
%>

//Assigning the values to the colLables array

<%while (rs.next())
{
for(int z=1;z<=colNumber;z++)
{
colLabels[z]=rs.getString("tod_col1");
rs.next();
}
}
%>

//printing the applet parameters


<%
for(int i=1;i<=colNumber-1;i++)
{
%>
<PARAM NAME="PIE_NAME_<%=i%>" VALUE="<%=colLabels%>">
<PARAM NAME="PIE_STYLE_<%=i%>" VALUE="<%=pieStyle%>">
<%
}
%>

my code works fine if the number of rows returned will be 7.
sometimes my query return more than 320 rows.
then i am getting the error
Error 500: Server caught unhandled exception from servlet [JSP 1.2 Processor]: 8

so i tried to declare an array of the number of rows

 //declaring the variable
<%
String[] colLabels=new String[colNumber];
String[] pieStyle=new String[colNumber];
%>

same error server caught undandled exception.
 now  i tried like below

String[] colLabels=new String[458];
String[] pieStyle=new String[458];


and assigning values
pieStyle[1]="#CCCC33";
pieStyle[2]="#99CCFF";
pieStyle[3]="#CC66CC";
pieStyle[4]="#CC3366";
..... etc till
pieStyle[50]="#9966FF";

 so its works fine if i have 50 records.
instead of doing this statically
 i thought of  assign randly generated colors to an array pieStyle[] . instead of assinging them static.?hwo to do this?
or is there any other solution please let me know.?
waiting for expert solution


saleemkhanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
String[] colLabels=new String[8];
pieStyle[0]="RED";
pieStyle[1]="BLUE";
pieStyle[2]="ORANGE";
pieStyle[3]="YELLOW";
pieStyle[4]="#00FF33";
pieStyle[5]="#996633";
pieStyle[6]="GREEN";
pieStyle[7]="violet";
0
TimYatesCommented:
Arrays go from "0", not "1"

0
jimmackCommented:
Arrays in Java are indexed from 0.

As Tim pointed out (when he should be working ;-))
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

TimYatesCommented:
> String[] pieStyle=new String[458];

here, you are making an array WAY bigger than the amount you use, so you never fall off the end :-)

Hope this helps :-)

Tim
0
saleemkhanAuthor Commented:
yes i know Java arrays are zero based.

please read my whole question and let me solution..
0
TimYatesCommented:
Do what you are doing:

---------

<%
String[] colLabels=new String[colNumber];
String[] pieStyle=new String[colNumber];
%>

---------

but then go from "0" not "1":

---------

// and assigning values START AT ZERO
pieStyle[0]="#CCCC33";
pieStyle[1]="#99CCFF";

----------

If that doesn't work, you're going to have to post your code, and not just snippets of it...

Tim
0
saleemkhanAuthor Commented:
hi timyates,
 why i declare array like below is because sometimes my resultset will result 460 records from table.
i know its not prefrerred to use such a big array.

String[] pieStyle=new String[458];
0
TimYatesCommented:
>  yes i know Java arrays are zero based.

How do you expect this to work then?

String[] colLabels=new String[8];
pieStyle[1]="RED";
pieStyle[2]="BLUE";
pieStyle[3]="ORANGE";
pieStyle[4]="YELLOW";
pieStyle[5]="#00FF33";
pieStyle[6]="#996633";
pieStyle[7]="GREEN";
pieStyle[8]="violet";  // THIS WILL THROW AN ARRAYINDEXOUTOFBOUNDSEXCEPTION
0
TimYatesCommented:
> i know its not prefrerred to use such a big array.

That's not a big array...I use 100K arrays with no problems...

I think you are right trying to size your array to the colNumber variable
0
TimYatesCommented:
> As Tim pointed out (when he should be working ;-))

hehe ;-P
0
jimmackCommented:
Use an ArrayList to store the values instead.  This will grow to allow the additions as necessary.

Random colours...

        ArrayList pieStyle = new ArrayList();
        for (int i = 0; i < 100; i++)
        {
           StringBuffer colString = new StringBuffer(Integer.toHexString((int)(Math.random() * 0xFFFFFF)));
           while (colString.length() < 6)
           {
              colString.insert(0, "0");
           }
           colString.insert(0, "#");
           pieStyle.add(colString.toString());
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saleemkhanAuthor Commented:
hi timyates,
 i will like this

---------

<%
String[] colLabels=new String[colNumber];
String[] pieStyle=new String[colNumber];
%>

---------
 till now ok.no problem.

// and assigning values START AT ZERO
pieStyle[0]="#CCCC33";
pieStyle[1]="#99CCFF";

this will be ok no probelm.

suppose if my query results 350 records i have assign  350 colors to the array statically.
// and assigning values START AT ZERO
pieStyle[0]="#CCCC33";
pieStyle[1]="#99CCFF";
... etc
 ------ till
pieStyle[349]="#99CCFF";

so thats why in my frist post itself
i mentioned below please refer this again.

 i thought of  assign randly generated colors to an array pieStyle[] . instead of assinging them static.?hwo to do this?
or is there any other solution please let me know.?
waiting for expert solution
0
jimmackCommented:
Then you need:

<%
for(int i=1;i<=colNumber-1;i++)
{
%>
<PARAM NAME="PIE_NAME_<%=i%>" VALUE="<%=colLabels%>">
<PARAM NAME="PIE_STYLE_<%=i%>" VALUE="<%= pieStyle.elementAt(i) %>">
<%
}
%>
0
jimmackCommented:
Sorry:

<PARAM NAME="PIE_STYLE_<%=i%>" VALUE="<%= (String)pieStyle.elementAt(i) %>">
0
TimYatesCommented:
>  i thought of  assign randly generated colors to an array pieStyle[] . instead of assinging them static.?hwo to do this?

Ahhhhhhh....

Sorry, the title of this question, and most of the Q at the top are about an ArrayOutOfBoundsException....

All you wanted was a way to randomly create colors?
0
saleemkhanAuthor Commented:
hi,
  i am sorry that title of the quetion is wrong but in my first post itself  i ask the experts

>  i thought of  assign randly generated colors to an array pieStyle[] . instead of assinging them static.?hwo to do this?

from now onwards i will take care of title of the question.
i am sorry for this.
 i tried  the comment

<%
for(int i=1;i<=colNumber-1;i++)
{
%>
<PARAM NAME="PIE_NAME_<%=i%>" VALUE="<%=colLabels%>">
<PARAM NAME="PIE_STYLE_<%=i%>" VALUE="<%= (String)pieStyle.elementAt(i) %>">
<%
}
%>


i am getting error the method elementAt(int) is undefined for the type java.util.ArryList


0
jimmackCommented:
Oops.  Sorry.  That should be:

(String)pieStyle.get(i)
0
jimmackCommented:
;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.