【问题标题】:PrintWriter giving null pointer exceptionPrintWriter 给出空指针异常
【发布时间】:2015-03-16 14:21:54
【问题描述】:

我正在尝试构建一个多客户端单 java 服务器模型。我正在使用线程执行此操作,因此每个客户端使用AsyncTask 有一个线程。因此,我正在使用CreateClient 类在onCreate() 方法中创建一个客户端套接字和PrintWriter 对象,然后每次按下button2,我只需将问题/文本传递给服务器即可那个特定的线程。问题是,我在SendMessage 类中得到了一个NullPointerException

这是我的客户活动:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

//import statements for client
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

//import statements for client
import android.os.AsyncTask;


public class MainActivity extends Activity implements OnClickListener {

    EditText question;

    //Client sockets
    private Socket client;
    private PrintWriter printwriter;
    private String toTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // create client as soon as the activity starts
        CreateClient clientConnection = new CreateClient();
        clientConnection.execute();

        question = (EditText)findViewById(R.id.editText1);

        Button query = (Button)findViewById(R.id.button2);
        query.setOnClickListener(this);

        ImageButton listen = (ImageButton)findViewById(R.id.button1);
        listen.setOnClickListener(this);
        listen.performClick();

    }

    private class CreateClient extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            try {
                client = new Socket("Server IP Address", 4444);

                printwriter = new PrintWriter(client.getOutputStream(), true);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();    
            }
            return null;
        }

    }

    private class CloseClient extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if(printwriter != null) {
                try {
                    printwriter.write("\n");
                    printwriter.close();
                    client.close(); // closing the connection

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

    }

    private class SendMessage extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

                printwriter.write(toTag); // GETTING NULL POINTER EXCEPTION ON THIS LINE
                printwriter.write("\n"); //delimiter

                printwriter.flush();

            return null;
        }

    }

    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.button2:
                if(validate()) //Validate is a function that validates the text
                {
                    toTag = question.getText().toString();

                    //Invoke the execute method of AsynTask, which will run the doInBackground method of SendMessage Class

                    SendMessage sendMessageTask = new SendMessage();
                    sendMessageTask.execute();

                }
                else
                {
                    //Toast error msg
                }

                break;
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        CloseClient closeConnection = new CloseClient();
        closeConnection.execute();
    }

}

【问题讨论】:

  • printwriter 显然为空。
  • 怎么样?我在创建活动时创建了一个 PrintWriter 对象。 @Kon

标签: java android eclipse client-server printwriter


【解决方案1】:

这条线很有可能

printwriter = new PrintWriter(client.getOutputStream(), true);

没有及时执行。尝试添加 println 或在其他地方初始化 printWriter。

【讨论】:

    【解决方案2】:

    printWriter 对象尚未初始化,仍然是null,导致空指针异常

    printwriter.write(toTag);// at this line -->
    

    所以在使用之前初始化printWriter=new PrintWriter();

    【讨论】:

    • 在我的 onCreate() 方法中,我调用了 clientConnection.execute();这是在这一行中为我初始化 printWriter printwriter = new PrintWriter(client.getOutputStream(), true);
    【解决方案3】:

    好吧,事实证明我的客户端套接字由于 Eclipse 中过时的 DDMS 而没有被创建。 DDMS 无法在端口 8600 上创建本地主机连接。因此,正如 Tyler 指出的那样,我的 printwriter = new PrintWriter(); 语句没有被执行。我更新了 DDMS,现在它工作得很好。

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 2014-07-19
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多