I'm trying to make a deck of playing cards in Java, but when I run my code an error comes up saying java.lang.NullPointerException.

This is my code:

public class Deck
{

Card[] card= new Card[52];
  Deck(){

        for(int i = 0; i<13; i++){
      card[i].value = i+1;
      card[i].suit= "Heart";
     if(card[i].value==1){
         card[i].value= 11;
        }
     if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
         card[i].value = 10;
        }
    }

          for(int i = 0; i<26; i++){
      card[i+13].value = i+1;
      card[i+13].suit= "Club";
     if(card[i].value==1){
         card[i].value= 11;
     }
      if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
         card[i].value = 10;
     }
    }

          for(int i = 0; i<39; i++){
      card[i+26].value = i+1;
      card[i+26].suit= "Diamond";
     if(card[i].value==1){
         card[i].value= 11;
     }
     if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
         card[i].value = 10;
     }
    }

          for(int i = 0; i<52; i++){
      card[i+39].value = i+1;
      card[i+39].suit= "Spade";
     if(card[i].value==1){
         card[i].value= 11;
     }
     if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
         card[i].value = 10;
     }

    }
   }

}




public class Card
{
  public int value;
  public String suit;
  Carta(){

    }
}

So what i basically want, is if someone could help me see why am i getting that error?

Or how else could I make a deck of playing cards in java?

Please post the full text of the error message. It has the line number where the exception happened which you'll need to find the problem. Look at the line where the error occurs, find the variable with the null value and then backtrack in the code to see why it does not have a valid non-null valud.

Edited 4 Years Ago by NormR1

Well the error message says it happens in line 8. So i believe the error is related with the "value variable" ...

This is the exact error message that i am getting:

java.lang.NullPointerException
    at Deck.<init>(Mazo.java:15)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:740)

That happened when I added a println... :S

      Deck(){
            for(int i = 0; i<13; i++){
            System.out.println(card[i]);
          card[i].value = i+1;
          card[i].suit= "Heart";
         if(card[i].value==1){
             card[i].value= 11;
            }
         if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
             card[i].value = 10;
            }
        }
              for(int i = 0; i<26; i++){
          card[i+13].value = i+1;
          card[i+13].suit= "Club";
         if(card[i].value==1){
             card[i].value= 11;
         }
          if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
             card[i].value = 10;
         }
        }
              for(int i = 0; i<39; i++){
          card[i+26].value = i+1;
          card[i+26].suit= "Diamond";
         if(card[i].value==1){
             card[i].value= 11;
         }
         if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
             card[i].value = 10;
         }
        }
              for(int i = 0; i<52; i++){
          card[i+39].value = i+1;
          card[i+39].suit= "Spade";
         if(card[i].value==1){
             card[i].value= 11;
         }
         if(card[i].value==10 || card[i].value==11 || card[i].value== 12){
             card[i].value = 10;
         }
        }
       }
    }
    public class Card
    {
      public int value;
      public String suit;
      Card(){
        }
    }

The thing is that it pops out the second I run the program...

Mazo means deck, It says mazo because my native language is Spanish so the original code is in spanish... I just translated it to english so it could be easier to understand for you guys...

 at Deck.<init>(Mazo.java:15)

What is the source on line 15 where the error occurs?

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.