Results 1 to 5 of 5
  1. #1
    cog
    cog is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Question 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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"); 
                }
            };

  3. #3
    cog
    cog is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    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();
        }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.
    Java Code:
    public class problem {
      private static PrintWriter netout = null;
      
      public static void main(String[] args) throws IOException {
    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.

    I'm no socket pro, so any corrections from anyone is most welcome!

  5. #5
    cog
    cog is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    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

  1. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM
  2. Listener for action "team->commit"
    By feva in forum Eclipse
    Replies: 0
    Last Post: 07-27-2009, 07:40 PM
  3. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •