BroadCast Receiver java.lang.NullPointerException

4

I'm doing an Android application for a barcode grabber, basically what the application does is:

From a navigation menu I select a winery that takes me to a fragment and in this fragment I have a spinner and two text view, I select a spinner product, then I enter the kilos manually and with the captor I read the code of bars, at the time you read the code sends the data to the database, but at the time that change of warehouse and performed the same procedure when reading the barcode the system drops because it does not rescue any value.

I've been trying to solve this problem for about two weeks and I do not know why changing the cellar and returning to the fragment does not rescue the values

private BroadcastReceiver mSamDataReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        kilos = edt1.getText().toString().trim();
        if (intent.getAction().equals(SCN_CUST_ACTION_SCODE)) {
            codigo_barras = intent.getStringExtra(SCN_CUST_EX_SCODE);
            edt2.setText(codigo_barras);

            System.out.println(kilos+ " " + codigo_barras+" "+ nombre);

            if (edt2 !=null){

                StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Toast.makeText(getActivity(),R.string.msjeEnvio,Toast.LENGTH_LONG).show();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(getActivity(),error.toString(), Toast.LENGTH_LONG).show();
                            }
                        }){

                    @Override
                    protected Map<String,String> getParams(){
                        Map<String,String> params = new HashMap<String, String>();
                        params.put(CATEGORIA,nombre);
                        params.put(CODIGO,codigo_barras);
                        params.put(KILOS,kilos);
                        return params;
                    }

                    };
Req.getInstance(getActivity()).addToRequestQueue(stringRequest);

Message displayed in LogCat:

10-26 01:52:48.961 5742-5742/cl.parmex.lfigueroa.inventario E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          java.lang.NullPointerException
                                                                              at android.widget.Toast.<init>(Toast.java:92)
                                                                              at android.widget.Toast.makeText(Toast.java:238)
                                                                              at cl.parmex.lfigueroa.inventario.SpinFragment$2$2.onErrorResponse(SpinFragment.java:244)
                                                                              at com.android.volley.Request.deliverError(Request.java:598)
                                                                              at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
                                                                              at android.os.Handler.handleCallback(Handler.java:730)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                              at android.os.Looper.loop(Looper.java:137)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5136)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:525)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                              at dalvik.system.NativeStart.main(Native Method)
    
asked by Leslie Karen Figueroa Meneses 25.10.2016 в 17:06
source

1 answer

2

The error according to your LogCat is generated in a Toast, and it is here:

Toast.makeText(getActivity(),error.toString(), Toast.LENGTH_LONG).show();

You must use the method getMessage () to get the error message displayed by volley, you can also validate if error has a null value:

if(error != null){
 Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG).show();
}else{
  Toast.makeText(getActivity(), "Respuesta Volley incorrecta", Toast.LENGTH_LONG).show(); 
}
    
answered by 25.10.2016 в 21:22