?
Solved

Array of TextFields hangs applet

Posted on 1997-06-20
2
Medium Priority
?
370 Views
Last Modified: 2006-11-17
I've created an array of TextFields, to simplify field-population in an applet.  In my init() method, I can 'setText()',  get 'myTextField[i].length()', whatever.  Any TextField method works as advertized, so I've allocated and instantiated it properly.  But once my event handler calls *any other method in my class*, 'myTextField[i]' becomes a black hole: the debugger (visual cafe's) won't show its
value and the execution seems to fix on any line with a reference to it (as if it was in an infinite loop).  Any TextField method, called from any of my methods,  (i.e.:"myTextField[0].setText()" ) produces the same
result.  If I hadn't declared it as "global" to my class, I'd suspect scope-trouble.  What am I doing wrong?!!

Sample:

public class URLApplet extends Applet {

    int i;
    java.awt.TextField myTextField[];

 public void init() {
  super.init();

        TextField myTextField[] = new TextField[8];

        for (i=0; i < myTextField.length; i++) {       //works just fine
           myTextField[i] = new TextField();
        }
        myTextField[0].reshape(156,36,331,23);       //works just fine
        //...etc...

        for (i=0; i < myTextField.length; i++) {         //works just fine
            add(myTextField[i]); //works just fine
        }
        myTextField[0].setText("succeeds");             //works just fine, here!!!
 }

 private void clearScreen() {

        for (i=0; i < 6; i++) {
           myTextField[i].setText(" ");                       //*hangs here*
        }
 }

 public void populateTextFields() {
     int token = 0;
     try {
         URL urlObject = new URL("http://etc.com/cgi-bin/foo" + myTextField[8].getText());                                     //*hangs here*
            URLConnection agent = urlObject.openConnection();
            InputStreamReader(agent.getInputStream());
            StreamTokenizer stDealer = new
StreamTokenizer(agent.getInputStream());
            int tokenType = st.nextToken();
            int elems = myTextField[0].length               //*hangs here*
            for (i=0; i < elems; i++) {
//*hangs here*
                myTextField[i].setText(st.sval);               //*hangs here*
                tokenType = st.nextToken();
            } catch { blah, blah, blah;}

 public boolean action (Event e, Object o) {

     if (e.target instanceof Button || e.target instanceof TextField) {
     //if (e.target instanceof Button) {
   try {
       clearScreen();                //*never comes back*
       stopWatch();
       populateTextFields();         //*never comes back*

          } catch (Exception x) {
             System.out.print(x);
            }
        }
    return true;
    }
}

0
Comment
Question by:pwicks
[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 Comments
 

Accepted Solution

by:
jaiger earned 300 total points
ID: 1221878
sounds to me like a synchronization problem.  

you may not be dealing with an infinite loop but actually a thread blocking on some sort of mutex semaphore.

try wrapping all references to the text field array, after init(), in synchronized blocks.  

you could also try using a Vector instead of an array (?) since Vectors tend to be somewhat synchronized.
0
 

Author Comment

by:pwicks
ID: 1221879
This synchronization notion is worth noting, but two other responses have arrived this morning via E-mail indicating that I had allocated a second array when I thought I was instantiating the original array:

java.awt.TextField myTextField[];//declares array (method global)
TextField myTextField[] = new TextField[8]; //declares (redundantly) and initializes, but local to the init() method!

/* -- this is a *second* array. Since I hadn't initialized the original array, I was trying to use an array which was null. This caused the "black hole" effect, although I expected better of Java. Perhaps this is where syncronization would help.
it should read: */

myTextField[] = new TextField[8]; //instantiates original array.

Problem solved. Lesson learned. Thanks to all who responded and good luck to all who this "object lesson" may help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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