gpt4 book ai didi

java - 客户端-服务器应用程序空指针异常

转载 作者:行者123 更新时间:2023-12-02 08:19:12 25 4
gpt4 key购买 nike

我正在开发一个小型项目,其中包括制作客户端-服务器聊天应用程序。该应用程序有一个 GUI,我的问题是我试图从 GUI 获取信息并连接到该服务器,但我收到一个空指针异常。到目前为止,我已经有了客户端的代码(服务器在没有 GUI 客户端的情况下进行了测试,并且工作完美)。

GUI 类:

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();
}
}

连接主机的线程:

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;
}
}

主要内容:

package tema09client;
public class Main {
public static void main(String[] args) {
GUI fereastra=new GUI();
fereastra.setVisible(true);
}

}

我认为这是堆栈跟踪:

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)

等。这是 GUI 中的第 124 行:test=new Comunicare(host, Integer.getInteger(port));

有人可以帮我吗? PS:当我按下“连接”按钮时,会引发空指针异常。

最佳答案

使用Integer.parseInt(port)而不是Integer.getInteger(port)Integer.getInteger(port)用于从 port 指定的系统属性中获取整数,由于它返回 null,因此您需要 unboxInteger 转换为 int,您将得到 NPE。

此外,您可能会在代码中的许多地方得到 NPE,因为您几乎没有检查空值。您使用 outinsocketbuffer 而不检查是否为 null。

关于java - 客户端-服务器应用程序空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com