Solved

Arrays of Strings

Posted on 2002-04-30
5
138 Views
Last Modified: 2010-03-31
Hi All,
Below is my JSP page:

<html>

<%@page import="java.io.*"%>

<%

String[] sTemp;

sTemp[0] = "Hi";
sTemp[1] = "There";
sTemp[2] = "How";
sTemp[3] = "are";
sTemp[4] = "you";

int i = 0;


for (i=0;i<4;i++)
{
 out.println(i + "<BR>");
  out.println(sTemp[i] + "<BR>");
}

%>

<body>

<h1> Just Java </h1>

</body>
</html>

I have tried using () instead of [], but that does not help. What am I doing wrong?

For some reason, I get this error when running the page in the browser. Jakarta-Tomcat-3.2.3 is my web server on a Windows 2000 OS. IE 5.5 is my browser.

org.apache.jasper.JasperException: Unable to compile class for JSPG:\jakarta-tomcat-3.2.3\work\localhost_8080%2Fjsp-ACO\_0002fjustjava_0002ejspjustjava_jsp_0.java:67: Variable sTemp may not have been initialized.
                sTemp[0] = "Hi";
                ^
1 error

     at org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
     at org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:612)
     at org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)
     at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:542)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:258)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:268)
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
     at org.apache.tomcat.core.Handler.service(Handler.java:287)
     at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
     at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
     at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
     at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
     at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
     at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
     at java.lang.Thread.run(Thread.java:484)
0
Comment
Question by:DoughBoy
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6982141
change String[] sTemp;

to

String[] sTemp = new String[5]; // allocate the memory needed for the array
0
 

Author Comment

by:DoughBoy
ID: 6982254
what if you don't know how big the array will get?
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6982272
In most dynamic cases(thus JSP) there would be a way of finding out the number of spaces needed before setting each value. If not then use an ArrayList instead and change your code.
0
 

Author Comment

by:DoughBoy
ID: 6982336
do you have any sample code to demonstrate your point
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6982428
you could also use a vector:

ie:

Vector v = new Vector();

v.add("Hello");
v.add("World");

for (int i=0;i<v.size();i++) {
   out.println(v.elementAt(i);
}

like ahosang stated, you could also use arraylist.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 32
Systems talking to each other 5 124
eclipse formatting 6 72
Java JRE greater than 1.6 5 50
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

22 Experts available now in Live!

Get 1:1 Help Now