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?

Recommended Answers

All 11 Replies

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.

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

What abouf card[i]? What is its value? Add a println to print out its value to see.

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

Post the code showing the println.

      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(){
        }
    }

What was the value of i when the NPE happens? Add that to the println

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

What is the source on line 15 in Mazo?

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.