cantmano is the index, it starts on 0. Another method increases with cant++. I recheck that 0 <= cantmano <= 10
public void dibujar(){
//actualiza la pantalla
if (cantmano >开发者_运维技巧;= 0 && cantmano < 10 && cantcroupier >= 0 && cantcroupier < 10){
TextView textomano = (TextView)findViewById(R.id.textView3);
TextView textocroupier = (TextView)findViewById(R.id.textView5);
CharSequence buffer = textomano.getText();
textomano.setText( buffer + " " +
String.valueOf(manojugador[cantmano].getPalo())+ " de " +
String.valueOf(manojugador[cantmano].getNumero()) ); // <-- ERROR
textocroupier.setText( String.valueOf(cantmano) );
}
}
I get a nice
Caused by: java.lang.NullPointerException at com.pruebas.blackjack.blackjack.dibujar(blackjack.java:58) at com.pruebas.blackjack.blackjack.onCreate(blackjack.java:23) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2717)
EDITS:
.getNumero()
returns the int with the value of a requested CARD. (playing card type)
.getPalo()
returns an int where 1= diamonds, etc.
initialization of manojugador:
Carta manojugador[]= new Carta[10];
constructor of Carta:
public Carta(){
int palo=0;
int numero=0;
}
MIDNIGHT UPDATE: With some improvements i managed it to get over the error. BUT now the array has all 0 values when written. This has to be an easy to solve but that's the final step before accepting the best answer.
Here's the method that adds cards:
public void hit(View v){
//sacan cartas
if (cantmano < manojugador.length){
manojugador[cantmano]=mazo.darcarta(); //adds a random Card to the manojugador. mazo means deck.
manocroupier[cantcroupier]=mazo.darcarta(); //adds a random Card to the manojugador. mazo means deck.
cantmano++;
cantcroupier++;
}
dibujar();
}
You must initialize the array and it's elements. If you simply have this:
Carta manojugador[]= new Carta[10];
then all 10 elements of the array will be null
. You must also initialize each element. Something like this:
for(int i=0, length=manojugador.length; i<length; i++) {
manojugador[i] = new Carta();
}
Update:
I see that in your hit()
method, I see you have:
if (cantmano <= 8) {
Shouldn't that be:
if (cantmano < 10) {
Or even better:
if (cantmano < manojugador.length) {
I think that what is happening in your code to cause the NullPointerException
is that manojugador[9]
can never be initialized.
Your manojugador
array null elements and your cantmano
is somehow pointing to one of those null elements.
For instance, let's say you have:
ManoJugador [] cantmano = new ManoJugador[10];
cantmano[0] = new ManoJugador();
cantmano[1] = new ManoJugador();
cantmano[2] = new ManoJugador();
You array beyond index 3 you have nulls. That's why when your try to get the numero of null you get NullPointerException.
EDIT
As per your edit:
Yeap, definitely, you have a null value there. Debug that part and you'll see some null values
hint: System.out.println( java.util.Arrays.toString( someArray ));
Suerte!
精彩评论