0

I have an error: java.lang.NullPointerException in my EditText when i try to setText string. I want to extract text from EditText to a Textview to insert into a database. I had a lot more code, but it was cutting up to find what the problem was. Here is the codes:

package ccv.checkhellsing.guiapaa;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class CrearUsuario extends Activity implements View.OnClickListener {

EditText nombreUsuario;
String user;
Button flecha;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.crearusuario_layout);

    EditText nombreUsuario = (EditText)findViewById(R.id.etNombreUsuario);
    Button flecha = (Button) findViewById(R.id.btFlechaDerecha);

    flecha.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    user = nombreUsuario.getText().toString().trim();
}
}

And the Log:

06-23 17:20:59.605    2483-2483/ccv.checkhellsing.guiapaa      E/AndroidRuntime: FATAL          EXCEPTION: main
    java.lang.NullPointerException
    at ccv.checkhellsing.guiapaa.CrearUsuario.onClick(CrearUsuario.java:42)
    at android.view.View.performClick(View.java:2485)
    at android.view.View$PerformClick.run(View.java:9081)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

I check my xml file and i sure that is not the problem.

1
  • Where is line 42 of CrearUsuario.java? Commented Jun 23, 2013 at 22:59

2 Answers 2

3

In your onCreate you do

EditText nombreUsuario = (EditText)findViewById(R.id.etNombreUsuario);

This creates a new local variable named nombreUsuario, rather than assigning the value to your field nombreUsuario (which is what onClick references). Remove the EditText in front of it so that your onCreate sets the field's value.

(Similarly for your Button flecha)

Sign up to request clarification or add additional context in comments.

1 Comment

Then this? nombreUsuario = (EditarTexto) findViewById (R.id.etNombreUsuario) flecha = (Button) findViewById (R.id.btFlechaDerecha);
0

You shouldn't define Edittext and Button two times. Only global is enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.