Mohammad Hosseinzadeh
Mohammad Hosseinzadeh

Reputation: 37

why i get NullPointerException from EditText android?

private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
EditText tex = (EditText) findViewById(R.id.editText3);
@Override
protected void onCreate(Bundle savedInstanceState) {
   // setContentView(R.layout.activity_startwith2player);

    System.out.println("yes yes");
   // StartByPlayerOne();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);
   // StartByPlayerOne();
   // prrrint();
  //  Bundle extras = getIntent().getExtras();
//    Integer latLong = extras.getInt("NameOfPlayer1");
  //  TextView player = (TextView)findViewById(R.id.textView3);
 //   player.setText("" + latLong);
 //   System.out.println(latLong);
}

I want to use tex.SetText(String) but before that i got NullPointerException from third line :( Any idea ?!?! android:onClick="button12"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="score1"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText3"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button4"
    android:layout_toEndOf="@+id/button4"
    android:text="AA" />

Thats my XML code

Upvotes: 0

Views: 53

Answers (3)

Swapna Singh
Swapna Singh

Reputation: 1

Use this code

private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
EditText tex;
@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("yes yes");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);

    tex = (EditText) findViewById(R.id.editText3);

}

Upvotes: 0

Ludger
Ludger

Reputation: 358

Try this:

private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
private EditText tex ;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);
    tex = (EditText) findViewById(R.id.editText3);
}

Upvotes: 1

JafarKhQ
JafarKhQ

Reputation: 8734

The tex = (EditText) findViewById(R.id.editText3); must be after setContentView(R.layout.activity_startwith2player);

Upvotes: 1

Related Questions