Forums Register Login
android output stream write method producing NullPointerException
I am attempting to pass messages from an Android(client) device to my PC(server) however, when the output stream write(bytes) method is called I get a NullPointerException.

The ConnectThread is a inner class, it passes a message to the Handler which then carries out the correct action. A new Intent is created and the new activity begins.

Here is the snippet of code:

Here is the code within the Handler:

The connected() method calls the ConnectedThread passing the socket:

This is the MediaPlayer class. When a button is clicked I want a String or character to be passed to the PC:

This is the ConnectedThread class which I got from the Android developer website. The ConnectedThread is in its own individual class.

The Android and PC establish a connection however, I get this error when I select the 'wmp' button.

01-16 16:46:44.787: E/AndroidRuntime(32682): FATAL EXCEPTION: main
01-16 16:46:44.787: E/AndroidRuntime(32682): java.lang.NullPointerException
01-16 16:46:44.787: E/AndroidRuntime(32682): at com.example.btclient.MediaPlayer$1.onClick(MediaPlayer.java:25)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.view.View.performClick(View.java:2485)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.view.View$PerformClick.run(View.java:9089)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.os.Handler.handleCallback(Handler.java:587)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.os.Looper.loop(Looper.java:130)
01-16 16:46:44.787: E/AndroidRuntime(32682): at android.app.ActivityThread.main(ActivityThread.java:3806)
01-16 16:46:44.787: E/AndroidRuntime(32682): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:46:44.787: E/AndroidRuntime(32682): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 16:46:44.787: E/AndroidRuntime(32682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-16 16:46:44.787: E/AndroidRuntime(32682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-16 16:46:44.787: E/AndroidRuntime(32682): at dalvik.system.NativeStart.main(Native Method)
01-16 16:46:44.797: W/ActivityManager(1654): Force finishing activity com.example.btclient/.MediaPlayer

However, the data is sent to the PC if I put the Button onClickListener within the Handler (which is in the same class as the ConnectThread. The code in the Handler looks like this:

But this is not ideal as I would prefer a new activity to begin to handle the Button onClickListeners and pass specific messages to the PC.

The NullPointerException is on this line:

It is not clear from what you posted what line of code is the one that has a null. What is DeviceListActivity.java line #144?
I am also having a hard time figuring out how the Handler compiles. There seems to be some excess brackets happening someplace. When I try to align open and close braces/parenthesis I get this:
Sorry, I didn't post the correct error message.
I have now edited the error message section.

The handler is within the DeviceListActivity class therefore, it won't compile without the rest of the code in this class. I haven't provided the full code of this class since I don't think it is relevant to my problem.
The NullPointerException is on this line:

It is not clear from what you posted what line of code is the one that has a null. What is MediaPlayer.java line #25?
Apologies for that.
Line 25 refers to this bit of code:

Jay Pat wrote:Apologies for that.
Line 25 refers to this bit of code:

The media player class never instantiates the con reference.
Yes, this may be the solution.
Do you know how I might instantiate the ConnectedThread con since it takes a BluetoothSocket as the parameter?
Documentation on the BluetoothSocket class should help. You can instantiate that and pass it to the class. Start your thread once you have a reference to the instantiated class.
I have now altered the MediaPlayer class and placed the connected() method in this class so that the BluetoothSocket object can be passed to this class:

However, I now get this error:

01-17 10:25:05.498: E/AndroidRuntime(6683): FATAL EXCEPTION: Thread-12
01-17 10:25:05.498: E/AndroidRuntime(6683): java.lang.NullPointerException
01-17 10:25:05.498: E/AndroidRuntime(6683): at com.example.btclient.DeviceListActivity$ConnectThread.run(DeviceListActivity.java:262)

where DeviceListActivity.java:262 refers to this line which is in the ConnectThread inner class:

med is an instance of the MediaPlayer class.
You have to provide enough context to answer your question. No code you posted shows med.connected(mmSocket, mmDevice); No code show you declare or assign to med. We can't answer these questions without context. Here is what we do know:
med.connected(mmSocket, mmDevice);
Throws a NullPointerException. Therefore:

med is an instance of the MediaPlayer class.

This statement is probably false. What we can guess is that med is a variable of type MediaPlayer which is null. You need to either instantiate it or pass a value to an actual instance to it.
I have instantiated the MediaPlayer class in the DeviceListActivity onCreate() method:

I still get a NullPointerException in the MediaPlayer class referring to this line:

The full class is shown in my previous post.
I think you are running in circles, sit back and think about your design and the dependencies.

In your first post you are starting the MediaPlayer using an Intent. Somewhere (not shown) you are calling med = new MediaPlayer(). On one of those objects you are calling connected(...). And one of those two instances of MediaPlayer is becoming the OnClickListener - probably the one that gets created from the Intent because it would go through the Activity lifecycle and the onCreate() would be called.

You need to simplify things. Figure out what classes you need, when you need them, and how you pass things from one to the other. What you are doing now is not working. The attempts to fix it are shots in the dark. You need to stop coding and start planning the data flow.
In the ConnectThread inner class the connected() method is called. The connected() method starts the ConnectedThread.
In the Handler and new Intent is created to start the MediaPlayer class.
I am new to Android programming therefore, I'm coming across these difficulties. My aim is to be able to write characters to the OutputStream from a number of classes when buttons are clicked on the Android app.
Do you know of any other methods in which I can achieve this?
I don't know, nor expect you to post, the full specification so it would be hard come up with something that will work in your specific case. I suggest you take a pencil and paper and draw up your application's work flow. Then, for each step determine what data is required for that step to work. Then determine where that data comes from. Then determine how the data can get from the source to where it is used.

The normal way to pass data from one Activity to the next is to use the overloaded Intent#setExtra(String name, ...) methods. If you can pass around basic data types or Parcelable objects, this is the route to go. But you should be careful about the data types you are passing around, since you are limited to the types of data (and for normal good encapsulation). For example do you have to pass around the Bluetooth sockets? Or just the name/id of the device to connect to? Do you really have to pass around the 'ConnectionThread'? What is it's purpose? Is it a Thread or a data holder? It should not be both (it should never be a Thread at all in my opinion, but that is a different discussion). If you can separate the data from the task then you could much easier pass the data around.
Ok, thanks for your help
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

This thread has been viewed 5247 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 19, 2019 05:29:15.