Results 1 to 5 of 5
- 12-24-2009, 06:44 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Accessing objects from within Action listener
I'm trying to periodically send data here but it won't compile. The error is coming from trying to access the netout PrintWriter from inside and inner class. I have tried using Problem.netout instead but this makes no difference. Here is the code below.
Cog
Java Code:import java.io.*; import java.net.*; import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Problem { public static void main(String[] args) throws IOException { // Make the connection. BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("JoePC", 5000); PrintWriter netout = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader netin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // Make a timer to periodically send a test message ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { // How do you access the "netout" PrintWriter from here? [B]netout.println("another event");[/B] // This won't compile } }; new Timer(400, taskPerformer).start(); // Send and receive. String fromUser = "", fromServer = ""; while (true) { if ((fromUser = stdIn.readLine()) != null) { System.out.println("Client: " + fromUser); netout.println(fromUser); if (fromUser.indexOf("ETX") == 0) break; } if ((fromServer = netin.readLine()) != null) System.out.println("Server: " + fromServer); } // Clean up before exit. netout.close(); netin.close(); stdIn.close(); clientSocket.close(); } }Last edited by cog; 12-24-2009 at 06:45 PM. Reason: Highlighted the problem area in bold
-
The error should tell you what to do to solve this, make netout final, i.e.,
Java Code:public class Problem { public static void main(String[] args) throws IOException { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("JoePC", 5000); // *** here *** final PrintWriter netout = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader netin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { netout.println("another event"); } };
- 12-24-2009, 07:18 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Thanks Fubarable. That does work.
Unfortunately I made the mistake of trying to shorten the code before posting by removing the try/catch part of setting up the socket which is not working. I have tried making netout final at the initialization to null and assign. This gives the error message "identifier expected". Sorry for the reposting, I'm new to Java from a C and Python background.
Java Code:import java.io.*; import java.net.*; import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class problem { public static void main(String[] args) throws IOException { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); // Make the connection. Socket clientSocket = null; [B]PrintWriter netout = null;[/B] BufferedReader netin = null; try { clientSocket = new Socket("JoePC", 5000); [B]final netout = new PrintWriter(clientSocket.getOutputStream(), true);[/B] netin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); } catch (Exception e) { System.err.println("Could not connect: " + e.toString()); System.exit(1); } // Make a timer to periodically send a test message ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { // How do you access the "netout" PrintWriter from here? [B]netout.println("another event"); // This won't compile[/B] } }; new Timer(400, taskPerformer).start(); // Send and receive. String fromUser = "", fromServer = ""; while (true) { if ((fromUser = stdIn.readLine()) != null) { System.out.println("Client: " + fromUser); netout.println(fromUser); if (fromUser.indexOf("ETX") == 0) break; } if ((fromServer = netin.readLine()) != null) System.out.println("Server: " + fromServer); } // Clean up before exit. netout.close(); netin.close(); stdIn.close(); clientSocket.close(); } }
-
One solution: put more code into the try block since you're not going to be using the streams if they fail to be created, and then perhaps to be cleaner, refactor the code within the try block into a method.
Another possible solution: make netout a static class variable.
I personally like the first solution better since it's generally a good thing for garbage collection and code cleanliness to locate your variables in the most limited scope necessary.Java Code:public class problem { private static PrintWriter netout = null; public static void main(String[] args) throws IOException {
I'm no socket pro, so any corrections from anyone is most welcome!
- 12-24-2009, 08:17 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Thanks again Fubarable.
I have made the PrintWriter and the BufferedReader class variables but kept the socket where it is. I've done that instead of adding to the try block because the garbage will be sitting around for the same length of time and since it right at the beginning of the program it doesn't matter too much. Although adding to the try block would make logical sense, it would in my opinion, make it less readable and since I've just come from the world of Python I do like nice looking code :)
Thanks again it now works.
Similar Threads
-
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
Listener for action "team->commit"
By feva in forum EclipseReplies: 0Last Post: 07-27-2009, 07:40 PM -
action listener on jcombobox
By chkm8 in forum New To JavaReplies: 2Last Post: 02-05-2009, 10:14 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
[SOLVED] action listener and Jtextfield
By tOpach in forum AWT / SwingReplies: 4Last Post: 12-16-2008, 01:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks