0

I have this method called HandScore that takes a Card array called hand, and returns the value of the hand, by adding up all the ranks. It also adds 10, to any ranks that have a value greater than 10. However, when I try to run the code, it gives me a NullPointerException. I know why, but, I don't know how to fix it. Is there any feedback I can get?

public int HandScore(Card[] hand)
{
  int total=0;
  int[] rAnk=new int[0];
  for(int i=0;i<hand.length;i++)
  {
    rAnk[i]=hand[i].rank;
    total=total+rAnk[i];
  }
  if(hand.rank>10)
  {
    total=total+10;
  }
  return total;
}
DNA
  • 42,007
  • 12
  • 107
  • 146
theUser
  • 91
  • 5
  • `ranK` size is 0 when you declare it as `rAnk=new int[0]` – Laerte Apr 21 '15 at 22:13
  • You need to read the stacktrace more critically as it will tell you which line throws the exception. So look at it, and then you tell us! – Hovercraft Full Of Eels Apr 21 '15 at 22:15
  • Which line gives a NullPointerException? – DNA Apr 21 '15 at 22:15
  • 3
    Either `hand` or `hand[i]` is null. The duplicate question link will guide you on the right way. – Makoto Apr 21 '15 at 22:15
  • @HovercraftFullOfEels is there a specific rule for handling error questions? For example, say I got an error but cannot figure out what is causing it, even after checking the stacktrace, is it generally allowed to post a question asking for helping finding out what is causing the error? – Ungeheuer Apr 21 '15 at 22:18
  • 1
    @JohnnyCoder: If it's likely to be a common question, the best thing to do is to inspect your stacktrace and then inspect several similar questions. Often that's enough to help you solve your problem, but if it's not, then ask your question, show your pertinent code, indicate which line(s) are involved, and explain the specifics of why the typical canonical questions and answers aren't helping you solve your issue. – Hovercraft Full Of Eels Apr 21 '15 at 22:22

0 Answers0