posted 19 years ago
This is the error your code generates:
Why? Count down to the given line (32 here but I've added some comments to the top of the file so it will be different for the code in your post). You declared JTextField response as a class varible but did not instantiate it. Instead you declared and instantiated it JTextField response = new JTextField(10); inside init.
Scope. What you declare inside curley braces (eg, init) is private to that scope, nobody outside the curley braces can see it. The curley braces define the scope of the variable you declare within them.
From the point of view of the actionPerformed method, response has been declared (in class scope, same as actionPerformed is in) but ha not been initialized. actionPerformed cannot see the JTextField instance response that was declared and instantiated within init.
The response delcared and instantiated inside init has no relationship to the response delcared in class scope above/outside init. So when you try to call a method on response you cause a NullPointerException, there is no JTextField named response - it never was instantiated. In other words, response is a reference to a JTextField that does not exist (has not been instantiated).
Here's one way fix it: