I am trying to read in data from a text file. How it works (well supposed to heh) is like this... I have a text file with info for 13 people (each person has 15 attributes). The text file starts with the number 13 so that the first line read in is the number 13, and the program uses that number as a loop counter. I have displayed the Employment.java code and the Print.java code. Everything is done in employee and then print is used to display it.
Also the text file contents have been displayed.
The code compiles fine, but I get a run time error... here it is:
init:
deps-jar:
compile:
run:
Begin Month: Begin Day: Begin Year: Name: Address: Phone #: SSN: Age: Sex: Employer Name: Employer Address: Employer Phone #: Years of Employment: Title: Salary:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Found I-O problem java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 0 seconds)
Employment.java
/* Todd Riethmiller
* January 19th 2009
* Data Structures
* Professor Kenison
* Read in data from a text file and store it in an array
*/
package Employee;
import java.io.*;
public class Employment
{
// Create variables
private String month;
private String day;
private String year;
private String name;
private String addy;
private String number;
private String ssn;
private String age;
private String sex;
private String ename;
private String eaddy;
private String enumber;
private String nyear;
private String title;
private String salary;
public void readEmploy()
{
try
{ // Set up to read in from a text file
File inFile = new File("EMPLOYEE.txt");
FileReader fr = new FileReader(inFile);
BufferedReader br = new BufferedReader(fr);
// Variables to count items and read them in
String numPpl;
int pplCount;
int i;
// Read in how many items there are
numPpl = br.readLine();
pplCount = Integer.parseInt(numPpl);
Employment [] thePpl = new Employment[pplCount];
// Read in the items as separated by the deliminator
for (i = 0; i < pplCount; i++)
{
thePpl[i].setMonth(br.readLine());
thePpl[i].setDay(br.readLine());
thePpl[i].setYear(br.readLine());
thePpl[i].setName(br.readLine());
thePpl[i].setAddy(br.readLine());
thePpl[i].setNumber(br.readLine());
thePpl[i].setSSN(br.readLine());
thePpl[i].setAge(br.readLine());
thePpl[i].setSex(br.readLine());
thePpl[i].setEname(br.readLine());
thePpl[i].setEaddy(br.readLine());
thePpl[i].setEnumber(br.readLine());
thePpl[i].setNyear(br.readLine());
thePpl[i].setTitle(br.readLine());
thePpl[i].setSalary(br.readLine());
}
// Print out each line
for (i = 0; i < pplCount; i++)
{
System.out.println(thePpl[i]);
}
// Finished reading so close file
br.close();
}
// Break program if problem found
catch (Exception e)
{
System.out.println("Found I-O problem " + e);
}
}
// Mutators
public void setMonth(String month)
{
this.month = month;
}
public void setDay(String day)
{
this.day = day;
}
public void setYear(String year)
{
this.year = year;
}
public void setName(String name)
{
this.name = name;
}
public void setAddy(String addy)
{
this.addy = addy;
}
public void setNumber(String number)
{
this.number = number;
}
public void setSSN(String ssn)
{
this.ssn = ssn;
}
public void setAge(String age)
{
this.age = age;
}
public void setSex(String sex)
{
this.sex = sex;
}
public void setEname(String ename)
{
this.ename = ename;
}
public void setEaddy(String eaddy)
{
this.eaddy = eaddy;
}
public void setEnumber(String enumber)
{
this.enumber = enumber;
}
public void setNyear(String nyear)
{
this.nyear = nyear;
}
public void setTitle(String title)
{
this.title = title;
}
public void setSalary(String salary)
{
this.salary = salary;
}
// Accessors
public String getMonth()
{
return month;
}
public String getDay()
{
return day;
}
public String getYear()
{
return year;
}
public String getName()
{
return name;
}
public String getAddy()
{
return addy;
}
public String getNumber()
{
return number;
}
public String getSSN()
{
return ssn;
}
public String getAge()
{
return age;
}
public String getSex()
{
return sex;
}
public String getEname()
{
return ename;
}
public String getEaddy()
{
return eaddy;
}
public String getEnumber()
{
return enumber;
}
public String getNyear()
{
return nyear;
}
public String getTitle()
{
return title;
}
public String getSalary()
{
return salary;
}
// Method to create appropriate spacing
public static String mySetw(String s, int size)
{
String x = s;
for (int i = s.length(); i < size; i++)
x += " ";
return x;
}
// Method to print out data in aligned format
public String toString()
{
return mySetw(getMonth(), 15) + mySetw(getDay(), 15) + mySetw(getYear(), 15) + mySetw(getName(), 15) + mySetw(getAddy(), 15) + mySetw(getNumber(), 15) + mySetw(getSSN(), 15) + mySetw(getAge(), 15) + mySetw(getSex(), 15) + mySetw(getEname(), 15) + mySetw(getEaddy(), 15) + mySetw(getEnumber(), 15) + mySetw(getNyear(), 15) + mySetw(getTitle(), 15) + mySetw(getSalary(), 15);
}
}
Print.java
/* Todd Riethmiller
* January 19th 2009
* Data Structures
* Professor Kenison
* Display the data from Employment.java
*/
package Employee;
import java.io.*;
public class Print
{
public static void main(String[] args) throws IOException
{
// Create column headers for the data being read in
Employment test = new Employment();
System.out.println("Begin Month: " + "Begin Day: " + "Begin Year: " + "Name: " + "Address: " + "Phone #: " + "SSN: " + "Age: " + "Sex: " + "Employer Name: " + "Employer Address: " + "Employer Phone #: " + "Years of Employment: " + "Title: " + "Salary: ");
System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
test.readEmploy();
}
}
Here is the text file contents
I'm using the code to just make it visually nicer...
13 12 3 2006 JOHN SMITH 132 ELM STREET 605-932-6541 002456721 23 M GILLETTE RAZOR 123 ANDOVER AVE 617-884-0987 2 RAZOR TESTER 15600 11 10 2004 MARY JONES 32 BAY AVE. 617-543-4421 004234562 25 F CITY OF MANCHESTER 432 ELM ST. 603-622-1717 3 JANITOR 14500 1 23 1987 TOM KELLEY 11 HIGH ST. 603-892-3317 021345654 43 M JONES TRUCKING 487 LOWELL ST. 508-989-6543 21 TRUCK DRIVER 25900 1 32 1990 SUSAN DESPARATE 321 KINGS HGY. 603-421-9865 165437689 41 F KELLY GIRLS 23 WORKING WAY 603-896-6655 18 SECRETARY 15000 2 12 1998 BILLY WORKER 63 RAND AVE 603-555-2342 432765431 37 M KELLY GIRLS 23 WORKING WAY 603-896-6655 10 SECRETARY 14500 1 4 1983 TESS TRUEHEART 952 WINSTON BLVD. 617-543-7781 009645439 55 F DAILY PLANET 4567 NEWSPAPER AVE. 414-506-9876 25 REPORTER 78000 2 15 1963 MAXWELL SMART 1 CHOAS AVE 904-326-6741 007007007 70 M FBI 231 HIDDEN LANE 800-700-7007 45 SECRET AGENT 70070 1 26 2006 FRED SAVAGE 2 WONDER WAY 421-654-8767 001234567 13 M DAILY PLANET 4567 NEWSPAPER AVE. 414-506-9876 2 NEWSPAPER BOY 2006 12 7 1970 DEAN SMITH 2 NCAA WAY 508-672-0091 020365768 59 M UNC DEAN SMITH ARENA 508-672-0029 35 BASKETBALL 98000 2 14 1979 TARK SHARK 1 NCAA WAY 919-825-5438 098763231 61 M UNLV 1 RATING AVE 919-825-5444 29 BASKETBALL 99999 2 11 1993 CONNIE CHUNG 3 MUMBLE WAY 826-541-2311 006754324 38 F NBC STUDIO CENTER 826-541-0098 12 REPORTER 75000 3 11 1996 BARBARA BUSH 1 MOTHERLY WAY 909-946-8765 004532344 57 F MEALS ON WHEELS 2 POVERTY LANE 909-946-8854 12 VOLUNTEER 0 1 30 2004 GEORGE H. BUSH 1 MOTHERLY WAY 909-946-8765 004532343 59 M MEALS ON WHEELS 2 POVERTY LANE 909-946-8854 4 PAID FUNDRAISER 30000
I know this is a lot, but I've been racking my brain trying to figure out why this isn't working. I've done similar programs, but with less attributes per person. I really hope someone can help me out with this. Thanks!
Red

New Topic/Question
Reply




MultiQuote





|