Hi i am facing a problem of passing data from a text file to array. i have read the text file but somehow i can't pass it into the array and it keep giving me nullPointerException. Can someone help? Is there something wrong with my code? And how do i sort the array according to customer name that i retrieve from the text file?
CustomerData.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class CustomerData {
// private static ArrayList custRec = new ArrayList();;
private static int recSize;
private static Customer cust[];
private static CustomerData cd = new CustomerData();
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner input = new Scanner(System.in);
String choice,value,fileName;
do{
menu();
System.out.println("Please enter your choice: ");
value = input.next();
choice = value;
if(choice.equals("1")){
System.out.println("Please enter name of input file: ");
fileName = input.next();
setArraySize(fileName);
readFile(fileName);
}else if(choice.equals("2")){
System.out.println("choice 2");
}else if(choice.equals("3")){
System.out.println("choice 3");
}else if(choice.equals("q")){
System.out.println("exit");
}
}while(!choice.equals("q"));
}
public static void setArraySize (String fileName) throws IOException{
BufferedReader br = null;
int c = 0;
String line,read;
try {
br = new BufferedReader(new FileReader(fileName));
line = null;
while((line = br.readLine()) != null){
StringTokenizer st1 = new StringTokenizer(line, "=");
while(st1.hasMoreTokens()){
read = st1.nextToken();
if(read.equals("Account Id ")){
c++;
}
}
}
recSize = c;
br.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
public static int getArraySize(){
return recSize;
}
public static int readFile(String fileName) throws IOException{
BufferedReader br = null;
String line;
String name = null;
String add = null;
String dob = null;
String phoneNum = null;
String read;
double accBal = 0;
int accid = 0;
int c = 0;
try {
System.out.println("Number of record read: " + recSize);
br = new BufferedReader(new FileReader(fileName));
line = null;
cust = new Customer[recSize];
while((line = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(line, "=");
while(st.hasMoreTokens()){
read = st.nextToken();
if(read.equals("Account Id ")){
c++;
cust[c].setAccountID(Integer.parseInt(st.nextToken().trim()));
}
if (read.equals("Name ")){
cust[c].setName(st.nextToken().trim().toUpperCase());
}
if (read.equals("Address ")){
cust[c].setAddress(st.nextToken().trim());
}
if (read.equals("DOB ")){
cust[c].setDob(st.nextToken().trim());
}
if (read.equals("Phone Number ")){
cust[c].setPhoneNumber(st.nextToken().trim());
}
if (read.equals("Account Balance ")){
cust[c].setBalance(Double.parseDouble(st.nextToken().trim()));
}
}
}
cd .displayRec();
br.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
return c;
}
public static void menu(){
//Menu
System.out.println("\n************Menu************");
System.out.println("1) Input data");
System.out.println("2) Display data");
System.out.println("3) Output data");
System.out.println("q) Quit\n");
}
public void displayRec(){
for(int i = 0; i < recSize; i++){
cust[i].getRecords();
}
}
}
Customer.java
public class Customer {
/**
* @param args the command line arguments
*/
private int accountID;
private String name;
private String address;
private String dob;
private String phoneNumber;
private double balance;
public Customer(){}
public void setAccountID(int accId){
accountID = accId;
}
public int getAccountID() {
return accountID;
}
public void setName(String Custname){
name = Custname;
}
public String getName() {
return name;
}
public void setAddress(String add) {
address = add;
}
public String getAddress() {
return address;
}
public void setDob(String dateOfBirth){
dob = dateOfBirth;
}
public String getDob() {
return dob;
}
public void setPhoneNumber(String phoneNum) {
phoneNumber = phoneNum;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setBalance(double accBal) {
balance = accBal;
}
public double getBalance() {
return balance;
}
public void getRecords(){
System.out.println("Name = " + name );
System.out.println("Account Balance = " + balance);
System.out.println("Account Id = " + accountID);
System.out.println("DOB = " + dob);
System.out.println("Phone Number = " + phoneNumber);
System.out.println("Address = " + address);
}
}