I am trying to create multiple client single Java server models. I am using these threads, so I have a customer per thread using the AsyncTask
. Thus, I am creating a customer socket and PrintWriter
object method is pressed into the
CreateClient
category and then every time the button2
, I get the question / text pass on the server for that particular thread Have to do. The problem is, I'm getting NullPointerException
in the SendMessage
class.
This is my client Actvity:
import java.util.ArrayList; Import java.util.Arrays; Import java.util.Locale; // import statement 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 Import android.content.Intent; Importroid.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 Windows Manager; Import android.widget.Button; Import android.widget.EditText; Import android.widget.ImageButton; Import android.widget.Toast; // Import Imported Client for Android. OS.Sync Task; OnClickListener {edit text question; on implementation of public class main activity activity; // client socket private socket client; Private PrintWriter PrintRighters; Private string breaks; @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_main); // Create the Client as soon as the activity starts Client Client Connection = New CreateClient (); ClientConnection.execute (); Question = (Edit Text) Search ViewById (R.id.editText1); Button query = (button) FindById (R.id.button2); Query.setOnClickListener (this); Find ImageButton = (Image Button) Find VVBID (R.B.ton1); Listen.setOnClickListener (this); Listen.performClick (); } Private Class CreateClient AsyncTask extension & lt; Zero, zero, zero> {@Override Safe Volume Doine Background (Zero ... Parameters) {try {client = new socket ("server IP address", 4444); Printwriter = new printritter (client.getOutputStream (), true); } Hold (unknownhostexception e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); } Return tap; }} Private class CloseClient AsyncTask is spreading & lt; Zero, zero, zero> {@Override Safe Volume Doine Background (Zero ... Parameter) {If (printwriter! = Null) {try {printwriter.write ("\ n"); Printwriter.close (); Client.close (); // close the connection} hold (unknownhostexception e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); }} Return tap; }} Private class SendMessage AsyncTask and lt; Zero, zero, zero> {@Override Safe Volume DownBeckground (Zero ... Parameter) {printwriter.write (toTag); // On this line printwriter.write ("\ n") received a notation marking exception; // delimiter printwriter.flush (); Return tap; }} Public void onClick (switch vv) {switch (v.getId ()) {case R.id.button2: if (validate ()) // Validate is a function that reads {toTag = question.getText () Validates .tar (); // Invoke AsynTask's execution method, which will run the doInBackground method of SendMessage Class SendMessage sendMessageTask = new SendMessage (); SendMessageTask.execute (); } And {// toast error msg} break; }} @ Override Backup on Public Zero (super.onBackpressed (); CloseClient closeConnection = New CloseClient (); CloseConnection.execute (); }}
chances are that line
printwriter = New printritter (client.getOutputStream (), true);
Not being executed on time Try adding a println or starting printWriter elsewhere.
No comments:
Post a Comment