在创建Do While循环后修复java.lang.NullPointerException

在创建Do While循环后修复java.lang.NullPointerException,java,Java,我遇到了java.lang.NullPointerException错误,希望您提供有关如何修复该错误的信息。我知道它有一个空值的问题,但我不知道是什么 基本上,只有当我尝试建立一个Do-While循环时,问题才开始出现。如果删除循环并编辑代码,以便在调用getHistogram()方法时只运行一次,则不会发生错误 下面是我的一些代码,以了解我目前正在做什么以及错误发生的位置。我能做些什么来阻止此错误的发生 第1页 public class MainClass { /** *

我遇到了java.lang.NullPointerException错误,希望您提供有关如何修复该错误的信息。我知道它有一个空值的问题,但我不知道是什么

基本上,只有当我尝试建立一个Do-While循环时,问题才开始出现。如果删除循环并编辑代码,以便在调用getHistogram()方法时只运行一次,则不会发生错误

下面是我的一些代码,以了解我目前正在做什么以及错误发生的位置。我能做些什么来阻止此错误的发生

第1页

public class MainClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int optionSelected;
        int NumberOfSuits;
        int NumberOfRanks;
        int numberOfCardsDealt = 0;
        int SizeOfDeck;
        int numberOfCards;


           System.out.printf( "How many suits? " );
           NumberOfSuits = getDecision();

           System.out.printf( "How many ranks? " );
           NumberOfRanks = getDecision();

           SizeOfDeck = NumberOfSuits * NumberOfRanks;

        DeckofCards newDeck = new DeckofCards();
        newDeck.setDeckofCards(NumberOfRanks, NumberOfSuits);
        newDeck.createDeckofCards();

        do {
            System.out.println ( newDeck.toString() );
            System.out.printf ( "1=Shuffle, 2=Deal 1 Hand, 3= Deal 100000 Times,"
                    + "4=Quit: " );
            optionSelected = getDecision();


            switch ( optionSelected ) {
                case 1:
                    newDeck.shuffleDeckofCards();
                    break;
                case 2:
                    numberOfCards = getCardDecision();
                    numberOfCardsDealt = numberOfCardsDealt + numberOfCards;

                    if ( SizeOfDeck > numberOfCardsDealt ) {
                        String[] dealCards = newDeck.dealCards ( numberOfCards );
                        for ( String printCardsDealt : dealCards ) {
                            System.out.println ( printCardsDealt );
                        } 

                    }
                    else {
                        System.out.println ( "Deck must be re-shuffled" );
                        numberOfCardsDealt = 0;
                        break;
                    }
                    break;
                case 3:
                    numberOfCards = getCardDecision();
                    int[] histogram = newDeck.getHistogram( numberOfCards );
                        for ( int printHistogram : histogram ) {
                            System.out.println ( printHistogram );
                        }
                    break;

                }
        } while ( optionSelected != 4 );




    }

    public static int getDecision() {
        String numberMatch;
        int optionSelected = 0;
        Scanner in = new Scanner(System.in);

        do { 
            numberMatch = in.next();
                if( numberMatch.matches("[0-9]+" ) ){ 
                    optionSelected = Integer.parseInt( numberMatch );
                }
                if ( optionSelected < 1 || optionSelected > 9 ){
                    System.out.println("Try again!");
                }
        } while ( optionSelected < 1 || optionSelected > 9 );

        return optionSelected;

    }

    public static int getCardDecision() {
        String numberMatch;
        int optionSelected = 0;
        Scanner in = new Scanner(System.in);

        do { 
            System.out.printf ( "How many cards? " );
            numberMatch = in.next();
                if( numberMatch.matches("[0-9]+" ) ){ 
                    optionSelected = Integer.parseInt( numberMatch );
                }
                if ( optionSelected < 1 || optionSelected > 9 ){
                    System.out.println("Try again!");
                }
        } while ( optionSelected < 1 || optionSelected > 9 );

        return optionSelected;

    }
}
StackTrace

Exception in thread "main" java.lang.NullPointerException
at MainClass.DeckofCards.getHistogram(DeckofCards.java:105)
at MainClass.MainClass.main(Assignment4.java:72)
C:\Users\mikel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
在对case3和整个其他方法调用进行更改后,这是getHistogram()的另一个选项。使用此选项,我遇到了ArrayIndexOutOfBoundsException的问题

        case 3:
            numberOfCards = getCardDecision();
            newDeck.getHistogram( numberOfCards );
                //for ( String printHistogram : histogram ) {
                  //  System.out.println ( printHistogram );
                //}
            break;

public void getHistogram( int numberOfCardsDealt ) {
    int sum = 0;
    int count = 0;
    this.numberOfCardsDealt = numberOfCardsDealt;
    this.numberOfCards = numberOfCardsDealt - 1;
    CardsDealtInTotal = numberOfCardsDealt + numberOfCards;



    DeckofCards histoDeck = new DeckofCards();

    do {
        if ( SizeOfDeck > CardsDealtInTotal ) {
            String[] histogramDeck = dealCards ( numberOfCardsDealt );

            for ( int i = 0; i <= numberOfCards; i++ ) {

                sum = sum + histoDeck.getSumOfHand( histogramDeck[i]);
                System.out.println ( sum );

        }

    }
    else {
        shuffleDeckofCards();
        CardsDealtInTotal = 0;       

    }
    count+= 1;
    } while ( count != 6 );


   }  


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at MainClass.DeckofCards.dealCards(DeckofCards.java:74)
    at MainClass.DeckofCards.getHistogram(DeckofCards.java:101)
    at MainClass.MainClass.main(MainClass.java:72)
C:\Users\mikel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

我已经编译并运行了你的程序。。。我将所有文本合并到一个文件中,所以我会显示带有错误的完整行

小结:您需要调查dealtDeck-初始化它时,它包含哪些值以及在哪里更新它

Exception in thread "main" java.lang.NullPointerException
    at DeckofCards.getHistogram(MainClass.java:207)
    at MainClass.main(MainClass.java:64)
这一行:

       sum = sum + histoDeck.getSumOfHand( dealtDeck[i]); //Check and send each position of hand to be added
我启动阵列,继续前进:

private String dealtDeck[] = new String[1000];
下一个错误

Exception in thread "main" java.lang.NullPointerException
    at java.util.regex.Matcher.getTextLength(Unknown Source)
    at java.util.regex.Matcher.reset(Unknown Source)
    at java.util.regex.Matcher.<init>(Unknown Source)
    at java.util.regex.Pattern.matcher(Unknown Source)
    at DeckofCards.getSumOfHand(MainClass.java:224)
    at DeckofCards.getHistogram(MainClass.java:207)
    at MainClass.main(MainClass.java:64)
线程“main”java.lang.NullPointerException中的异常 位于java.util.regex.Matcher.getTextLength(未知源) 位于java.util.regex.Matcher.reset(未知源) 位于java.util.regex.Matcher。(未知源) 位于java.util.regex.Pattern.matcher(未知源) 在DeckofCards.getSumOfHand(MainClass.java:224) 在DeckofCards.getHistogram(MainClass.java:207) 在MainClass.main(MainClass.java:64) 发生在以下位置:

    Pattern pattern = Pattern.compile("^S([\\d]+)R([\\d]+)$");
    Matcher matcher = pattern.matcher(s);  //<----- here from histoDeck.getSumOfHand( dealtDeck[i]);
Pattern=Pattern.compile(“^S([\\d]+)R([\\d]+)$”;

匹配器匹配器=模式匹配器//后堆栈跟踪。另外,
newDeck
dealtDeck
都是null。当某些内容初始化为null时,不会抛出null指针。Java中的所有对象都是可空的,所以我猜newDeck中有一些空值。使用方法链接,你确定newDeck会返回此
?如果我能够理解我已经通读的帖子,以便尝试修复我的代码,我不会再发布其他问题,以期获得更好的见解或不同措辞的答案,从而找到解决方案@SergeiSirik,但我感谢你的输入。我将查看newDeck,看看这是否是我的问题JohEker,感谢您的输入我将查看Stacktrace,我不确定如何100%调试我的指导老师简要介绍了它。我看看能不能找到一个能帮我解决问题的视频。我正在调查newDeck和dealtDeck,因为你认为它们是空的,我来看看那里发生了什么。感谢你的意见@pkpnd@Twissted顺便说一下,如果您发布完整的代码,我们可以为您提供更多帮助。现在,没有人能看到
newDeck
dealtDeck
从何而来,因为它们没有在您发布的代码中声明。我唯一的另一个选择是完全取消使用int[]的想法,我想这在技术上是没有意义的,因为我可以从方法getHistogram()打印出一条语句。这样做之后,我又遇到了另一个问题,即我似乎无法使用我的循环重新排列数据组,并最终导致ArrayIndexOutOfBoundsException错误,我以前曾经遇到并修复过该错误。因此,我正在尝试再次使用它。如果所有其他方法都失败了,我想我可能会在每一手牌发完后重新洗牌,即使这不是我最初想要做的。更新以显示如果你想快速达到峰值,我目前所做的更改。我仍在测试,试图找出我的问题。我还需要更改我的变量名,因为它们越来越令人困惑。。
private String dealtDeck[] = new String[1000];
Exception in thread "main" java.lang.NullPointerException
    at java.util.regex.Matcher.getTextLength(Unknown Source)
    at java.util.regex.Matcher.reset(Unknown Source)
    at java.util.regex.Matcher.<init>(Unknown Source)
    at java.util.regex.Pattern.matcher(Unknown Source)
    at DeckofCards.getSumOfHand(MainClass.java:224)
    at DeckofCards.getHistogram(MainClass.java:207)
    at MainClass.main(MainClass.java:64)
    Pattern pattern = Pattern.compile("^S([\\d]+)R([\\d]+)$");
    Matcher matcher = pattern.matcher(s);  //<----- here from histoDeck.getSumOfHand( dealtDeck[i]);