-1

I get "java.lang.NullPointerException" from : " firstplayer = player1.getText().toString();" in OnCreate method . player1 in my code is one of the EditText in previous activity .

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;


public class startwith2player extends ActionBarActivity {
private boolean playerone = true , playertwo = false ;
private Integer score1 = 0 , score2 = 0;
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;
TextView tex1 ;
TextView tex2 ;
String firstplayer = "";
String secondplayer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);
     tex1 = (TextView) findViewById(R.id.textView3);
     tex2 = (TextView) findViewById(R.id.textView4);
    EditText player1 = (EditText)findViewById(R.id.editText);
    EditText player2 = (EditText)findViewById(R.id.editText2);
    firstplayer = player1.getText().toString();
    secondplayer = player2.getText().toString();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_startwith2player, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void refresh(){
    BlueAllBoxes();
    if(playerone){
        playerone = false;
        playertwo = true;
    }
    else{
    playerone = true;
    playertwo = false;
    }
    flag4 = 0 ; flag5 = 0 ; flag6 = 0 ; flag7 = 0; flag8 = 0; flag9 = 0 ; flag10 = 0; flag11 = 0; flag12 = 0;
    counter1 = 0 ; counter2 = 0;
}

public void BlueAllBoxes(){
    Button button4 = (Button) findViewById(R.id.button4);
    button4.setBackgroundColor(0xFF4B4BFF);
    Button button5 = (Button) findViewById(R.id.button5);
    button5.setBackgroundColor(0xFF4B4BFF);
    Button button6 = (Button) findViewById(R.id.button6);
    button6.setBackgroundColor(0xFF4B4BFF);
    Button button7 = (Button) findViewById(R.id.button7);
    button7.setBackgroundColor(0xFF4B4BFF);
    Button button8 = (Button) findViewById(R.id.button8);
    button8.setBackgroundColor(0xFF4B4BFF);
    Button button9 = (Button) findViewById(R.id.button9);
    button9.setBackgroundColor(0xFF4B4BFF);
    Button button10 = (Button) findViewById(R.id.button10);
    button10.setBackgroundColor(0xFF4B4BFF);
    Button button11 = (Button) findViewById(R.id.button11);
    button11.setBackgroundColor(0xFF4B4BFF);
    Button button12 = (Button) findViewById(R.id.button12);
    button12.setBackgroundColor(0xFF4B4BFF);
}
public void button4(View view) {
    if(counter1 == 3){
        if(flag4 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
               // tex1.setText(String.valueOf(score1));
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
            refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
 else {
        if (flag4 == 0) {
            flag4 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
              BlueAllBoxes();
        }
    }
}
public void button5(View view) {
    if(counter1 == 3){
        if(flag5 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

               if(playerone) {
                    score1 = score1 + 30;
                   tex1.setText(firstplayer + " : " + score1 );
                }
               else {
                   score2 = score2 + 30;
                   tex2.setText(secondplayer + " : " + score2 );
                }
                if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        if (flag5 == 0) {
            flag5 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
                BlueAllBoxes();
        }
    }
}
public void button6(View view) {
    if(counter1 == 3){
        if(flag6 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
                if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        if (flag6 == 0) {
            flag6 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
                BlueAllBoxes();
        }
    }
}
public void button7(View view) {
    if(counter1 == 3){
        if(flag7 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1);
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag7 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button8(View view) {
    if(counter1 == 3){
        if(flag8 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag8 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button9(View view) {
    if(counter1 == 3){
        if(flag9 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag9 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button10(View view) {
    if(counter1 == 3){
        if(flag10 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag10 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button11(View view) {
    if(counter1 == 3){
        if(flag11 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2);
            }
        }
    }
    else {
        flag11 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button12(View view) {
    if(counter1 == 3){
        if(flag12 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2);
            }
        }
    }
    else {
        flag12 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
 }

i think findViewById(R.id.editText); return NULL but it should not return NULL because i have EditText with editText id in previous activity here is my XML previous activity

<TextView
    android:layout_width="150dp"
    android:layout_height="40dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="NAME PLAYER 1 :"
    android:id="@+id/textView"
    android:layout_marginBottom="70dp"
    android:layout_above="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="150dp"
    android:layout_height="40dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="NAME PLAYER 2 :"
    android:id="@+id/textView2"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text="START"
    android:id="@+id/button3"
    android:layout_marginBottom="60dp"
    android:background="@drawable/circle"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="StartGame2Player"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_below="@+id/textView"
    android:layout_toRightOf="@+id/textView2"
    android:layout_toEndOf="@+id/textView2"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_alignBottom="@+id/textView"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText" />

I want to getText from a EditText in previous activity and then use of that string in this current activity how can i do ?

  • 1
    I know nothing about native Android development, but it's clear that `findViewById(R.id.editText)` is returning `null`. I've see a bunch of questions here about Android resources, so my guess is there's some issue with the resource definition of what you're looking for. – T.J. Crowder May 01 '15 at 15:10
  • In android when it happens when you don't think it should then clean the project. How depends on what IDE, but usually in a "build" menu. – weston May 01 '15 at 15:24
  • you are getting error because you are calling an edittext that is not within the scope of your activity's xml which is R.layout.activity_startwith2player. that is not the correct approach to get the text from other activity. use intent to pass data from one activity to another. From your previous activity, pass the intent to the second activity. Intent intent = new Intent(this, startwith2player.class); intent.putExtra("firstplayer", player1.getText().toString()); intent.startActivity(); – Genevieve May 01 '15 at 15:54
  • And on your startwith2player activity, remove the edittext for player1 because that will cause error. you can get the data that was passed from previous activity by String firstplayer= intent.getStringExtra("firstplayer); – Genevieve May 01 '15 at 15:54

2 Answers2

0

player1 in my code is one of the EditText in previous activity .

That's bad design practice, and not going to work without substantial trickery.

i think findViewById(R.id.editText); return NULL but it should not return NULL because i have EditText with editText id in previous activity here is my XML previous activity

You cannot obtain references to the views of another activity that way, and even if you could you should not assume that they would still be valid once that activity is off the screen and your current one has replaced it.

Instead, have the first activity forward this information to the second as an Intent extra. If the information is fairly persistent you can also consider putting it in shared preferences. In some cases it may be appropriate to store it as a field of an Application object, and in a few special cases a global variable is not out of the question.

Always be careful to check for the possibility that the previous Activity may not have obtained this value; in fact, depending on how your manifest is set up, the previous Activity may never have run, or may not have run in the present process, if Android needed to kill and then re-create your process because something else resource hungry ran in the meantime.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
0

Answer is simple. you edittext is empty. Look at the line above it

EditText player1 = (EditText)findViewById(R.id.editText);
    EditText player2 = (EditText)findViewById(R.id.editText2);
    firstplayer = player1.getText().toString();
    secondplayer = player2.getText().toString();

You have initialized player1 and player2 which are empty and then you can using firstplayer = player1.getText().toString(); to get text from player1 which is empty.

My suggestion would be to add button and then get its value using gettext().

Sam
  • 1,237
  • 1
  • 16
  • 29