Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a string input and I'm trying to convert it to a character array then store the numbers from that array in a manner that will let me use them in a credit card program. This is how i am currently trying to do it.

I'm currently getting Null Pointer Exception at :

charDigits[x - invalid] = charInput[x];

I need to be able to use the integers on the credit card algorithm from a string input.

public class testFunction {
    private char[] charInput;
    private char[] charDigits;
    private int[] intInput;
    private int invalid = 0;
    private String input = "125-6543-3356";

    public testFunction()
    {
        charInput = input.toCharArray();

        for(int x = 0; x < charInput.length; x++)
        {
            if (charInput[x] == '0' || charInput[x] == '1' || charInput[x] == '2' || charInput[x] == '3' || charInput[x] == '9' ||
                charInput[x] == '4' || charInput[x] == '5' || charInput[x] == '6' || charInput[x] == '7' || charInput[x] == '8')
            {               
                charDigits[x - invalid] = charInput[x];
            }
            else
            {
                invalid++;
            }
            System.out.println("charDigits: " + x + ": " + charDigits[x] );
        }

        for(int i = 0; i < charDigits.length; i++)
        {
            intInput[i] = Integer.parseInt(charDigits[i] + "");
        }
    }

        public static void main(String[] args)
        {
            testFunction test = new testFunction();
        }
}
share|improve this question
    
try using something like if(c >= '0' && c <= '9') rather than chaining all those || together. As you can see here the code points for 0 to 9 are contiguous. –  NameSpace Mar 23 at 22:11

1 Answer 1

up vote 0 down vote accepted

I don't see you initializing charDigits anywhere. Its initial value is null, hence accessing it like charDigits[i] would raise an NPE (you should have been able to easily deduce this based on the documentation of NullPointerException). You'll need to initialize charDigits first to an array of whatever size it should be (looks like max size will be charInput.length):

charDigits = new char[charInput.length];

However, to make the rest of your logic work, you'll also want to keep track of how many digits in charDigits are actually valid (since ultimately it can be shorter than charInput). You have a lot of options:

  • Consider doing valid++ in the positive case instead of invalid++ in the negative case, and index by charDigits[valid] instead of [x - invalid]. At the end of the loop, valid will contain the number of characters copied to charDigits, and intInput can be initialized accordingly.
  • Since the elements of charDigits will be initialized to 0, stop copying to intInput when charDigits[i] == 0.
  • Use a dynamic array for charDigits (e.g. an ArrayList<Character>) and append as needed.
  • Use a StringBuilder or something instead for charDigits, and append as needed.
  • Etc.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.