2016-12-27 7 views
0

Я пытаюсь подключить свое приложение Android к моему Arduino Uno по Bluetooth. Чтобы сделать соединение общим для всего приложения, я создал службу, в которой я реализовал свои функции Bluetooth. Проблема в том, что моя функция sendData вызывает ошибку и не имеет представления о том, как ее разрешить.Попытка вызвать виртуальный метод 'void java.io.OutputStream.write (byte [])' на ссылке нулевого объекта

Пожалуйста, помогите мне. Спасибо заранее :)

Вот мой MainActivity.java:

package llg.trashbin; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.UUID; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

import android.view.View; 
import android.widget.Button; 
import android.view.View.OnClickListener; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.widget.Toast; 

import android.content.Intent; 


public class MainActivity extends AppCompatActivity { 

public int objectOut = 0; 
communicationEnabled mCommunicate = new communicationEnabled(); 

Button bottle, emballages, papier; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent mBluetooth = new Intent(MainActivity.this, BluetoothConnectService.class); 
    startService(mBluetooth); 

    bottle = (Button) findViewById(R.id.bottle); 
    emballages = (Button) findViewById(R.id.emballages); 
    papier = (Button) findViewById(R.id.papier); 

    bottle.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      objectOut = 2; 
      mCommunicate.MessageSent(); 
     } 
    }); 
    emballages.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      objectOut = 3; 
      mCommunicate.MessageSent(); 
     } 
    }); 
    papier.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      objectOut = 5; 
      mCommunicate.MessageSent(); 
     } 
    }); 
} 

public class communicationEnabled extends BluetoothConnectService { 
    public String hello = "1"; 

    public void MessageSent() { 
     sendData(hello); 
    } 
} 

}

BluetoothConnectService.java:

package llg.trashbin; 

import android.app.Service; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 

import android.os.IBinder; 
import android.util.Log; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.UUID; 


public class BluetoothConnectService extends Service { 

private static final String TAG = "Tripoubelle"; 

public static final int REQUEST_ENABLE_BT = 1; 
public BluetoothAdapter btAdapter = null; 
public BluetoothSocket btSocket = null; 
public OutputStream outStream = null; 

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private static String address = "00:13:EF:00:00:4C"; 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    Log.d(TAG, "In onCreate()"); 
    super.onCreate(); 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    checkBTState(); 

    Log.d(TAG, "...In onResume - Attempting client connect..."); 

    BluetoothDevice device = btAdapter.getRemoteDevice(address); 

    try { 
     btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + "."); 
    } 

    btAdapter.cancelDiscovery(); 
    Log.d(TAG, "...Connecting to Remote..."); 
    try { 
     btSocket.connect(); 
     Log.d(TAG, "...Connection established and data link opened..."); 
    } catch (IOException e) { 
     try { 
      btSocket.close(); 
     } catch (IOException e2) { 
      errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); 
     } 
    } 

    Log.d(TAG, "...Creating Socket..."); 

    try { 
     outStream = btSocket.getOutputStream(); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume(), input and output stream creation failed:" + e.getMessage() + "."); 
    } 
} 


public void onPause() { 

    Log.d(TAG, "...In onPause()..."); 

    if (outStream != null) { 
     try { 
      outStream.flush(); 
     } catch (IOException e) { 
      errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + "."); 
     } 
    } 

    try  { 
     btSocket.close(); 
    } catch (IOException e2) { 
     errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); 
    } 
} 

public void checkBTState() { 
    // Check for Bluetooth support and then check to make sure it is turned on 

    // Emulator doesn't support Bluetooth and will return null 
    if(btAdapter==null) { 
     errorExit("Fatal Error", "Bluetooth Not supported. Aborting."); 
    } else { 
     if (btAdapter.isEnabled()) { 
      Log.d(TAG, "...Bluetooth is enabled..."); 
     } else { 
      //Prompt user to turn on Bluetooth 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivity(enableBtIntent); 
     } 
    } 
} 

private void errorExit(String title, String message){ 
    Toast msg = Toast.makeText(getBaseContext(), 
      title + " - " + message, Toast.LENGTH_SHORT); 
    msg.show(); 
    finish(); 
} 

public void sendData(String message) { 
    byte[] msgBuffer = message.getBytes(); 

    Log.d(TAG, "...Sending data: " + message + "..."); 
    try { 
     outStream.write(msgBuffer); 
    } catch (IOException e) { 
     String msg = "In onResume() and an exception occurred during write: " + e.getMessage(); 
     if (address.equals("00:13:EF:00:00:4C")) 
      msg = msg + ".\n\nUpdate your server address from 00:13:EF:00:00:4C to the correct address on line 37 in the java code"; 
     msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n"; 

     errorExit("Fatal Error", msg); 
    } 
} 

}

Здесь ошибка:

12-27 10:21:52.511 1178-1178/llg.trashbin E/AndroidRuntime: FATAL EXCEPTION: main 
Process: llg.trashbin, PID: 117 
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference 
at llg.trashbin.BluetoothConnectService.sendData(BluetoothConnectService.java:125) 
at llg.trashbin.MainActivity$communicationEnabled.MessageSent(MainActivity.java:103) 
at llg.trashbin.MainActivity$2.onClick(MainActivity.java:71) 
at android.view.View.performClick(View.java:5702) 
at android.widget.TextView.performClick(TextView.java:10888) 
at android.view.View$PerformClick.run(View.java:22541) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Edit:

Я поставил код функции sentData в если/другое заявление для того, чтобы проверить, если назначенная строка была пустой:

public void sendData(String message) { 
    if (message != null) { 
     Log.d(TAG, "sent data isn't null !"); 
     byte[] msgBuffer = message.getBytes(); 
     Log.d(TAG, "...Sending data: " + message + "..."); 
     try { 
      outStream.write(msgBuffer); 
     } catch (IOException e) { 
      String msg = "In onResume() and an exception occurred during write: " + e.getMessage(); 
      if (address.equals("00:13:EF:00:00:4C")) 
       msg = msg + ".\n\nUpdate your server address from 00:13:EF:00:00:4C to the correct address on line 37 in the java code"; 
      msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n"; 

      //errorExit("Fatal Error", msg); 
     } 
    } 
    else { 
     Log.d(TAG, "sent data is null !"); 
    } 
} 

И у меня был результат POSITIF: назначенная строка не была нулевой!

Вот окно отладки:

12-27 12:53:24.661 8183-8183/llg.trashbin D/Tripoubelle: sent data isn't null ! 
+0

код, который зависит от успех предыдущего кода в блоке 'try' должен находиться внутри блока' try'. Часто это означает, что весь метод должен генерировать исключение, а не ловить его. Не пишите такой код. – EJP

ответ

0

Если его не переменная message, то она должна быть OutputStream. Попробуйте сделать это в методе sendData()

if (message!=null){ 
byte[] msgBuffer = message.getBytes();  
try{ 
outStream = btSocket.getOutputStream(); 
} 
catch (IOException e) { 
      errorExit("Fatal Error", "in sendData() input and output stream creation failed:" + e.getMessage() + "."); 

    } 
try { 
      outStream.write(message); 
     } catch (IOException e) { 
} 
0

Прежде всего, stacktrace может много говорить о фактическом исключении. Чтобы понять это, вы, как правило, хотите начать снизу (что вызвало ошибку) вверх (какая была фактическая ошибка).

В вас случае линия

java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference 

означает, что метод write() пытается назвать на объект, который является нулевым (OutputStream).

Я хотел бы предложить вам проверить объект выходного потока для нулевой ссылки первый:

if(outStream==null) 

тогда, если она у вас есть, чтобы инициализировать его снова

try { outStream = btSocket.getOutputStream(); } catch (IOException e) { errorExit("Fatal Error", "In onResume(), input and output stream creation failed:" + e.getMessage() + "."); }

 Смежные вопросы

  • Нет связанных вопросов^_^