Solved

Array of TextFields hangs applet

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

757 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

26 Experts available now in Live!

Get 1:1 Help Now