Bookmark Topic Watch Topic
  • New Topic

How to activate threads in Swings.

 
mvs raman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hai Frds,
I'm using NetBeans,i'm getting Null Pointer exception after running, wht to do help frds...

package networking;
import java.io.*;
import java.net.*;
class Ser extends javax.swing.JFrame implements Runnable
{
ServerSocket server;
Socket client;
ObjectOutputStream output;
ObjectInputStream input;
public Ser()
{
super("SERVER DISPLAY");
initComponents();
setVisible(true);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

PortNo = new javax.swing.JLabel();
PortNoTF = new javax.swing.JTextField();
Start = new javax.swing.JButton();
Message = new javax.swing.JLabel();
MessageTF = new javax.swing.JTextField();
Send = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
ServerDisplayTA = new javax.swing.JTextArea();
DisConnect = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

PortNo.setText("PortNo");

Start.setText("Start");
Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartActionPerformed(evt);
}
});

Message.setText("Message");

MessageTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MessageTFActionPerformed(evt);
}
});

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

jLabel1.setText("SERVER DISPLAY");

ServerDisplayTA.setColumns(20);
ServerDisplayTA.setRows(5);
jScrollPane1.setViewportView(ServerDisplayTA);

DisConnect.setText("DisConnect");
DisConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DisConnectActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(PortNo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Message, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(PortNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Start))
.addGroup(layout.createSequentialGroup()
.addComponent(MessageTF, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Send))))
.addGroup(layout.createSequentialGroup()
.addGap(182, 182, 182)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(183, 183, 183)
.addComponent(DisConnect)))
.addContainerGap(140, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PortNo)
.addComponent(PortNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Start))
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Message)
.addComponent(MessageTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Send))
.addGap(40, 40, 40)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(DisConnect))
);

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

private void MessageTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void StartActionPerformed(java.awt.event.ActionEvent evt) {
ServerDisplayTA.append("Waiting for Server "+"\n");
try
{
server=new ServerSocket(1234,100);
client=server.accept();
ServerDisplayTA.append("Server Accepted the Connection"+"\n");
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("Server Could not Connect"+"\n");
}

// TODO add your handling code here:
}

private void SendActionPerformed(java.awt.event.ActionEvent evt) {
String mesg=MessageTF.getText();
try
{
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
output.writeObject(mesg);
ServerDisplayTA.append("SERVER>>"+mesg+"\n");
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("Server Could not Send"+"\n");
}
// TODO add your handling code here:
}

private void DisConnectActionPerformed(java.awt.event.ActionEvent evt) {
try
{
server.close();
client.close();
output.close();
input.close();
ServerDisplayTA.append("Server Terminated Application"+"\n");
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("Server Cannot Terminate During Communication"+"\n");
}

}
public void run()
{
String msg;
try
{
input=new ObjectInputStream(client.getInputStream());
msg=(String)input.readObject();
ServerDisplayTA.append("CLIENT>>"+msg+"\n");
run();
}
catch(IOException e)
{
e.printStackTrace();
ServerDisplayTA.append("IO Exception During Termination"+"\n");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
ServerDisplayTA.append("IO Exception During Termination"+"\n");
}
catch(NullPointerException e)
{
e.printStackTrace();
ServerDisplayTA.append("Exception in run");
}
}


// Variables declaration - do not modify
private javax.swing.JButton DisConnect;
private javax.swing.JLabel Message;
private javax.swing.JTextField MessageTF;
private javax.swing.JLabel PortNo;
private javax.swing.JTextField PortNoTF;
private javax.swing.JButton Send;
private javax.swing.JTextArea ServerDisplayTA;
private javax.swing.JButton Start;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration

}
public class Server
{
public static void main(String args[])
{
Ser ob=new Ser();
Thread th=new Thread(ob);
th.start();
}
}

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!