I've passed the code when I add player = new MyPlayer() in onCreate. but
next step, I change it to array:
MyPlayer[] players;
.
.
.
.
onCreate....
players = new MyPlayer[4];
then it also NPE. why it pass when I use single player but use array do not
pass?
== full code in main activity====
package hktsang56.android.mjhelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class name_input extends Activity {
final static int NUM_PLAYER = 4;
private MyPlayer[] players;
MyPlayer player;
MarkSetting markSetting;
private EditText[] namesET;
Button bt[];
private TextView tv;
int[] names = new int[]{
R.id.et_p1,
R.id.et_p2,
R.id.et_p3,
R.id.et_p4
};
int[] btIds = new int[]{
R.id.but_sendName,
R.id.but_clearName
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.player_input);
players = new MyPlayer[NUM_PLAYER];
namesET = new EditText[NUM_PLAYER];
bt = new Button[btIds.length];
player = new MyPlayer();
tv = (TextView) findViewById(R.id.tv_monitor);
tv.setText("onCreate");
initButIds();
}
private void initButIds(){
for (int i=0; i<btIds.length; i++){
bt[i] = (Button) findViewById(btIds[i]);
bt[i].setOnClickListener(myClickListener);
tv.setText("initButIds");
}
}
private View.OnClickListener myClickListener = new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("onClick");
but_click(v.getId());
}
};
public void but_click(int btId){
switch (btId){
case R.id.but_sendName:
tv.setText("but_click");
setName();
break;
case R.id.but_clearName:
clearName();
break;
}
}
private void setName() {
// TODO Auto-generated method stub
String name="Null";
for (int i=0; i<NUM_PLAYER; i++){
namesET[i] = (EditText) findViewById(names[i]);
}
for (int i=0; i<NUM_PLAYER; i++){
name = namesET[i].getText().toString().trim();
players[i].setPlayerName(name); // remark: player.setPlayerName(name);
<------------ can be run.
}
tv.setText(players[2].getPlayerName());
}
private void clearName() {
// TODO Auto-generated method stub
for (int i=0; i<NUM_PLAYER; i++){
namesET[i].setText("");
}
}
}
On Wed, May 23, 2012 at 6:29 PM, Daniel Drozdzewski <
[email protected]> wrote:
> Jason
>
> name == null
>
> would not cause NPE, as it is simple assignment to a member variable
> in MyPlayer class. If there was anything else within setName() that
> caused NPE, we could see it on the top of the stacktrace.
>
> The cause is that *player* reference in *player_input.setName()* is null.
>
> The way player object is being constructed is wrong: (player = new
> Player;) This code should not compile, so it begs the question, how
> did the OP manage to run it.
>
> There are many other issues with this code from the first look, like
> naming conventions, and auto generated comments dotting it, just to
> name the few.
>
>
>
> Daniel
>
>
>
>
> On 23 May 2012 10:56, Jason Teagle <[email protected]> wrote:
> >> I've create an object class but it cannot work. any mistake of my code?
> >
> >
> >> private void setName() {
> >> // TODO Auto-generated method stub
> >> getNameText();
> >> for (int i=0; i<NUM_PLAYER; i++){
> >> player.setPlayerName(name); // name is a String get from EditText by
> some
> >> code before
> >> }
> >> }
> >
> >
> >> when I run the programme, it have the following error code:
> >
> >
> >> 05-23 09:24:35.115: E/AndroidRuntime(438):
> java.lang.NullPointerException
> >> 05-23 09:24:35.115: E/AndroidRuntime(438): at
> hktsang56.android.mjhelper
> >> .name_input.setName(name_input.java:81)
> >
> >
> > Which of the lines in your source code is line 81? Is it the line that
> says
> > getNameText()? We need to see that method's definition, if that is the
> case,
> > since I can't see any other problem (if 'name' were coming out null, then
> > the error would be in setPlayerName() rather than in setName() - in fact
> it
> > probably wouldn't generate an error in such a case).
> >
> > Show us the code for getNameText(). You are possibly trying to access a
> > control that you have not filled out correctly.
> >
> >
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> > http://groups.google.com/group/android-developers?hl=en
>
>
>
> --
> Daniel Drozdzewski
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
--
HK Tsang
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en