-2

Can someone help me check ?

error:

java.lang.NullPointerException

How do I remove this error ?

my main Class:

public class deck {

    public static void main(String[] args) {
        Deck app = new Deck();
        app.Deck();
    }

}

my Deck class:

    public class Deck {
    public int TOTALCARDS;
    Card[] d;
    public int nH;


    public void createDeck() {
            String[] suitsArray = new String[4];

            for(int i=0; i<numArray.length; i++) {
                numArray[i] = i+1;
            }

            for (int i=0; i<13; i++) { 
                if (i!=11){
                deck[i+25] = new Card(suitsArray[2], i+1);
                }
                else if(i > 10){
                    deck[i+25] = new Card(suitsArray[2], i+1);
                }
                }
    for (int i=0; i<13; i++) {
        deck[i+25].display();
    }
}

This is the Card Class.

public class Card {

        public String suit;
        public int number;


        public Card(String s, int n){
            this.suit = s;
            this.number = n;
        }


        public String words;
        public String getTitle(){
            String 
            if (number == 1){
                words = " Ace";

            }
            else if (number ==3){
                words = " Three";
            }
            else if (number ==4){
                words = " Four";
            }
            else if (number ==5){
                words = " Five";
            }
            else if (number ==6){
                words = " Six";
            }
            else if (number ==7){
                words = " Seven";
            }
            else if (number ==8){
                words = " Eight";
            }
            else if (number ==9){
                words = " Nine";
            }
            else if (number ==10){
                words = " Ten";
            }
            else if (number ==11){
                words = " Jack";
            }
            else if (number ==12){
                words = " Queen";
            }
            else if (number ==13){
                words = " King";
            }
            displayTitle = suit + words;
                    if (displayTitle.equalsIgnoreCase("Diamond Queen")){
                        displayTitle= "DIAMOND QUEEN";
                    }


        return (displayTitle);

    }

        public void display(){
            System.out.println("< " + get() + " >");
        }
    }

When I run the code I got

Hearts Ace
Hearts Two
Hearts Three
Hearts Four
Hearts Five
Hearts Six
Hearts Seven
Hearts Eight
Hearts Nine
Hearts Ten
Hearts Jack
Exception in thread "main" java.lang.NullPointerException
    at Deck.createDeck(Deck.java:57)
    at Deck.<init>(Deck.java:9)
    at deckMain.main(deckMain.java:5)
Jerry
  • 114
  • 2
  • 13
  • 1
    Can you give the exception traceback, it's super helpful. The traceback will give a line number... which would be great because I don't see room for a NullPointer in what you posted. Also this http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – en_Knight Dec 03 '14 at 03:10
  • 1
    Where/How do you declare `deck`? It seems you are leaving out relevant code. – takendarkk Dec 03 '14 at 03:13
  • 1
    There is simply not enough information here to tell where an NPE could be coming from. A hunch of mine is that it's coming from within your `Card` constructor, but I can't be sure. Nothing here would cause an NPE (provided `deck` is actually instantiated). – Makoto Dec 03 '14 at 03:16
  • @Makoto I think Takendarkk is right to ask how op define deck array in its code. do not you agree? – Kick Buttowski Dec 03 '14 at 03:17
  • 1
    @KickButtowski: No doubt. But until we get that, this question is completely unanswerable. – Makoto Dec 03 '14 at 03:18
  • Is -3 completely necessary? OP seems new, that's for sure, but he did attempt to post his code and his issue (and kindly asked for an easier to understand answer since he's new). For a new user that's unimaginable. – theGreenCabbage Dec 03 '14 at 03:28
  • @theGreenCabbage Downvotes will probably continue to roll in the longer OP refuses to reply to requests for more info. – takendarkk Dec 03 '14 at 03:31
  • @Takendarkk He's probably doing it as we are downvoting him. Not a lot of time has passed since the request for more code was posted. – theGreenCabbage Dec 03 '14 at 03:32
  • 2
    I am sorry for the unclear codes, as I am first time asking question on stackoverflow. – Jerry Dec 03 '14 at 03:32
  • Plenty of time has passed. Also, this is why you can reverse a vote when a post is edited. – takendarkk Dec 03 '14 at 03:32
  • It seems to me like `words` is null at this point `String displayTitle = suit + words;` at the top of `getTitle()`. However, those code you posted doesn't show you calling any methods that would get there so hard to say. – takendarkk Dec 03 '14 at 03:34
  • @Takendarkk please refer to the code, I have insert the output of the program. – Jerry Dec 03 '14 at 03:36
  • The code you posted never calls any of the methods in your Card class, but your output indicates you are doing just that. Have you still left out relevant code? – takendarkk Dec 03 '14 at 03:38
  • @FrezzeyTsuna I kind of ran your code and did not get such error – Kick Buttowski Dec 03 '14 at 03:39
  • 1
    You need to post the _exact_ code which produces this error, otherwise we can't help you. – takendarkk Dec 03 '14 at 03:39
  • @FrezzeyTsuna can you post up your driver class too? – Kick Buttowski Dec 03 '14 at 03:41
  • @Takendarkk I help put the neccessary code in already I am really sorry. I not in the good mind state, been trying to solve this for very long already. – Jerry Dec 03 '14 at 03:50
  • @FrezzeyTsuna No worries. We keep asking because we want to help you. Everyone has been new and frustrated at some point. – takendarkk Dec 03 '14 at 03:51
  • When you see in the error `at Deck.createDeck(Deck.java:57)`, 57 is the line number of the code that gave the error. Knowing which line it is helps identify the problem. – outlyer Dec 03 '14 at 05:04
  • @FrezzeyTsuna Don't just edit your answer to remove code, other people may experience the same problem and can learn from your question. If you don't want it to stay, press the delete option. – Joetjah Dec 03 '14 at 08:09

3 Answers3

1

EDIT: See the comment by Kick Buttowski below, this is at best a partial answer.


You're calling Card.display()

for (int i=0; i<13; i++) {
    deck[i+25].display();
}

Which in turn calls Card.getTitle()

public void display(){
    System.out.println("< " + getTitle() + " >");
}

The problem lies there:

public String getTitle(){
    String displayTitle = suit + words; // <-- Problem
    if (number == 1){
        words = " Ace";
    }
    // ...

At the marked line, words is not set yet, so it's null. Accessing its value will raise a NullPointerException.

As Takendarkk pointed out, you can overcome this with minimal changes to your code by initialising it either in your constructor or in the declaration:

public class Card {
    // ...
    String words = "";

Or, since (as far as we know) words is only used within display(), move it there:

public void display() {
    String words = ""; // Still need to be initialised
    // ...
outlyer
  • 3,933
  • 2
  • 14
  • 18
0

EDIT: Your card size is 51 ... it should be 52

if (i!=11){
    deck[i+25] = new Card(suitsArray[2], i+1);
}
else if(i > 11){
    deck[i+25] = new Card(suitsArray[2], i+1);
}

This doesn't add up, what if i = 11?? You do nothing as

i!=11 =  false and i > 11 == false;

this means the else if (i > 11) will never execute. also your code does the exact same thing??

if you are trying to initalise all 52 cards you can do something like:

for (int i=0;i<deck.size;i++) {
   deck[i] = new Card(suitsArray[i/13], numArray[i % 13]);

EDIT: after more of an explanation of what you are doing .....

for (int i=0, j=0;j<deck.length;i++) {
    if ((i/13) == 2 && (i%13) == 11)
        continue;
    deck[j++] = new Card( suitsArray[i/13], (i % 13));
}

also in card have (much clearer) static String[] titles = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; and

public String getTitle() {

        return suit + " " + titles[number];

}
outlyer
  • 3,933
  • 2
  • 14
  • 18
Kevin Hussey
  • 1,582
  • 1
  • 10
  • 17
0

It is because your value at deck[11] is null because you skipped putting an instance of Card at that index.

edit: [should be deck[i + 25]], where i = 11;

Regarding the answers pointing to the variable word being null as reason of NPE. I doubt this. I have tried the code in my machine and it is not giving me NPE. (see also Concatenating null strings in Java)

Community
  • 1
  • 1
user3308224
  • 423
  • 2
  • 5
  • 16
  • Not really, although that's probably the intended behaviour. But the code is `if (i!=11) { ... } else if(i > 10) { ... }`. With i=11, the else branch is entered, and it does create a card in `deck[i+25]`. – outlyer Dec 03 '14 at 04:20
  • hmmm... the original condition was if( i > 11). (see answer by KevH) – user3308224 Dec 03 '14 at 04:41
  • yes, looking through edits it looks like it changed a few times – outlyer Dec 03 '14 at 04:42