1

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();
    }
14
  • Can you println the value of (manojugador[cantmano]) before the problem line? Commented Jun 29, 2011 at 4:02
  • Que dice la linea 58 de blackjack.java ? com.pruebas.blackjack.blackjack.dibujar(blackjack.java:58) Commented Jun 29, 2011 at 4:05
  • Please post the code that initializes the manojugador array. I suspect it's not being initialized completely. Commented Jun 29, 2011 at 4:09
  • what does getNumero() do? and what does it return? Commented Jun 29, 2011 at 4:09
  • @OscarRyz: The OP has indicated what line the NullPointerException is thrown with the // <-- ERROR comment. Commented Jun 29, 2011 at 4:10

2 Answers 2

3

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.

2
  • 1
    +1 just for conversation above. ( I don't intend to disregard you answer though ) Commented Jun 29, 2011 at 5:06
  • @OscarRyz: I just upvoted your answer too. The conversation was fun for me too. Commented Jun 29, 2011 at 5:16
2

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!

7
  • So do i have to loop trough cantmano[i] calling the constructor? Commented Jun 29, 2011 at 4:19
  • No no no, you have to either fill your array with valid values, or trim your array to the correct size. I would also recommend you to use a List<ManoJugador> instead. Commented Jun 29, 2011 at 4:19
  • Other ( bad ) option would be to check if the element is null, but that's only a way to hide the underlaying problem. Commented Jun 29, 2011 at 4:22
  • Post that in the original code. Seems like you're leaving empty slots. Perhaps the last one. Commented Jun 29, 2011 at 4:24
  • @alfa64 Definitivamente tienes algunos nulls en tu arreglo, revisa esa sección de tu código. Saludos. Commented Jun 29, 2011 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.