简体   繁体   English

为什么Java方法返回null?

[英]Why does Java method return null?

I am trying to debug why my Java method returns null. 我正在尝试调试为什么我的Java方法返回null。 Here are the details. 这是详细信息。

We are making a simple card "game" and Im having trouble with external method call and creating a new object.. It is a deck of cards ..So 1 class for the cards, 1 class for deck and 1 for the Game 我们正在制作一个简单的卡片“游戏”,而Im在调用外部方法和创建新对象时遇到了麻烦。这是一副纸牌..因此,一类为纸牌,一类为纸牌,一类为游戏

This is my class the code goes into 这是我的课程代码

 public class Game
 {
  private InputReader reader;
  private Deck deck;
  private ArrayList<Card> listCard;

/**
 * Constructor for objects of class Game
 */
public Game()
{
    deck = new Deck();
    reader = new InputReader()
    listCard = new ArrayList<Card>();
}

/**
 *
 */
public void dealCard()
{
   listCard.add(deck.takeCard());
}

}// End of Game class

and this is the deck class which I will grabbing methods from 这是甲板类,我将从中获取方法

 import java.util.ArrayList;
 /**
  * Write a description of class Deck here.
  * 
  * @author  
  * @version 2012.05.31
  */
 public class Deck
{
private ArrayList<Card> redblue;

/**
 * Main constructor for objects of class Deck
 */
public Deck()
{
    redblue = new ArrayList<Card>();
}


   public Card takeCard()
{
      **return redblue.remove(0);**  /// this is the Index.pointer.exception
}



}
}// End of class

So my problem is im trying to pull the first card off the deck and add it to my "hand" ..So im trying to call dealCard() which calls takeCard()..The takeCard() works fine but when I try and call if through dealCard() it returns null and errors out because i cant add null to arrayList.. I think my problem might be external method calling not step up with the right variables, I dunno 所以我的问题是我试图将第一张卡从卡组中取出并添加到我的“手”中。所以我试图调用DealCard(),后者调用takeCard().. takeCard()正常,但是当我尝试调用是否通过DealCard()返回null和错误,因为我无法将null添加到arrayList ..我认为我的问题可能是外部方法调用,而没有使用正确的变量来增强,我不知道

Thanks in advance 提前致谢

***Edited. ***编辑。 Removed irrelevant methods and Class.. 删除了不相关的方法和类。

Look at your constructor for Deck: 查看Deck的构造函数:

public Deck()
{
    redblue = new ArrayList<Card>();
}

After that constructor has run, how many cards are there in the redBlue ArrayList? 在构造函数运行之后,redBlue ArrayList中有多少张卡?

What do you think might happen if you try to remove a card from that ArrayList? 如果您尝试从该ArrayList中移除卡,您认为会发生什么?

I think you need to post some more code from your main(game class) showing us the usage of your deck and card classes. 我认为您需要从主游戏类中发布更多代码,向我们展示甲板和纸牌类的用法。 Only thing that took my attention now is this: 现在唯一引起我注意的是:

public Card takeCard()
{
      **return redblue.remove(0);**  /// this is the Index.pointer.exception
}

According to this an exception can be thrown if: 据此,在以下情况下会引发异常:

IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

Are you sure your deck is filled with cards? 您确定您的甲板上装满卡了吗? You could add a check to your method. 您可以在方法中添加检查。 If your list of cards is empty, 0 = it's size. 如果您的卡片列表为空,则0 =它的大小。

public void dealCard()
{
   Card card = deal.takeCard();

   if (card != null)
      listCard.add(deck.takeCard());
}

public Card takeCard()
{
    if ( !this.redblue.isEmpty() )
        return redblue.remove(0);

    return null;        
} 

Somehow the first element of your 不知何故,您的第一个元素

redblue

is null. 一片空白。 Nulls are allowed in an ArrayList. 在ArrayList中允许为空。

My guess is that there is more code where you fill in the Deck of cards and the first time around you are adding null to the ArrayList 我的猜测是,有更多的代码可以在卡片组中填充,并且第一次将null添加到ArrayList

Check the ArrayList specification here. 此处检查ArrayList规范。

It seems to me like redblue doesn't actually have anything in it! 在我看来,redblue实际上没有任何东西! when you intialise it like so: 当您像这样初始化它时:

redblue = new ArrayList<Card>();

You're just creating an empty container for Cards, not actually putting cards in it. 您只是为Cards创建一个空容器,而不是将Cards实际放入其中。 What you may want to do is create a function to generate the cards for redblue.. something like.. 您可能想要做的是创建一个函数来为redblue ..之类的东西生成卡。

public ArrayList<Card> createInitialDeck(){ 
  //Create an empty ArrayList
  //Do some code here to create new cards..
       //you might want to consider nested for loops if you're creating 13 cards of 4 different suits for example
       //while inside those for loop add each new card object to your array list

   //return arrayList;
}

then instead of redblue = new ArrayList<Card>(); 然后代替redblue = new ArrayList<Card>(); you'd do redblue = createInitialDeck(); 你会做redblue = createInitialDeck(); .

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

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