NullPointerException on login screen

1

I'm having this error NullPointerException and I do not know why. I have only a login screen, a dao that takes two parameters, email and password:

java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3662)
at br.com.caixadesujestoes.dao.Dao.Logar(Dao.java:47)
at br.com.caixadesujestoes.visao.Activity_Login$1.onClick(Activity_Login.java:94)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
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:5103)
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)
eglSurfaceAttrib not implemented

Activity:

public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setContentView(R.layout.layout_login);

    final Dao dao = new Dao(this);

    // Resolvendo o erro NetworkOnMainThreadException utilizando ActionBar na versao min do SDK 11
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    ActionBar ab = getActionBar();
    ab.setTitle("Login");

    email       = (EditText)findViewById(R.id.editEmail);
    senha       = (EditText)findViewById(R.id.editSenhaUsuario);

    entrar = (Button)   findViewById(R.id.btnEntrar);
    entrar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(email.getText().toString().length() <= 0){
                email.setError("Campo Email não pode ser vazio");
                email.requestFocus();
            }else if (senha.getText().toString().length() <= 0){
                    senha.setError("Campo Senha não pode ser vazio");
                    senha.requestFocus();
                }else{

                    Email = email.getText().toString();
                    Senha = senha.getText().toString();
                    dao.Logar(Email, Senha);

                }
        }
    });

Dao:

public class Dao extends Activity {

Context context;
Funcoes funcoes = new Funcoes();

public Dao(Context context){
    this.context = context;
}

public void Logar(String email, String senha){
    String urlPost = "http://192.168.1.105/tccbimbati/android/login.php";
    ArrayList<NameValuePair> pPost = new ArrayList<NameValuePair>();
    pPost.add(new BasicNameValuePair("email", email));
    pPost.add(new BasicNameValuePair("senha", senha));

    String respostaRetornada = null;
    Log.i("Entrou", "Vai entrar no try");

    try{
        respostaRetornada = ConexaoHttpClient.executaHttpPost(urlPost, pPost);
        Log.i("Entrou", "Entrou");
        String resposta = respostaRetornada.toString();
        resposta = resposta.replaceAll("\s+", "");

        Log.i("Logar", "Resposta: "+resposta);

        if (resposta.equals("1")){
            Log.i("Email", "Email: "+email);
            Log.i("Senha", "Senha: "+senha);

            Intent intent = new Intent(this, PrincipalActivity.class);
            intent.putExtra("email", email);
            intent.putExtra("senha", senha);
            startActivity(intent);
        }else{
            Toast.makeText(context, "Usuário e ou senha inválida", Toast.LENGTH_LONG).show();
        }

    }catch(Exception erro){
        erro.printStackTrace();
        funcoes.mensagemSimples(context, "Erro", "Erro: "+erro);
    }
}

}
    
asked by anonymous 27.11.2014 / 15:07

1 answer

3

The problem lies in this line:

Intent intent = new Intent(this, PrincipalActivity.class)

As you are not in an Activity , then this turns out to be your Dao object and not the Activity context passed in the constructor.

To work you have to use the context:

Intent intent = new Intent(this.context, PrincipalActivity.class)
    
27.11.2014 / 17:19