0

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)
orangeocelot
  • 127
  • 8

1 Answers1

2

It looks like listener is null. Try adding

receiveTask.listener = this;

after

receiveTask = new UdpReceiveTask();

In your Activity

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • the NPE is in `onPostexecute` in asynctask listener is not initialized! – Raghunandan Oct 28 '13 at 18:37
  • @Raghunandan I know but that would initialize it after creating an instance of the task in the `Activity` before executing it – codeMagic Oct 28 '13 at 18:38
  • but `OnTaskCompleted` is an interface in asynctask – Raghunandan Oct 28 '13 at 18:40
  • @Raghunandan The `interface` is implemented in the `Activity`. Hmmm...am I missing something here? When the task instance is created, the `interface` variable can be initialized – codeMagic Oct 28 '13 at 18:42
  • [Similar answer](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) – codeMagic Oct 28 '13 at 18:45
  • @Raghunandan no problem, I kept getting confused also because of the order the code is posted. Logically in my mind its hard seeing the `AsyncTask` before the `Activity` class since the `Activity` calls the task. Crazy how minds work – codeMagic Oct 28 '13 at 18:49
  • the only thing that is still confusing is where is the interface. agreed your answer is right and it as good as passing context to the constructor and initializing the listener there – Raghunandan Oct 28 '13 at 18:55
  • 1
    @Raghunandan Ah, I see what you are saying. I just assumed it was declared in its own file somewhere and not posted because it really isn't needed here, IMHO, as long as it is declared somewhere. I agree, initializing it either way would be fine. Maybe less confusing passing `Context` and initializing it in the task's constructor – codeMagic Oct 28 '13 at 19:00
  • now confusion cleared i thought the interface was in asynctask and that lead to a huge confusion. Any way your answer is right. – Raghunandan Oct 28 '13 at 19:04
  • Thanks codeMagic, that works but now I get another null pointer exception for appending 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) – orangeocelot Oct 28 '13 at 19:05
  • Looks like `textView1` is not the `id` of a `TextView` in `activity_main.xml` so `tv1` is `null` when trying to call `append()` on it. – codeMagic Oct 28 '13 at 19:10