hey im having problems with java messenger application. i've made a simple gui in netbeans and a server and client class. when i create a new instance of server in gui i start a new thread and start listening in it. the problem occurs when server gets something and goes to handleConnection() where i want to use setText() on a JTextPane in another class and another thread(GUI). here's the code:
code for connect button in gui's class:
private void doConnect(java.awt.event.ActionEvent evt) {
server = new mServer(1234);
ms = new mServer(this);
Thread t = new Thread(server);
t.start();
statusLine.setText("Connection established");
}
server class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nejc;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
/**
*
* @author Nejc
*/
public class mServer implements Runnable{
private int port;
JMessenger jMsg;
public mServer(int port){
this.port=port;
}
public mServer(JMessenger in){
jMsg = in;
}
public void listen(){
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
while(true){
server = listener.accept();
handleConnection(server);
}
}
catch (IOException ioe){
System.out.println("IOException: " + ioe);
//ioe.printStackTrace();
System.exit(1);
}
}
protected void handleConnection(Socket server) throws IOException{
//DataOutputStream out = new DataOutputStream(server.getOutputStream());
final DataInputStream in = new DataInputStream(server.getInputStream());
Runnable r = new Runnable(){
@Override
public void run(){
try{
jMsg.field.setText("Mitja: " + in.readUTF());
}
catch (IOException ex){
Logger.getLogger(mServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
SwingUtilities.invokeLater(r);
//out.writeUTF(output);
/*JTextPane field = jMsg.field;
String mssg = in.readUTF();
field.setText(mssg);
*
*/
server.setKeepAlive(true);
}
@Override
public void run(){
listen();
}
}
Sorry I forgot about that:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.nejc.mServer$1.run(mServer.java:70)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)