Search code examples
javaandroidandroid-widgetclass-variables

Android/Java: Using EditText/getText to change public class string variable


First off I'd like to say this is the only java experience I've had. I've messed around with c++ a good bit but its been awhile, so this may just be a stupid lack of understanding java classes.

Alright so I have a simple class file called Player:

package com.iRprojects.HelloAgain;


public class Player{
    public int Health;
    public int Strength;
    public String Name;

}

Then I have another class called Options (It was a previous activity that I decided to use for this. Also this is opened from another activity called main menu.)

package com.iRprojects.HelloAgain;


import android.app.Activity;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.iRprojects.HelloAgain.Player;



public class Options extends Activity {

        EditText name;
        Button getName;

        TextView playerName;

        Player playerOne;




        @Override

        public void onCreate(Bundle options) {
            super.onCreate(options);



            setContentView(R.layout.options);


            name = (EditText) findViewById(R.id.tName);
            getName = (Button) findViewById(R.id.bGetName);

            playerName = (TextView) findViewById(R.id.vName);

            getName.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    playerOne.Name = name.getText().toString(); //This is the problem

                    playerName.setText(playerOne.Name);
                }
            });


        }


}

I'm trying to use the input from the EditText to set the player name, but when I click the "getName" button it crashes. I think its pretty safe to say I either set up the class wrong, created a class member wrong, or called/declared the variables wrong. Any help would be appreciated.

Heres the debug log if it helps:

HelloAgain [Android Application]    
    DalvikVM[localhost:8600]    
        Thread [<1> main] (Suspended (exception NullPointerException))  
            Options$1.onClick(View) line: 46    
            Button(View).performClick() line: 2485  
            View$PerformClick.run() line: 9080  
            ViewRoot(Handler).handleCallback(Message) line: 587 
            ViewRoot(Handler).dispatchMessage(Message) line: 92 
            Looper.loop() line: 123 
            ActivityThread.main(String[]) line: 3683    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 507  
            ZygoteInit$MethodAndArgsCaller.run() line: 864  
            ZygoteInit.main(String[]) line: 622 
            NativeStart.main(String[]) line: not available [native method]  
        Thread [<8> Binder Thread #2] (Running) 
        Thread [<7> Binder Thread #1] (Running) 

Solution

  • You have to create an instance of Player and assign it to playerOne variable.

    Player playerOne = new Player();