Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

nullpointer exception and cant figure out how to go about fixing it. please help

ID: 3772120 • Letter: N

Question

nullpointer exception and cant figure out how to go about fixing it. please help

////////main class

import java.io.*;
import java.util.ArrayList;
/**
* Write a description of class validate_driver here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Validate_driver
{


    public static void main(String[] args)
    {

        String line = "";
        ArrayList<ValidateCC> cards = new ArrayList<ValidateCC>();

        try
        {

            BufferedReader inFile = new BufferedReader(new FileReader("ccnumbers.txt"));

            line = inFile.readLine();
            while(line != null)
            {
                cards.add(new ValidateCC());
                line = inFile.readLine();

            }
         
            inFile.close();
        }
        catch(Exception e)
        {
            System.out.println("Problem opening or reading file");
        }
     
        for(ValidateCC card : cards)
        {
            String cardType = card.creditCardType();///////////////////////////////////////////////////////////(nullpointer here)
            String cardNum = card.getCardNumber();
            if(cardType.equals("Unknown"))
            {
                System.out.println("Card number " + cardNum + " is an unknown credit card type.");
            }
            else
            {
                if(card.Check(line) == true)
                {
                    System.out.println(cardType + " number " + cardNum + " is valid.");
                }
                else
                {
                    System.out.println(cardType + " number " + cardNum + " is not valid.");
                }
            }
        }
    }
}

////////validateCC class

public class ValidateCC extends CC_type
{

public static boolean Check(String ccNumber)
        {
            int sum = 0;
            boolean alternate = false;
            for (int i = ccNumber.length() - 1; i >= 0; i--)
            {
                    int n = Integer.parseInt(ccNumber.substring(i, i + 1));
                    if (alternate)
                    {
                            n *= 2;
                            if (n > 9)
                            {
                                    n = (n % 10) + 1;
                            }
                    }
                    sum += n;
                    alternate = !alternate;
            }
            return (sum % 10 == 0);
    }
}

////////////CC_type class

public class CC_type
{
    public String cardNumber;
    public String getCardNumber()
    {
        return cardNumber;
    }
    public void setCardNumber(String cardNumber)
    {

        this.cardNumber = cardNumber;

    }
   public String creditCardType()
    {
        String cardType = "Unknown";
        if(cardNumber.startsWith("44")|| cardNumber.startsWith("45"))//////////////////////////////////////////////////////(nullpointer here)
        {
            if(cardNumber.length() == 13 || cardNumber.length() == 16)
            {
                cardType = "Visa";
            }
        }
        if(cardNumber.startsWith("48")|| cardNumber.startsWith("49"))
        {
            if(cardNumber.length() == 16)
            {
                cardType = "Visa Electron";
            }
        }
        if(cardNumber.startsWith("51") || cardNumber.startsWith("53"))
        {
            if(cardNumber.length() == 16 || cardNumber.length() == 19)
            {
                cardType = "MasterCard";
            }
        }
        if(cardNumber.startsWith("34") || cardNumber.startsWith("37"))
        {
            if(cardNumber.length() == 15)
            {
                cardType = "American Express";
            }
        }
        if(cardNumber.startsWith("60"))
        {
            if(cardNumber.length() == 16)
            {
                cardType = "Discover";
            }
        }
        if(cardNumber.startsWith("31")|| cardNumber.startsWith("33"))
        {
            if(cardNumber.length() == 16)
            {
                cardType = "JCB";
            }
        }
        if(cardNumber.startsWith("54")|| cardNumber.startsWith("55"))
        {
            if(cardNumber.length() == 16)
            {
                cardType = "Diners Club - North America";
            }
        }
        if(cardNumber.startsWith("30"))
        {
            if(cardNumber.length() == 14)
            {
                cardType = "Diners Club - Carte Blanche";
            }
        }
        if(cardNumber.startsWith("36"))
        {
            if(cardNumber.length() == 14)
            {
                cardType = "Diners Club - International";
            }
        }
        if(cardNumber.startsWith("58"))
        {
            if(cardNumber.length() == 16 || cardNumber.length() == 19)
            {
                cardType = "Maestro";
            }
        }
        if(cardNumber.startsWith("67"))
        {
            if(cardNumber.length() == 16 || cardNumber.length() == 19)
            {
                cardType = "LASER";
            }
        }
        if(cardNumber.startsWith("63"))
        {
            if(cardNumber.length() == 16)
            {
                cardType = "InstaPayment";
            }
        }

        return cardType;
    }
}

Explanation / Answer

Thrown when an application attempts to use null in a case where an object is required. These include:

Calling the instance method of a null object.

Accessing or modifying the field of a null object.

Taking the length of null as if it were an array.

Accessing or modifying the slots of null as if it were an array.

Throwing null as if it were a Throwable value.

public class CreditCardValidator {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scan = new Scanner(new File(args[0])); // read from a file
        while (scan.hasNextLine()) {
            String str = scan.nextLine();
            if (validate(str)) {
                System.out.println(creditCardType(str));
            } else {
                System.out.println("Invalid");
            }
        }
    }

    private static boolean validate(String str) {
        String reverse = new StringBuilder().append(str).reverse().toString();
        int[] array = new int[str.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = Integer.parseInt("" + reverse.charAt(i));
        }
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            if (i % 2 == 1) {
                array[i] *= 2;
                if (array[i] > 9) {
                    array[i] -= 9;
                }
            }
            sum += array[i];
        }
        return sum % 10 == 0;
    }

    private static String creditCardType(String str) {
        int input1Char = Integer.parseInt(str.substring(0, 1));
        int input2Chars = Integer.parseInt(str.substring(0, 2));
        int input3Chars = Integer.parseInt(str.substring(0, 3));
        int input4Chars = Integer.parseInt(str.substring(0, 4));
        int input6Chars = Integer.parseInt(str.substring(0, 6));

        if (input2Chars == 34 || input2Chars == 37) {
            if (str.length() == 15) {
                return "American Express";
            }
        }

        if (input4Chars == 6011
                || (input6Chars >= 622126 && input6Chars <= 622925)
                || (input3Chars >= 644 && input3Chars <= 649)
                || input2Chars == 65) {
            if (str.length() == 16) {
                return "Discover";
            }
        }

        if (input2Chars >= 51 && input2Chars <= 55) {
            if (str.length() >= 16 && str.length() <= 19) {
                return "MasterCard";
            }
        }

        if (input1Char == 4) {
            if (str.length() >= 13 && str.length() <= 16) {
                return "Visa";
            }
        }
        return "Invalid Credit Card Type";
    }
}