Client-server application null pointer exception
I am working on a mini-project which consists of doing a client-server chat application. The application has a GUI and my problem is that I am trying to get the information from GUI and connect on that server, but I receive a null pointer exception. So far I have this code for the client(the server was tested without GUI client and works perfectly).
The GUI class:
package tema09client;
public class GUI extends javax.swing.JFrame {
Comunicare test;
String host, port, uname;
int init=0;
public GUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Network chat");
setBackground(new java.awt.Color(255, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Pentru a va loga selectati host-ul si portul, apoi scrieti-va username-ul in causta si apasati OK.");
jLabel2.setText("HOST");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel3.setText("PORT");
jLabel4.setText("USERNAME");
jButton1.setText("Connect");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(79, 79, 79)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(55, 55, 55)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(132, 132, 132)
.addComponent(jButton1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(84, 84, 84)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)))))
.addGap(81, 81, 81))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(185, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
if(init==0){
host=jTextField1.getText();
port=jTextField2.getText();
System.out.println(host+" "+port);
if((host!=null) && (port!=null)){
test=new Comunicare(host, Integer.getInteger(port));
test.start();
init=1;
}
}
test.setMesaj("cheieloginStep1");
uname=jTextField3.getText();
test.setUname(uname);
}catch (NullPointerException e) {
System.out.println(e);
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
public String getHost(){
return jTextField1.getText();
}
public String getPort(){
return jTextField2.getText();
}
public String getUsername(){
return jTextField2.getText();
}
}
The thread which connects to host:
package tema09client;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Comunicare extends Thread{
Socket client=null;
PrintWriter out=null;
BufferedReader in=null;
boolean listen=true;
String username=null;
String mesaj=null;
public Comunicare(String host, int port){
try {
client = new Socket(host, port);
out=new PrintWriter(client.getOutputStream());
in=new BufferedReader(new InputStreamReader(client.getInputStream()));
//out.println("loginStep1");
} catch (UnknownHostException ex) {
Logger.getLogger(Comunicare.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Comunicare.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run(){
String buffer;
String line;
while(listen){
try {
out.flush();
buffer = in.readLine();
if(buffer.startsWith("cheie")){
System.out.println("Serverul a trimis: "+buffer);
if(buffer.equals("cheieOKStep1")){
out.println("cheieloginStep2");
System.out.println("Am trimis: loginStep2");
out.flush();
logare();
//out.flush();
}
if(buffer.equals("cheieLoggedIn")){
System.out.println("Conectat! ... se incarca lista de prieteni online.");
}
if(buffer.equals("cheieFail"))
System.out.println("Username-ul nu este in baza de date.");
if(buffer.equals("cheieActualizarePrieteni"))
System.out.println("Acum se adauga prietenii in lista");
}
if(mesaj.startsWith("cheie")){
if(mesaj.equals("cheieloginStep1"))
out.println(mesaj);
}
} catch (IOException ex) {
System.out.println(ex);
}
}
}
private void logare(){
out.println(username);
System.out.println("Am trimis: "+username);
}
public void setUname(String user){
username=user;
}
public void setMesaj(String mesaj){
this.mesaj=mesaj;
}
}
And the main:
package tema09client;
public class Main {
public static void main(String[] args) {
GUI fereastra=new GUI();
fereastra.setVisible(true);
}
}
I think this is the stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tema09client.GUI.jButton1ActionPerformed(GUI.java:124)
at tema09client.GUI.access$100(GUI.java:2)
at tema09client.GUI$2.actionPerformed(GUI.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
ETC. This is line 124 from GUI: test=new Comunicare(host, Integer.getInteger(port));
Can anyone help me please? PS: The null pointer exception fires when I press "Connect" button.开发者_高级运维
Use Integer.parseInt(port)
and not Integer.getInteger(port)
. Integer.getInteger(port)
is used to get integer from the system property specified by port
, and since it returns null and you need to unbox the Integer
to int
, you get NPE.
Moreover, you might get NPE in many places in your code, since you don't check for null values almost nowhere. you use out
, in
, socket
and buffer
without checking if null.
精彩评论