NullPointerException when trying to send a message to a Client Socket
I have a ServerHandler class, which creates a new PrintWriter and stores
it into a PrintWriter array, The message gets sent by giving the
selectedId integer the ID number of the client that should receive the
message. When i call the SendMessage() method inside the ServerHandler
class it sends the message without a problem, but when i try to call the
SendMessage("some message") method from the class that contains the GUI it
gives me a NullPointerException.
The posljiSporocilo() is the actual SendMessage() because some methods are
in my language
import java.net.*;
import java.io.*;
import java.awt.*;
public class ServerHandler implements Runnable{
Socket klientSocket;
static int userCounter = 0;
static int selectedId = 0;
BufferedReader reader;
static PrintWriter writer;
static PrintWriter[] writerHolder = new PrintWriter[10];
static outputHelper out = new outputHelper();
public ServerHandler(Socket klientSocket) throws IOException
{
userCounter++;
this.klientSocket = klientSocket;
writer = new PrintWriter(this.klientSocket.getOutputStream());
writerHolder[userCounter] = writer;
InputStreamReader inReader = new
InputStreamReader(this.klientSocket.getInputStream());
reader = new BufferedReader(inReader);
out.frameOutput(this.klientSocket.getInetAddress().toString(), "Server
sprejel povezavo:");
selectedId = 1;
posljiSporocilo("AutoSent");
When i call the posljiSporocilo() method in the actual class it does send
the message to the Client.
}
public ServerHandler(){
// Do not launch the main Constructor
}
public void run(){
String inMessage = null;
try{
while((inMessage = reader.readLine()) != null){
System.out.println("Server Sprejel: " + inMessage);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
public void selectId(int id){
selectedId = id;
out.frameOutput("ID Changed to: " + selectedId, "ID change");
}
public void posljiSporocilo(String message){
try
{
writerHolder[selectedId].println(message);
writerHolder[selectedId].flush();
}catch(Exception ex)
{
out.frameOutput("ERROR Trying to send a message", "ERR_MESSAGE:");
ex.printStackTrace();
}
}
}
The class that contains the GUI stuff:
package homeControl;
IMPORTS
public class mainWindow extends JFrame {
private JPanel contentPane;
private JTextField messageTextField;
ServerHandler svr = new ServerHandler();
outputHelper o = new outputHelper();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainWindow frame = new mainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
JMenu mnId = new JMenu("ID");
menuBar.add(mnId);
JMenuItem idEnaItem = new JMenuItem("ID: 1");
mnId.add(idEnaItem);
idEnaItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(event.getActionCommand() != null){
svr.selectId(1);
setTitle("ID changed to 1");
}
}
});
JMenuItem idDvaItem = new JMenuItem("ID: 2");
mnId.add(idDvaItem);
idDvaItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(event.getActionCommand() != null){
svr.selectId(2);
setTitle("ID changed to 2");
}
}
});
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
messageTextField = new JTextField();
messageTextField.setBounds(10, 209, 292, 20);
contentPane.add(messageTextField);
messageTextField.setColumns(10);
JButton sendButton = new JButton("Send");
sendButton.setBounds(312, 208, 89, 23);
contentPane.add(sendButton);
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(event.getActionCommand() != null)
{
svr.posljiSporocilo("THIS MESSAGE DOES NOT GET SENT");
}
When i call the posljiSporocilo() method from this class it gives me a
NullPointerException
}
});
}
}
i hope i gave you enough information, Thanks!
No comments:
Post a Comment