Solved

Array of TextFields hangs applet

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

690 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