-3

I am a newbie to Java and I'm creating a chat client which can send and receive text. But I'm stuck here:

exception-at Netr$IncomingReader.run(Netr.java:88) at java.lang.Thread.run(Thread.java:745) java.lang.NullPointerException

The application terminates with a java.lang.NullPointerException when it's getting executed. Please help. Thanks in advance.

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
public class Netr {
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
JScrollPane qscroller;
public static void main(String[]args)
{
Netr ab=new Netr();
ab.go();
}
public void go()
{
JFrame frame=new JFrame("my first client baby");
JPanel panel=new JPanel();
incoming=new JTextArea();
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
qscroller=new JScrollPane(incoming);
qscroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qscroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing=new JTextField(20);
JButton sendbutton=new JButton("Send");
sendbutton.addActionListener(new SendButtonListener());
panel.add(qscroller);
panel.add(sendbutton);
panel.add(outgoing);
setupnetworking();
IncomingReader thef=new IncomingReader();
Thread abc=new Thread(thef);
abc.start();
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(400,500);
frame.setVisible(true);}


private void setupnetworking()
{
try
{
    ServerSocket serversock=new ServerSocket(3311);
    sock =new Socket("100.68.56.116",3311);
    Socket sock1=serversock.accept();
    InputStreamReader reader=new InputStreamReader(sock.getInputStream());
    BufferedReader streamreader=new BufferedReader(reader);
    writer=new PrintWriter(sock.getOutputStream());
    System.out.println("newtorking established");
}
catch(IOException ex)
{
    ex.printStackTrace();
}
}
public class SendButtonListener implements ActionListener
{public void actionPerformed(ActionEvent ev)
{
try
{
    writer.println(outgoing.getText());
    writer.flush();
}
catch(Exception ex)
{
    ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
public class IncomingReader implements Runnable
{
private BufferedReader streamreader;

public void run()
{
    String message;
    try
    {
        while((message= streamreader.readLine())!=null)
        {
            System.out.println("read"+message);
            incoming.append(message+"\n");

        }
}catch(Exception ex)
    {
    ex.printStackTrace();
    }
    }
}

}
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
Abhishek
  • 49
  • 9

1 Answers1

0

You haven't initialized streamreader variable in IncomingReader class. Without initializing it, run() method makes readLine() call on it. That's why you get NPE

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • May be because of duplicate answers available, but not sure if it's fair – Nabin Aug 11 '14 at 11:23
  • intialized it ,but getting same error – Abhishek Aug 11 '14 at 11:28
  • I'm also not sure if it deserves a down vote. But, still i think it helps OP's problem. @user3571223 can you show us what is the change you did? – Keerthivasan Aug 11 '14 at 11:33
  • i inintialed the stream reader to null and getting this exception.java.net.ConnectException: Connection timed out: connect java.lang.NullPointerException – Abhishek Aug 11 '14 at 11:37
  • could u please @keerthivasan run on your ide and tell where i am going wrong..just by displaying that specific part of code which you edited..i shall b highly thankful to you for your response – Abhishek Aug 11 '14 at 11:41