开发者

My PrintWriter's value becomes void when used in different classes

开发者 https://www.devze.com 2023-02-26 22:31 出处:网络
I\'m new at programming and I need help with this little hurdle. I\'m making a simple chat program, with a server and a chat client. So my client starts, and creates a network connection just fine, wh

I'm new at programming and I need help with this little hurdle. I'm making a simple chat program, with a server and a chat client. So my client starts, and creates a network connection just fine, which includes a PrintWriter which sends to my server. Then, in another class (my ActionListener), when I call the writer variable, it ends up void with a NullPointerException.

So I tried calling setupnetworking each time ActionListener runs, but them I end up with to many connections and my message prints out more than once. Can I keep setupnetworking in my main() method, and keep the variable value of PrintWriter for my ActionListener class. I'm sorry if I'm confusing, so please look at my code and tell me what you think.

Here's the entire program...

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class SimpleChatClient {

    BufferedReader reader;
    PrintWriter writer;
    Socket sock;
    JTextArea chatbox = new JTextArea(10,20);   
    JTextField entertext = new JTextField(20);
    JButton sendchat = new JButton("send");

    public void go(){
        setUpNetworking();
        Thread readerThread1 = new Thread(new IncomingReader());
        readerThread1.start();
        //big probelm. to many setUpNetworkings!!!!!!!!
    }

    public class SendButtonListener implements ActionListener{
        public void actionPerformed (ActionEvent ev){
        try {
                writer.println(entertext.getText());
                writer.flush();
                System.out.println("chat flush");
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("action fail");
            }
            entertext.setText("");
            entertext.requestFocus();
        }
    }

    public void setUpNetworking() {
        try {
            sock = new Socket("192.168.0.11", 65534);
            InputStreamReader streamReader = 
                new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);
            writer = new PrintWriter(sock.getOutputStream(), true);
            System.out.println("network established. Beginning mainstream feed.");
        }
        catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("setup fail");
        }
    }


    public class IncomingReader implements Runnable {
        public void run() {
            String message;
            try {
                while ((message = reader.readLine()) != null) {
                    System.out.println("read " + message);
                    chatbox.append(message + "\n");
                    chatbox.append("\n");
                }
            }catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("run fail");
            }
        }
    }
}

P.s i have a GUI program for th开发者_高级运维is too. This just sets up the connection.


It could be a memory initialization issue with multiple threads. If the writer field is read on the EDT before it is initialized, then it may not be re-read again on that thread when initialized on your main thread.

As a quick test, add volatile to the declaration of writer.

0

精彩评论

暂无评论...
验证码 换一张
取 消