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);
    
       }
}

Recommended Answers

All 3 Replies

I'm disappointed how you devastated the code which I give to you. Also, why didn't you posted in same thread or closed the previous thread?

It is nice that you declare size of your array on the line #100 cust = new Customer[recSize]; however you never actually initialize each object of array as cust[c] = new Customer(); PS: Increment of your integer variable "c" should be done at the end of the loop or solved it differently (for loop for example), otherwise you start storing from cust[1] instead of cust[0] and get ArrayIndexOutOfBoundsException

Hi i'm sorry about it because i really don't know how to apply your code to mine. Thank for your reply i gt the the data store in the array but may i know how to sort it according to name of the record? Is it using compareTo method to do it? Below is my code

public static int readFile(String fileName) throws IOException{
      
     BufferedReader br = null;
     String line;
     String name;
     String add;
     String dob;
     String phoneNum;
     String read;
     double accBal;
     int accid;
     int c = 0;
     

     try {
            System.out.println("Number of record read: " + recSize + "\n");
            
            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 ")){

                     cust[c] = new Customer();
                     accid = Integer.parseInt(st.nextToken().trim());
                     cust[c].setAccountID(accid);

                   }
                   
                   if (read.equals("Name ")){
                       
                     //cust[c] = new Customer();
                     name = st.nextToken().trim().toUpperCase();
                     cust[c].setName(name);                    
                     
                   }
                    
                   if (read.equals("Address ")){
                       
                     //cust[c] = new Customer();
                     add = st.nextToken().trim();
                     cust[c].setAddress(add);
                     
                   }
                    
                   if (read.equals("DOB ")){
                       
                     //cust[c] = new Customer();
                     dob = st.nextToken().trim();
                     cust[c].setDob(dob);  
                     
                   }
                    
                   if (read.equals("Phone Number ")){
                       
                     //cust[c] = new Customer();
                     phoneNum = st.nextToken().trim();
                     cust[c].setPhoneNumber(phoneNum);

                   }
                    
                   if (read.equals("Account Balance ")){
                       
                     //cust[c] = new Customer();
                     accBal = Double.parseDouble(st.nextToken().trim());
                     cust[c].setBalance(accBal);
       
                     c++; 
                   }  
                   
                }
                 
            }  
             
            cd.displayRecords();
            br.close();       
         } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
         }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.