Exception in thread "Thread-6" java.lang.NullPointerException

1 reply [Last post]
Offline
Joined: 05/06/2012

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author ayush
 */

public class Textchat extends javax.swing.JFrame implements Runnable {

    /**
     * Creates new form Textchat
     */
 
   watch w=new watch();
    String ip=Voip.str;
   
        String s;

        Thread t;
    Socket client;
    Textchat(Socket client)
    {
        this.client=client;
        t=new Thread(this);
        t.start();   
    }
 
    public void run()
    {
        try {
           
           
           
            br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            while(true)
                        {
                            String st1="";
                            st1=br.readLine();
                       // jList1.addItem(st1);                    
                      
                    jTextArea1.append(st1);
                            System.out.println(st1);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Textchat.class.getName()).log(Level.SEVERE, null, ex);
                }
                        }
        } catch (IOException ex) {
            //Logger.getLogger(Textchat.class.getName()).log(Level.SEVERE, null, ex);
        }
        }   
    BufferedReader br;
  //  BufferedWriter bw;
    PrintStream ps;
    public Textchat()throws IOException {
        
            initComponents();
      
         chat();
      
         GregorianCalendar c =new GregorianCalendar();
         int d,m,y;
         d=c.get(Calendar.DATE);
         m=c.get(Calendar.MONTH);
         y=c.get(Calendar.YEAR);
       
        
         String t=(""+d+" "+" / "+m+" / "+y);
         jLabel1.setText(t);
  
  
    }
   void type(Socket s1) throws IOException
    {
     
    //bw = new BufferedWriter(new OutputStreamWriter(
                        //s1.getOutputStream()));   
        ps=new PrintStream(s1.getOutputStream());
    }
public void chat()
{
         if(ip=="")
         {
            try {
                ServerSocket server=new ServerSocket(1235);
                System.out.println("waiting for request from client ");
                client=server.accept();
                System.out.println("request accepted");
                Textchat ss=new Textchat(client);
                type(client);
                    NickName nn=new NickName();
                    nn.setVisible(true); 
            } catch (IOException ex) {
                Logger.getLogger(Textchat.class.getName()).log(Level.SEVERE, null, ex);
            }
         
         }else
         {
            try {
                System.out.println("sending requst to the server");
                client=new Socket(ip,1235);
                System.out.println("successfully connected");
                Textchat ss=new Textchat(client);
                type(client);
                    NickName nn=new NickName();
                    nn.setVisible(true);
                   } catch (UnknownHostException ex) {
                Logger.getLogger(Textchat.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Textchat.class.getName()).log(Level.SEVERE, null, ex);
            }
         
         }
     
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setBounds(new java.awt.Rectangle(250, 250, 0, 0));
        setResizable(false);

        jButton1.setText("Send");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jButton2.setText("DissConnect");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 2, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 153, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
              try{
                         String nick=NickName.nick_name;
                        ps.println(nick+":-"+jTextField1.getText());
             //bw.newLine();
                         //bw.flush();
                 jTextArea1.append("\n"+nick+":-"+jTextField1.getText());
                         System.out.println(nick+":-"+jTextField1.getText());
                         jTextField1.setText("");
             }catch(Exception m){}
         
       
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        try {
            JOptionPane.showMessageDialog(this,"You are now Disconnected......");
               client.close();
               this.dispose();
               Voip v=new Voip();
           v.setVisible(true);
            // TODO add your handling code here:
        } catch (IOException ex) {
            Logger.getLogger(Textchat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                       

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                  
}

 

 

my code is above i found following exception in dark line.

pls help,.....

 

sending requst to the server
successfully connected
Exception in thread "Thread-6" java.lang.NullPointerException
    at Textchat.run(Textchat.java:55)
    at java.lang.Thread.run(Thread.java:722)
 

Offline
Joined: 12/13/2010
When you get a

When you get a nullpointerexecption, it means that you are trying to access an object that has not been allocated (new...).  Looks to me the order of exectuion is suspect, and you are tryint to access jTextArea1 object before the allocation occurs.  Check the rest of your code and see if that helps.