Solved

Array of TextFields hangs applet

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

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

919 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

18 Experts available now in Live!

Get 1:1 Help Now