AS上写了一个聊天室客户端报错java.lang.String java.io.BufferedReader.readLine()

图片说明

求大神看一下 在网上也没有搜到报这个readLine错误的 是一个聊天室的客户端MainAcitivity的代码如下

package com.example.mysocketclient;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    EditText editTextIp;
    EditText editTextContent;
    TextView textViewContent;
    Button btnConnect;
    Button btnSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextIp = findViewById(R.id.editTextIp);
        editTextContent = findViewById(R.id.editTextContent);
        textViewContent = findViewById(R.id.textViewContent);
        btnConnect =findViewById(R.id.btnConnect);
        btnSend = findViewById(R.id.btnSend);

        btnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                connect();
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                send();
            }
        });

    }

    //-----------------------------------

    Socket socket = null;
    BufferedWriter writer = null;
    BufferedReader reader = null;

    public void connect() {
            AsyncTask<Void, String, Void> read = new AsyncTask<Void, String, Void>() {

                @SuppressLint("WrongThread")
                @Override
                protected Void doInBackground(Void... voids) {
                    try {
                        socket = new Socket(editTextIp.getText().toString(),12345);
                        writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        publishProgress("@success");
                    } catch (UnknownHostException e1) {
                        //Toast.makeText(MainActivity.this, "无法建立链接", Toast.LENGTH_SHORT).show();
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        //Toast.makeText(MainActivity.this, "无法建立链接", Toast.LENGTH_SHORT).show();
                        e1.printStackTrace();
                    }
                    try {
                        String line;
                        while ( (line = reader.readLine()) != null ) {
                            publishProgress(line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onProgressUpdate (String...values){
                    if (values[0].equals("@success")) {
                        Toast.makeText(MainActivity.this, "成功建立链接", Toast.LENGTH_SHORT).show();
                    }
                    textViewContent.append("别人说: "+values[0]+"\n");
                    super.onProgressUpdate(values);
                }
            };
            read.execute();
    }


    public void send () {
        try {
            textViewContent.append("我说: "+editTextContent.getText().toString()+"\n");
            writer.write(editTextContent.getText().toString()+"\n");
            writer.flush();
            editTextContent.setText("");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1个回答

空指针异常,你的reader是空的,进而readline()就会报错,仔细检查你初始化reader的代码

weixin_40031735
weixin_40031735 谢谢 解决了 我把readLine那里的读取放在了创建缓冲流后面 就不出现这个异常了
6 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

2
springboot security oauth2 报错 java.lang.IllegalArgumentException: URI must not be null
4
使用dbcp连接数据库报错java.lang.NumberFormatException: For input string: "20 "?
2
myeclipse报错java.lang.ClassCastException: java.lang.Integer cannot be cast to org.model.Login
4
java.lang.StringIndexOutOfBoundsException报错的原因?
0
MediaRecorder录屏,Socket传输录屏内容报错java.lang.IllegalStateException
1
格式化日期在8.0手机报java.lang.AssertionError
3
Exception in thread "main" java.lang.NoClassDefFoundError,有导入对应jar包,但显示无法找到对应类?
2
springboot报错java.lang.NoClassDefFoundError
1
阿里云OSS对象创建时报错“java.lang.ClassNotFoundException: com.aliyun.oss.OSSClient”
2
Runnable 中抛出java.lang.ArrayIndexOutOfBoundsException: -1
1
flume开启报错java.lang.SecurityException: sealing violation: package org.apache.flume.conf is sealed
1
weblogic启动报错java.lang.NoClassDefFoundError: weblogic/security/SecurityLogger
1
调用webservice报错SOAPFaultException: java.lang.NullPointerException
4
新人求解:JAVA导入外部包后提示java.lang.NoClassDefFoundError
1
AndroidStudio 编程中出现java.lang.RuntimeException错误
1
使用jaxrs-ri-2.25.1做restful报错:java.lang.ClassNotFoundException
3
@Autowired 报错java.lang.NullPointerException
5
Exception in thread "Thread-12" java.lang.RuntimeException: java.lang.NullPointerException
5
字符串转Double 报错java.lang.NumberFormatException
2
spring cloud zuul 使用注解@EnableZuulProxy项目启动报错,报java.lang.IllegalStateException!