PrintWriter给出空指针异常

时间:2015-03-16 14:21:54

标签: java android eclipse client-server printwriter

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

这是我的客户行动:

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

}

3 个答案:

答案 0 :(得分:1)

可能是那条线

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

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

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