Solved

Arrays of Strings

Posted on 2002-04-30
5
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 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