0

EDIT: [SOLVED] Thanks again -- I think I learned something new that I should have known about arrays:

When creating object[] something = new object[int]; something all point to null and need to be explicitly told what to point to.

After browsing through possible solutions I still haven't found the answer to my question:

I am attempting to call a method from another class but it seems that it isn't initialized/instantiated. I thought I did but apparently not? I keep getting a NPE

Main method (countingCards.java):

public class deck {
     card[] deckCard = new card[51];

    /** Rules for the deck of card.
      * 1. You cannot have same value of cards with same suite.
      * --- i.e) no two queens of hearts
     **/

    public void test() {
        System.out.println( deckCard.length );
        System.out.println( deckCard[1].getValue() );
    }

Card class:

public class card {
    private String value = "hello";
    private String suite = "suiteHello";

    // Method to return value
    public String getValue() {
        return value;
    }
    // Method to return suite
    public String getSuite() {
        return suite;
    }
    // Method to set value
    public void setValue(String s) {
        value = s;
    }
    // Method to set suite
    public void setSuite(String s) {
        suite = s;
    }
    // Method to test
    public void testing() {
        value = "test";
        System.out.println( value );
    }
}

deck class:

public class deck {
    card[] deckCard = new card[51];

    /** Rules for the deck of card.
      * 1. You cannot have same value of cards with same suite.
      * --- i.e) no two queens of hearts
     **/

    public void test() {
        System.out.println( deckCard.length );
        System.out.println( deckCard[1].getValue() );
    }
}

The Exception I get says

 Exception in thread "main" java.lang.NullPointerException
 at deck.test(deck.java:11)
 at countingCards.main(countingCards.java:4)

Anyone have any idea what's going on with my code?

5
  • 4
    An array doesn't initialize the elements inside it. They are initially referencing null. Given that, what do you think deckCard[1].getValue() does? Commented Jan 5, 2014 at 21:50
  • This type of problem is nothing to do with the method in the other class. If you execute myobj.mymethod() when myobj is null then you will get an NPE on that line of code. Commented Jan 5, 2014 at 21:59
  • Awesome, thanks. So I just made a bunch of null pointers and didn't actually do anything with them. Commented Jan 5, 2014 at 22:11
  • Please don't mark your question as solved in the title. Instead you should encourage to convert the comment that help you to an answer and accept it. Commented Jan 5, 2014 at 22:15
  • Got it. I thought it would be helpful to others to see that the question was solved in case anyone was looking for a quick and dirty answer to similar problems but I'll refrain from that. I did however accept the answer shared by Vannens. Commented Jan 5, 2014 at 22:19

3 Answers 3

5

You have not initialized the elements of the array. You need something like this in your deck class.

int l = deckCard.length();

for (i = 0; i<l; i++) {
    deckCard[i] = new Card();
}

PD: the name class should be Card with uppercase.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I just added a massive amount of for loops to initialize each card to their respective values (Ace, 2, 3, 4... etc). Thanks again!
Well, it is not necessary a massive amount of for loops. You can create two arrays with values (1 - 13) and suites (heart, blabla..). Then, using nested loops you can choose a value and suite and create a Card(): deckCard[i] = new Card(value, suite);
2

deckCard[1].getValue() raises a NullPointerException because all the 51 elements of the deckCard array are null until you explicitly initialize them.

Comments

2

You need to actually add values to this array, otherwise it will always return null. Try making a quick for-loop and add some random variables to it, then try it.

You can't get the value for something that was never initially created.

2 Comments

Thanks -- The guy above this comment had the same answer as you did; I'm an idiot. Thanks again!
No problem. We all start somewhere, not an idiot, just still learning the ropes! Just as I am in some ways!

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.