0

I've been working on a single activity application that receives UDP packets, parses their message, then changes an ImageView, a TextView, and plays an alert song depending on what the message contains. I have three classes: MainActivity, ListeningService, and AndroidUDP. MainActivity contains all of the GUI implentation, AndroidUDP contains the enumeration that contains the 20 possible alert types dependent on what the UDP message contains as well as sets the TextView and ImageViews, and ListeningService contains the AsyncTask private class.

I didn't write this, an electrical engineer did and I was tasked with making it actually work. When I attempt to call the ListeningService via the AndroidUDP.startIt() method I get the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.p3_group.v2vapp/com.p3_group.v2vapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.p3_group.v2vapp.ListeningService.continueListening()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.p3_group.v2vapp.ListeningService.continueListening()' on a null object reference at com.p3_group.v2vapp.MainActivity.onCreate(MainActivity.java:61)

I'm sure that I'm doing something dumb like trying to call the background update from the wrong thread. I've never done this kind of thing before so any pointers would be great!

and yes, i know this code is pretty bad. I only need it functional for what we're doing, not perfect

Tadhg
  • 474
  • 7
  • 19

1 Answers1

1

Hi you didn't start the service at all and you are trying to bind the service in "public void doStuff(MainActivity r, IntentFilter filter)" but it will not bind, so ListeningService listeningService is null in AndroidUDP, but you are trying to call below method, but listeningService is null so it's giving null pointer exception.

public void startIt()
{

    listeningService.continueListening();

}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • this makes sense. How would I start the `ListeningService` in `AndroidUDP`? I see that the `AndroidUDP` service is started in `MainActivity` (at least, it contains `startService(AndroidUDP.intent1)`, which appears to start the `ListeningService` – Tadhg Jul 13 '17 at 22:21
  • In fact, for something as simple as this, do I really even need to run the listener as a service? Would I be able to just create the AsyncTask in the MainActivity and have it go like that? – Tadhg Jul 13 '17 at 23:19
  • 1
    Yes for simple Network call we should not use service. you can use AsyncTask. – Muthukrishnan Rajendran Jul 14 '17 at 04:09