Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 循环中的空指针异常_Arrays_Loops_Pointers_Null_Blackjack - Fatal编程技术网

Arrays 循环中的空指针异常

Arrays 循环中的空指针异常,arrays,loops,pointers,null,blackjack,Arrays,Loops,Pointers,Null,Blackjack,在我正在编写的blackjack程序中,我在使用while循环时遇到一个空指针异常 以下是循环本身: int total = 0; for (int x = 5; x <= hitOrStayCounter; x++) { total = total + temp[x].getBJ(); //Line 57 where the error is occuring. } 下面是getBJ方法: public int getBJ() { return bJValu

在我正在编写的blackjack程序中,我在使用while循环时遇到一个空指针异常

以下是循环本身:

int total = 0; 
  for (int x = 5; x <= hitOrStayCounter; x++) 
  {

    total = total + temp[x].getBJ(); //Line 57 where the error is occuring. 
  }
下面是getBJ方法:

public int getBJ()
{
return bJValue; 
}
我怀疑这是因为我试图在循环中使用数组,但在括号中插入了一个int值。我试图解决这个问题,不知道你们能否帮我解决。我找不到任何类似的问题,但如果你找到一个,请直接告诉我,我会删除这个。谢谢你抽出时间

java.lang.NullPointerException
at Blackjack.main(Blackjack.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at       edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这是我初始化临时值的循环

 while (hitOrStay.equalsIgnoreCase("hit"))
  {
    temp[hitOrStayCounter] = cardDeck.deal();
    System.out.println("You are delt a " + temp[hitOrStayCounter].getBJ()); 
    hitOrStayCounter = hitOrStayCounter + 2;
    System.out.println("Would you like to hit again?"); 
    hitOrStay = keyboard.next(); 
  }

您的
hitOrStayCounter
5
。您将从
5
开始循环


for(int x=0;x)您使用的编译器是什么?@Maroun Maroun我目前正在用Dr.Java编写代码,其中一行是第57行?@rookieB抱歉,让我在中评论一下。“我使用的是while循环中的异常”?此外,hitOrStay计数器不应等于5。我有一个计数器,上面的hitOrStay计数器在用户每次点击或发牌时都会递增。你初始化了temp吗?是的,它在上面的代码中。它是我编写的card类的一部分。它也会有帮助吗?实际上并没有解决我意识到的问题。我的temp应该我在另一次编辑中添加了更多的代码,以显示在上面的循环中temp的初始化位置。
 while (hitOrStay.equalsIgnoreCase("hit"))
  {
    temp[hitOrStayCounter] = cardDeck.deal();
    System.out.println("You are delt a " + temp[hitOrStayCounter].getBJ()); 
    hitOrStayCounter = hitOrStayCounter + 2;
    System.out.println("Would you like to hit again?"); 
    hitOrStay = keyboard.next(); 
  }
Card[] temp = new Card[300]; This will not initialize
Card c1;
Card c2;..