Question: How to resolve this issue? Error : Exception in thread main java.lang.NullPointerException at Program8.sort(Program8.java:25) at Program8.main(Program8.java:107) Program8: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class
How to resolve this issue?
Error:
Exception in thread "main" java.lang.NullPointerException
at Program8.sort(Program8.java:25)
at Program8.main(Program8.java:107)
Program8:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Program8 {
public static void sort(OfficeCustomerClass customersObject[])
{
int min;
for(int i=0;i
{
min=i;
for(int j=i+1;j
if(customersObject[j].getId()< customersObject[min].getId())
min=j;
if(min != i)
{
OfficeCustomerClass temp = customersObject[i];
customersObject[i] = customersObject[min];
customersObject[min] =temp;
}
}
}
public static void main(String[] args) throws FileNotFoundException {
OfficeCustomerClass customersObject[];
File file = new File("OfficeCustomerClass.txt");
Scanner fileScan = new Scanner(file);
int records;
records = Integer.parseInt(fileScan.nextLine());
customersObject = new OfficeCustomerClass[records];
int index = 0;
String nameElement="", email="",taxLiabilityElement="";
int id=0;
double bill=0;
while(fileScan.hasNext() && index < records)
{
try {
nameElement = fileScan.nextLine();
id = Integer.parseInt(fileScan.nextLine());
bill = Double.parseDouble(fileScan.nextLine());
email = fileScan.nextLine();
taxLiabilityElement = fileScan.nextLine();
double tax = Double.parseDouble(taxLiabilityElement);
customersObject[index] = new TaxNonExemptCustomer(nameElement,id,bill,email,tax);
index++;
}catch(NumberFormatException ex)
{
customersObject[index] = new TaxExemptCustomerClass(nameElement,id,bill,email,taxLiabilityElement);
index++;
}
}
fileScan.close();
sort(customersObject);
int page = 0;
int i=0;
while(i
page++;
int j=0;
System.out.println(" \t Office Supplies Inc \t Page : "+page);
System.out.println("Customer Name \t ID \t Bill balance \t email \t\t Tax Liability");
// displaying 2 records per page, change this to display the desired number of records per page
while(i < customersObject.length && j <2) {
System.out.println( customersObject[i]);
i++;
j++;
}
}
}
}
TaxNonExemptCustomer
public class TaxNonExemptCustomer extends OfficeCustomerClass{
private double taxLiabilityElement;
public TaxNonExemptCustomer()
{
super();
taxLiabilityElement=0;
}
public TaxNonExemptCustomer(String nameElement, int id, double billBalanceElement, String email,double taxLiabilityElement)
{
super(nameElement,id,billBalanceElement,email);
settaxLiabilityElement(taxLiabilityElement);
}
public void settaxLiabilityElement(double taxLiabilityElement)
{
this.taxLiabilityElement = ((taxLiabilityElement*getbillBalanceElement())/100);
}
public double gettaxLiabilityElement()
{
return taxLiabilityElement ;
}
public String toString()
{
return(super.toString()+"\t"+taxLiabilityElement);
}
}
TaxExemptCustomer
public class TaxExemptCustomerClass extends OfficeCustomerClass{
private String taxLiabilityElement;
public TaxExemptCustomerClass()
{
super();
taxLiabilityElement= "";
}
public TaxExemptCustomerClass(String nameElement, int id, double billBalanceElement, String email,String taxLiabilityElement)
{
super(nameElement,id,billBalanceElement,email);
this.taxLiabilityElement = taxLiabilityElement;
}
public void settaxLiabilityElement(String taxLiabilityElement)
{
this.taxLiabilityElement = taxLiabilityElement;
}
public String gettaxLiabilityElement()
{
return taxLiabilityElement;
}
public String toString()
{
return (super.toString()+"\t"+taxLiabilityElement);
}
}
Input File:
100 Amazon 900 20000.00 purchasing@amazon.com 0.08 Nordstrom 210 50000.00 purchasing@nordstrom.com 0.07 Rutgers 010 32000.00 purchasing@rutgers.edu education Alamo 520 23000.00 purchasing@alamo.com 0.05 Kean 001 158000.50 purchasing@kean.edu education Allied 100 85300.00 purchasing@allied.com 0.06 JoesInc 950 999999.00 purchasing@joesinc.com 0.03 BostonU 697 340020.23 purchasing@tufts.edu education
OfficeCustomerClass
import java.io.FileNotFoundException;
import java.util.Scanner;
public class OfficeCustomerClass {
private String nameElement;
private int id;
private double billBalanceElement;
private String email;
public OfficeCustomerClass()
{
nameElement="";
id=0;
billBalanceElement=0;
email="";
}
public OfficeCustomerClass(String nameElement, int id, double billBalanceElement, String email)
{
this.nameElement = nameElement;
this.id = id;
this.billBalanceElement = billBalanceElement;
this.email = email;
}
public void setnameElement(String nameElement)
{
this.nameElement = nameElement;
}
public void setId(int id)
{
this.id = id;
}
public void setbillBalanceElement(double billBalanceElement)
{
this.billBalanceElement = billBalanceElement;
}
public void setEmail(String email)
{
this.email = email;
}
public String getnameElement()
{
return nameElement;
}
public int getId()
{
return id;
}
public double getbillBalanceElement()
{
return billBalanceElement;
}
public String getEmail()
{
return email;
}
public String toString()
{
return (nameElement+"\t"+id+"\t"+billBalanceElement+"\t\t"+email);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
