Solved

Array of TextFields hangs applet

Posted on 1997-06-20
2
363 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
2 Comments
 

Accepted Solution

by:
jaiger earned 100 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

789 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