E / AndroidRuntime: FATAL EXCEPTION: main, java.lang.NullPointerException: println needs a message

3

I have the following code:

package org.jcloarca.laboratorio2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.jcloarca.laboratorio2.volley.WebService;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {

private Button btnLogin;
private EditText txtEmail, txtPassword;

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

    btnLogin = (Button)findViewById(R.id.btnLogin);
    txtEmail = (EditText)findViewById(R.id.txtEmail);
    txtPassword = (EditText)findViewById(R.id.txtPassword);

    btnLogin.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("correo", txtEmail.getText().toString());
            params.put("password", txtPassword.getText().toString());
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, WebService.autenticar, new JSONObject(params), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try{
                        JSONArray listaUsuarios = response.getJSONArray("user");
                        if(listaUsuarios.length()>0){
                            JSONObject user = listaUsuarios.getJSONObject(0);

                            Toast.makeText(getApplicationContext(), "Bienvenido "+user.getString("nombreCompleto"), Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(getApplicationContext(), "Verifique sus credenciales.", Toast.LENGTH_LONG).show();
                        }

                    }catch(Exception ex){
                        Log.e("Error: ",ex.getMessage());
                    }
                }
            }, new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.e("Error: Response",error.getMessage());
                }
            });
            WebService.getInstance(v.getContext()).addToRequestQueue(request);
        }
    });

}
}

Which is to make a request to a Web Service, but at the time of calling the web service, it throws me the following error:

05-21 16:16:59.990 16961-16961/org.jcloarca.laboratorio2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: org.jcloarca.laboratorio2, PID: 16961
                                                                       java.lang.NullPointerException: println needs a message
                                                                           at android.util.Log.println_native_inner(Native Method)
                                                                           at android.util.Log.println_native(Log.java:290)
                                                                           at android.util.Log.e(Log.java:416)
                                                                           at org.jcloarca.laboratorio2.LoginActivity$1$2.onErrorResponse(LoginActivity.java:64)
                                                                           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:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                           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:905)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                           at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)

Which tells me that the error is on line 64, where I have a Log.e , but I do not know how to fix it since I have reviewed and compared the code but I can not find an error.

    
asked by José Carlos Loarca Hass 22.05.2016 в 00:34
source

1 answer

2

You can try the following:

Log.e("Error: Response", "" + error.getMessage());

If the previous one were the log of the error, if it were another use something similar to the previous one for another log using ""+ .

You can also check to see if it is null, by using the operador ternario .

String msg = (ex.getMessage() == null) ? "Su texto" : ex.getMessage();
    
answered by 22.05.2016 / 00:54
source