2 Replies - 593 Views - Last Post: 26 February 2010 - 06:52 AM Rate Topic: -----

#1 Guest_Shreya Sharma*


Reputation:

Java- Question on array of objects

Posted 26 February 2010 - 05:46 AM

I have been trying this question for two weeks and keep getting the null pointer error again and again


The question

import java.util.*;
import java.io.*;
import java.lang.String.*;

class Customer
{
    int accountID;
    String name;
    String address;
    String DOB;
    String phoneNo;
    double accountBal;
}

public class Lab2
{
    static Customer c[]=new Customer[30];
    static int len=0;

    Lab2()
    {
        for(int i=0;i<30;i++)
        {
            c[i].accountID=0;
            c[i].name="   ";
            c[i].address="   ";
            c[i].DOB="  ";
            c[i].phoneNo="   ";
            c[i].accountBal=0;
        }
    }
    
    static boolean checkDuplicate(String name)
    {
        for(int i=0;i<len;i++)
        {
            if(name.compareToIgnoreCase(c[i].name)==0)
                return true;
        }
        return false;
    }

    static void sortRecord()
    {
       String temp;
       for(int i=1;i<len;i++)
       {
           for(int j=0;j<len-i;j++)
           {
              if(c[j].name.compareTo(c[j+1].name)>0)
              {
                  temp=c[j].name;
                  c[j].name=c[j+1].name;
                  c[j+1].name=temp;
              }
           }
       }
    }

    static int readFile(String filename)
    {
        try
        {
            Customer n=new Customer();
            String str1, str2, str3;
            FileReader iStream= new FileReader(filename);
            BufferedReader cStream= new BufferedReader(iStream);
            String input=" ";

            while(input!=null)
            {
               input=cStream.readLine();
               StringTokenizer aString= new StringTokenizer(input);
               str1=aString.nextToken();
               str2=aString.nextToken();
               str3=aString.nextToken();
               n.accountID=Integer.parseInt(aString.nextToken().trim());
               System.out.println(n.accountID);

               input=cStream.readLine();
               StringTokenizer bString= new StringTokenizer(input);
               str1=bString.nextToken();
               str2=bString.nextToken();
               n.name=bString.nextToken();
               while(bString.hasMoreTokens())
               {
                   n.name+=" "+bString.nextToken();
               }
               n.name=n.name.toUpperCase();
               System.out.println(n.name);

               input=cStream.readLine();
               StringTokenizer cString= new StringTokenizer(input);
               str1=cString.nextToken();
               str2=cString.nextToken();
               n.address=cString.nextToken();
               while(cString.hasMoreTokens())
               {
                   n.address+=" "+cString.nextToken();
               }
               System.out.println(n.address);

               input=cStream.readLine();
               StringTokenizer dString= new StringTokenizer(input);
               str1=dString.nextToken();
               str2=dString.nextToken();
               n.DOB=dString.nextToken();
               System.out.println(n.DOB);

               input=cStream.readLine();
               StringTokenizer eString= new StringTokenizer(input);
               str1=eString.nextToken();
               str2=eString.nextToken();
               str3=eString.nextToken();
               n.phoneNo=eString.nextToken();
               System.out.println(n.phoneNo);

               input=cStream.readLine();
               StringTokenizer fString= new StringTokenizer(input);
               str1=fString.nextToken();
               str2=fString.nextToken();
               str3=fString.nextToken();
               n.accountBal=Double.parseDouble(fString.nextToken());
               System.out.println(n.accountBal);
               cStream.readLine();
               if(!checkDuplicate(n.name))
               {
                   c[len]=n;
                   len++;
               }
            }
            sortRecord();
            return len;
        }
         catch(ArrayIndexOutOfBoundsException e)
         {
             System.out.println("Error! More than 30 records!");
             return len;
         }
         catch(IOException e)
         {
             System.out.println("Error");
             return len;
         }
    }

    static void displayRecords()
    {
        int i=0;
        while(i<len)
        {
            System.out.println("Name: "+c[i].name);
            System.out.println("Balance: "+c[i].accountBal);
            System.out.println("Account ID: "+c[i].accountID);
            System.out.println("Address: "+c[i].address);
            System.out.println("Phone No: "+c[i].phoneNo);
            System.out.println("Date of Birth: "+c[i].DOB);
            i++;
        }
    }

    static int writeFile(Customer c[])
    {
        try
        {
            Writer output;
            output=new BufferedWriter(new FileWriter("newCustomers.txt"));
            for(int i=0;i<len;i++)
            {
                output.write("Name = ");
                output.write(c[i].name);
                output.write("\n");
                output.write("Account Balance = ");
                output.write(Double.toString(c[i].accountBal));
                output.write("\n");
                output.write("Account Id = ");
                output.write(c[i].accountID);
                output.write("\n");
                output.write("Address = ");
                output.write(c[i].address);
                output.write("\n");
                output.write("DOB = ");
                output.write(c[i].DOB);
                output.write("\n");
                output.write("Phone Number = ");
                output.write(c[i].phoneNo);
                output.write("\n");
                output.write("     ");
                output.write("\n");
            }
            output.close();
            return len;
        }
        catch(Exception e)
        {
            System.out.println("Error");
            return -1;
        }
    }

    static void menu()
    {
         char ch='a';
         String filename, input;
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter 1 to input data");
         System.out.println("Enter 2 to display data");
         System.out.println("Enter 3 to output data");
         System.out.println("Enter Q to quit");
         System.out.println("Enter choice: ");
         while(ch!='Q'||ch!='q')
         {
         input=sc.next();
         ch=input.charAt(0);
         switch(ch)
         {
             case '1': System.out.println("Enter input filename:");
                       filename=sc.next();
                       int num=readFile(filename);
                       System.out.println(num+" record read");
                       break;
             case '2': displayRecords();
                       break;
             case '3': System.out.println("Enter output filename:");
                       filename=sc.next();
                       break;
             case 'Q': System.out.println("Program exitted");
                       break;
             case 'q': System.out.println("Program exitted");
                       break;
             default: System.out.println("Invalid choice!");
                      break;
         }
     }
   }

    public static void main(String[] args)
    {
        menu();
    }
}





I really really need help especially with the insertRecord() function.

Thank you!

Is This A Good Question/Topic? 0

Replies To: Java- Question on array of objects

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Java- Question on array of objects

Posted 26 February 2010 - 06:39 AM

static Customer c[]=new Customer[30];

here you declare an Array of 30 Customer Objects.
but actually, before you assign Customer Object to this array, the values inside it point to "nothing", i.e they are null.

so inside that for loop:
for(int i=0;i<30;i++) 
        { 
            c[i].accountID=0; 
            c[i].name="   "; 
            c[i].address="   "; 
            c[i].DOB="  "; 
            c[i].phoneNo="   "; 
            c[i].accountBal=0; 
        } 

you try to assign values to null Objects.
you will have to create a new Customer first, and then pass it's variables a value:
for(int i=0;i<30;i++) 
        { 
            //new customer created
            c[i] = new Customer();
            c[i].accountID=0; 
            c[i].name="   "; 
            c[i].address="   "; 
            c[i].DOB="  "; 
            c[i].phoneNo="   "; 
            c[i].accountBal=0; 
        } 


However, why aren't you initialize all those Customer variables in the Customer class inside the constructor?
add to the Customer class a constructor:
class Customer 
{ 
//all variables..
public Customer(){
c[i].accountID=0; 
            name="   "; 
            address="   "; 
            DOB="  "; 
            phoneNo="   "; 
            accountBal=0; 
        } 
}


then, inside your lab class, all you will have to do in that for loop is create a new Customer object. the variables will be initlized then:
for(int i=0;i<30;i++) 
        {
         c[i] = new Customer();
}

Was This Post Helpful? 2
  • +
  • -

#3 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Java- Question on array of objects

Posted 26 February 2010 - 06:52 AM

japanir sir is great
hats off
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1