I have an async UDP receive task listening for data and then calling a callback function to return the string of data received to the main activity. I get a null pointer exception in the onPostExecute() function.
Here's the exact error from logcat:
10-28 18:14:44.277: E/AndroidRuntime(5208): FATAL EXCEPTION: main
10-28 18:14:44.277: E/AndroidRuntime(5208): java.lang.NullPointerException
10-28 18:14:44.277: E/AndroidRuntime(5208): at com.ocelot.healthviewer.UdpReceiveTask.onPostExecute(UdpReceiveTask.java:55)
10-28 18:14:44.277: E/AndroidRuntime(5208): at com.ocelot.healthviewer.UdpReceiveTask.onPostExecute(UdpReceiveTask.java:1)
10-28 18:14:44.277: E/AndroidRuntime(5208): at android.os.AsyncTask.finish(AsyncTask.java:631)
10-28 18:14:44.277: E/AndroidRuntime(5208): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-28 18:14:44.277: E/AndroidRuntime(5208): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-28 18:14:44.277: E/AndroidRuntime(5208): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 18:14:44.277: E/AndroidRuntime(5208): at android.os.Looper.loop(Looper.java:158)
10-28 18:14:44.277: E/AndroidRuntime(5208): at android.app.ActivityThread.main(ActivityThread.java:5751)
10-28 18:14:44.277: E/AndroidRuntime(5208): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 18:14:44.277: E/AndroidRuntime(5208): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 18:14:44.277: E/AndroidRuntime(5208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-28 18:14:44.277: E/AndroidRuntime(5208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-28 18:14:44.277: E/AndroidRuntime(5208): at dalvik.system.NativeStart.main(Native Method)
The interface is here
public interface OnTaskCompleted {
public void onTaskCompleted();
}
Here's the UDP receive Task code
public class UdpReceiveTask extends AsyncTask<Void, Void, String> {
private OnTaskCompleted listener;
int port = 25566;
public boolean stop = false;
DatagramSocket socket;
DatagramPacket packet;
byte[] message_buffer;
String data;
public String output;
@Override
protected void onPreExecute()
{
message_buffer = new byte[512];
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
packet = new DatagramPacket(message_buffer, message_buffer.length);
}
@Override
protected String doInBackground(Void... params) {
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
data = new String(message_buffer, 0, packet.getLength());
return data;
}
@Override
protected void onPostExecute(String output) {
output = data;
listener.onTaskCompleted();
}
}
Here is my main activity:
public class MainActivity extends Activity implements OnTaskCompleted {
public TextView tv1;
boolean done = false;
UdpReceiveTask receiveTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 =(TextView)findViewById(R.id.textView1);
receiveTask = new UdpReceiveTask();
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void StartReceive(View view)
{
if(done == false)
{
receiveTask.execute();
done = true;
}
}
@Override
public void onTaskCompleted() {
String data = receiveTask.output;
tv1.append(data);
}
}
Thanks in advance - Adrian
EDIT I got rid of the first null pointer error thanks to codeMagic but now I get another null pointer exception to do with the text view:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TextView.append(TextView.java:3393)
at com.ocelot.healthviewer.MainActivity.onTaskCompleted(MainActivity.java:47)