I am a beginner in java programming. I am trying to develop a program but when I ran my program which is posted below and it came back with this error:
Exception in thread "main" java.lang.NullPointerException
at ChargeAccount1.isValid(ChargeAccount1.java:39)
at ChargeAccount1Test.main(ChargeAccount1Test.java:22)
Here is my code:
import java.io.*;
import java.util.*;
public class ChargeAccount1 {
private int [] valid;
public void main(String[] args) throws IOException {
FileReader file = new FileReader("Account.txt");
valid=new int [10];
int i=1;
Scanner input = new Scanner(file);
while(input.hasNext())
{
valid[i] = input.nextInt();
i++;
}
input.close();
}
public boolean isValid(int number) {
boolean found = false;
int n = 0;
while (!found && n < valid.length) {
if (valid[n] == number) {
found = true;
} else {
n++;
}
}
return found;
}
}
public class ChargeAccount1Test {
public static void main(String[]args)throws IOException{
ChargeAccount1 in = new ChargeAccount1();
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a charge account number:");
String account = scan.nextLine();
int number = Integer.parseInt(account);
if (in.isValid(number)) {
System.out.println("This is a valid account number");
} else {
System.out.println("This is an invalid account number");
}
}
}
mainofChargeAccount1to be executed, since not the root of the program. Indeed,ChargeAccount1Testis the root.