Null pointer Exception in Custom Adapter while set the value for list item

368 views Asked by At

I want to display some conditional data in List view when user request or click on button. Each list row has two value one value is name and 2nd its description. When User press button it fire command and fetch information from peripheral and match all value with different condition one by one, as a Conditions are match with value it put one list row value in adapter after all conditions check it will assign adapter to list view, then display all adapter data will be display in list view.

This is my Code

public class DTCode extends Activity { private final static String TAG = DTCode.class.getSimpleName();

final static String EXTRA_DEVICE_ADDRESS = "DEVICE_ADDRESS";

public  String mDeviceAddress;
public BluetoothLeService mBluetoothLeService;
private boolean mConnected = false;

Button btnread, btnclear;
String receiveData = "";
ListView myList;

ErrorsAdapter adapter;

ArrayList<Error> arrayofErrors;

Error error;

ListView listError;
//Code for get Service;

private  final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if (!mBluetoothLeService.initialize()){
            Log.e(TAG, "Bluetooth is not initialize");
            finish();
        }
        Log.e(TAG,"Bluetooth Service is oKay");
        mBluetoothLeService.connect(mDeviceAddress);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mBluetoothLeService = null;
    }
};

private final BroadcastReceiver mGattUpdateReceiver= new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final  String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)){
            Log.e(TAG, "Only Gatt, Just Wait");
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)){
            mConnected = false;
            invalidateOptionsMenu();
            btnclear.setEnabled(false);
            btnread.setEnabled(false);
        }else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)){
            mConnected =true;
            btnread.setEnabled(true);
            Log.e(TAG,"In what we need");
        }else  if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)){
            Log.e(TAG,"Receive Data");

            String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);

            if (data != null){
                btnclear.setEnabled(true);
                receiveData = receiveData.concat(data);

                char character = receiveData.charAt(receiveData.length() - 1 );
                System.out.println(receiveData);

                if (character == '\n'){

                    String[] err = receiveData.split("!");

                    for (int i = 0 ; i < err.length ; i++) {

                        String[] errCode = err[i].split("_");

                        for (int j = 0; j < errCode.length; j++) {

                            System.out.println(errCode[j]);

                            if (errCode[j].startsWith("108E07")){
                                error = new Error("DIM-B108E07", "Display failure Information");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("115C71")){
                                error = new Error("DIM-B115C71", "Transfer Fuel Pump. Actuator Stuck");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1A0107")){
                                error = new Error("DIM-B1A0107","Speaker #1 Failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1B6916")){
                                error = new Error("DIM-B1B6916","Circuit Voltage Below Threshold.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1B6917")){
                                error = new Error("DIM-B1B6917","Circuit Voltage above Threshold");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1A4815")){
                                error = new Error("DIM-C1A4815", "Short Circuit to Battery or Open.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("600262")){
                                error = new Error("DIM-C200262", "Odometer Tempered");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("046071")){
                                error = new Error("DIM-P0460171","Fuel Level A Circuit, Actuator Stuck.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("134671")){
                                error = new Error("DIM-P134671","Fuel Level B Circuit, Actuator Stuck.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C00155")){
                                error = new Error("DIM-U000155","High Speed can not Configured.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C00188")){
                                error = new Error("DIM-U000188","High Speed Can Bus Off.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C01055")){
                                error = new Error("DIM-U001055","Medium Speed Can Not Configured.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C01088")){
                                error = new Error("DIM-U001088","Medium Speed Can Bus Off.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C10000")){
                                error = new Error("DIM-U010000","Lost Communication with ECM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C10100")){
                                error = new Error("DIM-U010100","Lost Communication with TCM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C10400")){
                                error = new Error("DIM-U010400","Lost Communication with FSM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C12100")){
                                error = new Error("DIM-U012100","Lost Communication with ABS");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C14000")){
                                error = new Error("DIM-U014000","Lost Communication with CEM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C15100")){
                                error = new Error("DIM-U015100","Lost Communication with SRSM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C15600")){
                                error = new Error("DIM-U015600","Lost Communication with ICM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C15900")){
                                error = new Error("DIM-U015900","Lost Communication with PAM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C25000")){
                                error = new Error("DIM-U025000","Lost Communication with PPM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C25200")){
                                error = new Error("DIM-U025200","Lost Communication with TRM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C40168")){
                                error = new Error("DIM-U040168","Invalid Data Received from ECM/PCM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C40268")){
                                error = new Error("DIM-U040268","Invalid Data Received from TCM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C41568")){
                                error = new Error("DIM-U041568","Invalid Data Received from ABS.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C42268")){
                                error = new Error("DIM-U042268","Invalid Data Received from CEM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C45292")){
                                error = new Error("DIM-U045292","Invalid Data Received from SRS.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C55168")){
                                error = new Error("DIM-U055168","Invalid Data Received from PPM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("DA4B49")){
                                error = new Error("DIM-U1A4B49","Internal Electronic Failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("E10000")){
                                error = new Error("DIM-U210000","Control Module not Configured.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("E10100")){
                                error = new Error("DIM-U210100","Control Module Configuration Incompatible.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("F00044")){
                                error = new Error("DIM-U300044","Data Memory failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("F00045")){
                                error = new Error("DIM-U300045","Program Memory Failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("F00362")){
                                error = new Error("DIM-U300362","Signal Compare Failure.");
                                adapter.add(error);
                            }
                        }
                    }
                  listError.setAdapter(adapter);
                }
            }
        }

    }
};

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

    final Intent intent = getIntent();
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    btnread = (Button) findViewById(R.id.read);
    btnread.setEnabled(false);

    btnclear= (Button) findViewById(R.id.clr);
    btnclear.setEnabled(false);

    arrayofErrors = new ArrayList<Error>();
    adapter = new ErrorsAdapter(this, arrayofErrors);

    listError = (ListView) findViewById(R.id.list);

    getActionBar().setTitle("TFT Trouble Code");
    getActionBar().setDisplayHomeAsUpEnabled(true);
    Log.d(TAG, "Try to bindService=" + bindService(new Intent(this, BluetoothLeService.class), mServiceConnection, BIND_AUTO_CREATE));

    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

}

@Override
protected void onResume(){
    super.onResume();
    registerReceiver(mGattUpdateReceiver,makeGattUpdateIntentFilter());
    if (mBluetoothLeService != null){
        final boolean result = mBluetoothLeService.connect(mDeviceAddress);
        Log.d(TAG,"Connect Result="+ result);
    }
}

@Override
protected void onPause(){
    super.onPause();
    unregisterReceiver(mGattUpdateReceiver);
    unbindService(mServiceConnection);
}

@Override
protected  void  onDestroy(){

    if (mBluetoothLeService != null){
        mBluetoothLeService.close();
        mBluetoothLeService = null;
    }
    Log.d(TAG, "We are in Destroy");
}

public void onClick (View v){

    if (v == btnread){
        if(!mConnected) return;
        mBluetoothLeService.WriteValue("$DIM_RE\n");
    }
    if (v == btnclear){
        if(!mConnected) return;
        mBluetoothLeService.WriteValue("$DIM_CLR\n");
        myList.setAdapter(null);
    }
}


public  class  ErrorsAdapter extends  ArrayAdapter<Error>{
    public ErrorsAdapter(Context context, ArrayList<Error> errors){
        super(context, 0, errors);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        Error error = getItem(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_error, parent, false);
        }

        TextView ecTxt = (TextView) findViewById(R.id.txt_error_code);
        TextView edTxt = (TextView) findViewById(R.id.txt_error_description);

        ecTxt.setText(error.errorCode);
        edTxt.setText(error.errorDescription);

        return convertView;
    }
}
1

There are 1 answers

1
Mohammad Ersan On BEST ANSWER

in getView in your adapter, replace

TextView ecTxt = (TextView) findViewById(R.id.txt_error_code);
TextView edTxt = (TextView) findViewById(R.id.txt_error_description);

with

TextView ecTxt = (TextView) convertView.findViewById(R.id.txt_error_code);
TextView edTxt = (TextView) convertView.findViewById(R.id.txt_error_description);