简体   繁体   English

尝试使用其他类中的方法时出现空指针异常

[英]Null Pointer Exception when attempting to use methods from another class

EDIT: [SOLVED] Thanks again -- I think I learned something new that I should have known about arrays: 编辑:[已解决]再次感谢-我想我学到了一些关于数组的新知识:

When creating object[] something = new object[int]; 创建object []时,某些东西=新对象[int]; something all point to null and need to be explicitly told what to point to. 所有东西都指向null,需要明确告知要指向什么。

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 我不断得到NPE

Main method (countingCards.java): 主要方法(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? 有人知道我的代码怎么了吗?

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. PD:名称类别应为大写Card

deckCard[1].getValue()引发NullPointerException因为在您明确初始化它们之前, deckCard数组的所有 51个元素都为null

You need to actually add values to this array, otherwise it will always return null . 您实际上需要向该数组添加值,否则它将始终返回null Try making a quick for-loop and add some random variables to it, then try it. 尝试制作一个快速的for循环并向其中添加一些随机变量,然后再尝试。

You can't get the value for something that was never initially created. 您无法获得最初从未创建过的东西的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM