鱼C论坛

 找回密码
 立即注册
查看: 3809|回复: 5

空指针异常

[复制链接]
发表于 2021-7-22 14:52:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 不会起名字的我 于 2021-7-22 15:00 编辑

我写了一个TCP程序,但是报空指针异常,客户端我发了消息还是一样,代码如下

服务器代码
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.*;

  5. import javax.swing.*;
  6. import java.net.*;

  7. public class Start extends JFrame {
  8.         boolean isStart = false;
  9.         JTextArea outtext;
  10.         JTextField port;
  11.         ServerSocket server;
  12.         Socket client;

  13.         public Start() {
  14.                 setVisible(true);
  15.                 setDefaultCloseOperation(EXIT_ON_CLOSE);
  16.                 setSize(500, 300);
  17.                 setTitle("服务器");
  18.                 setLayout(new BorderLayout());
  19.                 Container container = getContentPane();

  20.                 JPanel panel1 = new JPanel();
  21.                 JLabel serverport = new JLabel("端口");
  22.                 JTextField ip = new JTextField(10);
  23.                 port = new JTextField(10);

  24.                 JToolBar tool = new JToolBar();

  25.                 JButton start = new JButton("启动服务器");
  26.                 JButton close = new JButton("关闭服务器");

  27.                 close.addActionListener(new ActionListener() {

  28.                         public void actionPerformed(ActionEvent e) {
  29.                                 try {
  30.                                         if (isStart) {
  31.                                                 print("正在关闭服务器···");
  32.                                                 server.close();
  33.                                                 isStart = false;
  34.                                                 print("服务器已经关闭。");
  35.                                         } else {
  36.                                                 print("警告:服务器已关闭。");
  37.                                         }
  38.                                 } catch (IOException e1) {
  39.                                         e1.printStackTrace();
  40.                                 }
  41.                         }
  42.                 });

  43.                 start.addActionListener(new ActionListener() {

  44.                         public void actionPerformed(ActionEvent arg0) {
  45.                                 new open().start();
  46.                         }
  47.                 });

  48.                 tool.add(start);
  49.                 tool.addSeparator();
  50.                 tool.add(close);
  51.                 tool.setFloatable(false);

  52.                 container.add(tool, BorderLayout.NORTH);
  53.                 panel1.setLayout(new BorderLayout());
  54.                 JPanel panel2 = new JPanel();
  55.                 panel2.add(serverport);
  56.                 panel2.add(port);
  57.                 panel1.add(panel2, BorderLayout.NORTH);

  58.                 outtext = new JTextArea();
  59.                 outtext.setEditable(false);
  60.                 JScrollPane js = new JScrollPane(outtext);
  61.                 panel1.add(js, BorderLayout.CENTER);

  62.                 container.add(panel1, BorderLayout.CENTER);

  63.                 validate();
  64.         }

  65.         class open extends Thread {
  66.                 @Override
  67.                 public void run() {
  68.                         print("正在启动服务器···");
  69.                         int port1 = new Integer(port.getText());
  70.                         try {
  71.                                 server = new ServerSocket(port1);
  72.                                 isStart = true;
  73.                                 new input().start();
  74.                                 print("服务器已经启动。");

  75.                         } catch (IOException e) {
  76.                                 e.printStackTrace();
  77.                         }
  78.                 }
  79.         }

  80.         class connect extends Thread {
  81.                 public void run() {
  82.                         while (true) {
  83.                                 try {
  84.                                         client = server.accept();
  85.                                 } catch (IOException e) {
  86.                                         e.printStackTrace();
  87.                                 }
  88.                         }
  89.                 }
  90.         }

  91.         class input extends Thread {
  92.                 @Override
  93.                 public void run() {
  94.                         while (isStart) {
  95.                                 try {
  96.                                         InputStream fis = client.getInputStream();//                        这里出错了
  97.                                         byte[] info = new byte[409600];
  98.                                         int count ;
  99.                                         while((count = fis.read(info))!=-1) {
  100.                                                 String str = new String(info, 0, count);
  101.                                                 System.out.println(str);
  102.                                                
  103.                                         }
  104.                                 } catch (Exception e) {
  105.                                         e.printStackTrace();
  106.                                 }
  107.                         }
  108.                 }
  109.         }

  110.         void print(String message) {
  111.                 outtext.append(message + "\n");
  112.         }

  113.         public static void main(String[] args) {
  114.                 new Start();
  115.         }

  116. }
复制代码



客户端代码

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;

  7. import javax.swing.*;
  8. import java.net.*;

  9. public class Start extends JFrame {
  10.         JTextArea area;
  11.         JTextField inputip;
  12.         JTextField inputport;
  13.         Socket client;
  14.         boolean lianjie1 = false;

  15.         public Start() {
  16.                 setVisible(true);
  17.                 setDefaultCloseOperation(EXIT_ON_CLOSE);
  18.                 setSize(600, 400);
  19.                 setTitle("Java Chat");
  20.                 setLayout(new BorderLayout());
  21.                 Container container = getContentPane();
  22.                 JLabel ip = new JLabel("服务器IP");
  23.                 JLabel port = new JLabel("        端口");
  24.                 inputip = new JTextField(10);
  25.                 inputport = new JTextField(10);

  26.                 JButton lianjie = new JButton("连接");
  27.                 lianjie.addActionListener(new ActionListener() {

  28.                         public void actionPerformed(ActionEvent arg0) {
  29.                                 new lianjieServer().start();
  30.                         }
  31.                 });

  32.                 JPanel panel1 = new JPanel();
  33.                 panel1.add(ip);
  34.                 panel1.add(inputip);
  35.                 panel1.add(port);
  36.                 panel1.add(inputport);
  37.                 panel1.add(lianjie);
  38.                 container.add(panel1, BorderLayout.NORTH);

  39.                 JPanel panel2 = new JPanel();
  40.                 panel2.setLayout(new BorderLayout());
  41.                 area = new JTextArea();
  42.                 JScrollPane js = new JScrollPane(area);
  43.                 panel2.add(js, BorderLayout.CENTER);

  44.                 container.add(panel2, BorderLayout.CENTER);

  45.                 JLabel send1 = new JLabel("发送:");
  46.                 JButton Send = new JButton("发送");
  47.                 JTextField send = new JTextField(30);
  48.                 JPanel panel3 = new JPanel();
  49.                 panel3.add(send1);
  50.                 panel3.add(send);
  51.                 panel3.add(Send);
  52.                
  53.                 Send.addActionListener(new ActionListener() {
  54.                        
  55.                         public void actionPerformed(ActionEvent e) {
  56.                                 print(lianjie1);
  57.                                 if(lianjie1) {
  58.                                         try {
  59.                                                 OutputStream fot = client.getOutputStream();
  60.                                                 byte a[] = send.getText().getBytes();
  61.                                                 fot.write(a);
  62.                                         } catch (IOException e1) {
  63.                                                 e1.printStackTrace();
  64.                                         }
  65.                                 }
  66.                         }
  67.                 });

  68.                 area.setEditable(false);

  69.                 container.add(panel3, BorderLayout.SOUTH);

  70.                 validate();

  71.         }

  72.         class lianjieServer extends Thread {
  73.                 public void run() {
  74.                         print("正在尝试连接···");
  75.                         try {
  76.                                 if (!(inputip.getText() == "")) {
  77.                                         int PORT = Integer.valueOf(inputport.getText());
  78.                                         client = new Socket(inputip.getText(), PORT);
  79.                                         lianjie1 = true;
  80.                                         print("连接成功!");
  81.                                 } else {
  82.                                         print("无效的端口。");
  83.                                 }

  84.                         } catch (Exception e) {
  85.                                 print("连接失败!");
  86.                         }
  87.                 }
  88.         }

  89.         void print(Object message) {
  90.                 area.append(message + "\n");
  91.         }

  92.         public static void main(String[] args) {
  93.                 new Start();
  94.         }
  95. }
复制代码


请问哪里出了问题?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-22 15:28:32 | 显示全部楼层
我没有复现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-22 15:34:35 | 显示全部楼层
  1. while (isStart) {
  2.                                 try {
  3.                                         InputStream fis = client.getInputStream();//                        这里出错了
  4.                                         byte[] info = new byte[409600];
  5.                                         int count ;
  6.                                         while((count = fis.read(info))!=-1) {
  7.                                                 String str = new String(info, 0, count);
  8.                                                 System.out.println(str);
  9.                                                
  10.                                         }
  11.                                 } catch (Exception e) {
  12.                                         e.printStackTrace();
  13.                                 }
  14.                         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-25 22:24:29 From FishC Mobile | 显示全部楼层
服务端没有 new connect。client 没有初始化。

题外话,写代码写的一言难尽
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-7-28 10:31:42 | 显示全部楼层
赚小钱 发表于 2021-7-25 22:24
服务端没有 new connect。client 没有初始化。

题外话,写代码写的一言难尽

不会这方面的东西
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-28 23:11:20 From FishC Mobile | 显示全部楼层
连帅帅 发表于 2021-7-28 10:31
不会这方面的东西

了解 tcp 与多线程的原理与用法吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 08:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表