I have read questions related to this question and I have come to the conclusion that my variable which is of reference type has not been initialized or its object has not been created to be used for dereferencing. However I am not sure of how to initialize my variable of type String
with the way it's being used. Here is part of the codes:
public class ManualControlsFragment extends Fragment {
private MqttAndroidClient client;
String payload1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_manual_controls, container, false);
switchLed = (Switch) view.findViewById(R.id.LEDManControl);
switchLed.setChecked(true);
switchLed.setTextOn("On");
switchLed.setTextOff("Off");
String clientId = MqttClient.generateClientId();
client =
new MqttAndroidClient(this.getActivity(), "tcp://192.168.100.6:1883",
clientId);
//Start of Manual Automation
try {
IMqttToken token = client.connect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
String topic = "rpi/gpio";
switchLed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// If ischecked means if it is ON the it will show true else
// show false
if (isChecked) {
/*Toast.makeText(getActivity().getApplicationContext(),
"Switch is : " + isChecked, Toast.LENGTH_SHORT)
.show();*/
payload1 ="ledOn";
} else {
/*Toast.makeText(getActivity().getApplicationContext(),
"Switch is : " + isChecked, Toast.LENGTH_SHORT)
.show();*/
payload1 ="ledOff";
}
}
});
try {
/*if(payload1!=null){
MqttMessage message1 = new MqttMessage(payload1.getBytes());
client.publish(topic, message1);
}*/
MqttMessage message1 = new MqttMessage(payload1.getBytes());
client.publish(topic, message1);
This is what appears on the logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference
at com.example.teerna.smartagriculturev5.ManualControlsFragment$1.onSuccess(ManualControlsFragment.java:294)
at org.eclipse.paho.android.service.MqttTokenAndroid.notifyComplete(MqttTokenAndroid.java:124)
at org.eclipse.paho.android.service.MqttAndroidClient.simpleAction(MqttAndroidClient.java:1497)
at org.eclipse.paho.android.service.MqttAndroidClient.connectAction(MqttAndroidClient.java:1439)
at org.eclipse.paho.android.service.MqttAndroidClient.onReceive(MqttAndroidClient.java:1368)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:308)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:118)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
The error appears at this line:
MqttMessage message1 = new MqttMessage(payload1.getBytes());
I tried the following but they didn't work:
if (isChecked) {
payload1 = new String("ledOn");
} else {
payload1 = new String("ledOff");
}
And I tried to see if the payload is null. That stopped the app from crashing (and displaying the NullPointException) and it didn't send the payload:
try {
if(payload1!=null){
MqttMessage message1 = new MqttMessage(payload1.getBytes());
client.publish(topic, message1);
}
I suppose the cause of the error lies in the payload
not being initialized which makes it null. I would like to know where I should initialize it or how I should use it because putting the variable inside the try
block gives me errors such it should be declared final and when I declare it as final
I am not able to assign a value to it as it cannot be changed after I make it final
.
Would greatly appreciate any help.
Thanks for all the answers.
I actually reconsidered the approach I used to send the payload and I realized that the reason why the server was not getting the payload was because a null payload was being sent and it was not sending the payload based on the changes in the switch. The aim of the switch was to send a payload to a server to tell my microcontroller to turn a LED on or off based on the status of the switch. I rewrote it as I have posted below and it worked perfectly fine:
switchLed.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// If ischecked means if it is ON the it will show true else
// show false
if (isChecked) {
String payload1="ledOn";
MqttMessage message1 = new MqttMessage(payload1.getBytes());
try {
client.publish(topic, message1);
} catch (MqttException e) {
e.printStackTrace();
}
} else {
String payload2 = "ledOff";
MqttMessage message2 = new MqttMessage(payload2.getBytes());
try {
client.publish(topic, message2);
} catch (MqttException e) {
e.printStackTrace();
}
}
}
});