473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exception in thread "main" java.lang.NullPointerException

122 100+
Exception in thread "main" java.lang.NullPointerException
at company.Employee.inputEmployee(Employee.java:172)
at company.Employee.main(Employee.java:267)
which I got before...
Can someone please help me.
This is where the error occures line 172
for (Employee_listing e : employees) {
String emp = e.getEmpId();

line 267
if (choice == 1) { // if 1 is select go to makePerson
emp.inputEmployee();
Expand|Select|Wrap|Line Numbers
  1. Here are my 
  2.  
  3.  
  4. /**
  5.      * Constructors.
  6.      */
  7.  
  8.     public Employee_listing(String id, String fn, String ln, String st,
  9.             String ct, String zp, Double pr, Integer yw) {
  10.         empid = id;
  11.         fname = fn;
  12.         lname = ln;
  13.         street = st;
  14.         city = ct;
  15.         zip = zp;
  16.         yearsworked = yw;
  17.         payrate = pr;
  18.     }// close public EmployeeClass
  19.  
  20.     /*
  21.      * accessors make info available
  22.      */
  23.     public String getEmpId() {
  24.         return empid;
  25.     }
  26.  
  27.     public String getFname() {
  28.         return fname;
  29.     }
  30.  
  31.     public String getLname() {
  32.         return lname;
  33.     }
  34.  
  35.     public String getStreet() {
  36.         return street;
  37.     }
  38.  
  39.     public String getCity() {
  40.         return city;
  41.     }
  42.  
  43.     public String getZip() {
  44.         return zip;
  45.     }
  46.  
  47.     public Integer getYearsworked() {
  48.         return yearsworked;
  49.     }
  50.  
  51.     public Double getPayrate() {
  52.         return payrate;
  53.     }
  54.  
  55.  
  56. public void inputEmployee() {
  57.  
  58.             Employee_listing MyEmployee;
  59.  
  60.             // prompt for data
  61.  
  62.             Scanner input_flag = new Scanner(System.in);
  63.             System.out.println("Enter Employee Id Number AB1234==>");
  64.             String empid = input_flag.next();
  65.             String id_flag = "";
  66.             boolean notfound = true;
  67.             for (Employee_listing e : employees) {
  68.                 String emp = e.getEmpId();
  69.                 if (emp.equals(id_flag)) {
  70.                     System.out.println("Hello" + ("e.lname"));
  71.                     notfound = false;
  72.                 }
  73.             }
  74.             if (notfound == true) {
  75. .....
  76. }
  77.         public static void main(String[] args) {
  78.             Employee emp = new Employee();
  79.             Scanner kbd = new Scanner(System.in);
  80.             int choice;
  81.             System.out.println("Make a Section: ");
  82.             System.out.println("1. Enter ");
  83.             System.out.println("2. Find ");
  84.             System.out.println("3. Exit ");
  85.             System.out.print("\nPlease press Enter afer each response");
  86.             System.out.println("Enter your chose please: ");
  87.             choice = kbd.nextInt();
  88.             kbd.nextLine();
  89.             if (choice == 1) { // if 1 is select go to makePerson
  90.                 emp.inputEmployee();
  91. ....
  92. }
  93.  
thanks
sandyw
Apr 19 '07 #1
12 6654
r035198x
13,262 8TB
Exception in thread "main" java.lang.NullPointerException
at company.Employee.inputEmployee(Employee.java:172)
at company.Employee.main(Employee.java:267)
which I got before...
Can someone please help me.
This is where the error occures line 172
for (Employee_listing e : employees) {
String emp = e.getEmpId();

line 267
if (choice == 1) { // if 1 is select go to makePerson
emp.inputEmployee();
Expand|Select|Wrap|Line Numbers
  1. Here are my 
  2.  
  3.  
  4. /**
  5.      * Constructors.
  6.      */
  7.  
  8.     public Employee_listing(String id, String fn, String ln, String st,
  9.             String ct, String zp, Double pr, Integer yw) {
  10.         empid = id;
  11.         fname = fn;
  12.         lname = ln;
  13.         street = st;
  14.         city = ct;
  15.         zip = zp;
  16.         yearsworked = yw;
  17.         payrate = pr;
  18.     }// close public EmployeeClass
  19.  
  20.     /*
  21.      * accessors make info available
  22.      */
  23.     public String getEmpId() {
  24.         return empid;
  25.     }
  26.  
  27.     public String getFname() {
  28.         return fname;
  29.     }
  30.  
  31.     public String getLname() {
  32.         return lname;
  33.     }
  34.  
  35.     public String getStreet() {
  36.         return street;
  37.     }
  38.  
  39.     public String getCity() {
  40.         return city;
  41.     }
  42.  
  43.     public String getZip() {
  44.         return zip;
  45.     }
  46.  
  47.     public Integer getYearsworked() {
  48.         return yearsworked;
  49.     }
  50.  
  51.     public Double getPayrate() {
  52.         return payrate;
  53.     }
  54.  
  55.  
  56. public void inputEmployee() {
  57.  
  58.             Employee_listing MyEmployee;
  59.  
  60.             // prompt for data
  61.  
  62.             Scanner input_flag = new Scanner(System.in);
  63.             System.out.println("Enter Employee Id Number AB1234==>");
  64.             String empid = input_flag.next();
  65.             String id_flag = "";
  66.             boolean notfound = true;
  67.             for (Employee_listing e : employees) {
  68.                 String emp = e.getEmpId();
  69.                 if (emp.equals(id_flag)) {
  70.                     System.out.println("Hello" + ("e.lname"));
  71.                     notfound = false;
  72.                 }
  73.             }
  74.             if (notfound == true) {
  75. .....
  76. }
  77.         public static void main(String[] args) {
  78.             Employee emp = new Employee();
  79.             Scanner kbd = new Scanner(System.in);
  80.             int choice;
  81.             System.out.println("Make a Section: ");
  82.             System.out.println("1. Enter ");
  83.             System.out.println("2. Find ");
  84.             System.out.println("3. Exit ");
  85.             System.out.print("\nPlease press Enter afer each response");
  86.             System.out.println("Enter your chose please: ");
  87.             choice = kbd.nextInt();
  88.             kbd.nextLine();
  89.             if (choice == 1) { // if 1 is select go to makePerson
  90.                 emp.inputEmployee();
  91. ....
  92. }
  93.  
thanks
sandyw
Where have you declared and initialized the employees collection/array?
Apr 19 '07 #2
JosAH
11,448 Expert 8TB
Nowhere in your code can I see that 'employees' is defined, nor is it visible
where/if it is initialized. By guess is that it is null, hence the Exception.

kind regards,

Jos
Apr 19 '07 #3
sandyw
122 100+
Where have you declared and initialized the employees collection/array?

public class Employee {

public ArrayList<Employee_listing> employees;// ArrayList of
}
Apr 19 '07 #4
sandyw
122 100+
Nowhere in your code can I see that 'employees' is defined, nor is it visible
where/if it is initialized. By guess is that it is null, hence the Exception.

kind regards,

Jos
How do I get it out of the null?

sandyw
Apr 19 '07 #5
r035198x
13,262 8TB
How do I get it out of the null?

sandyw
Alright. Look at it this way. You want to go over each of the employees in the employees arraylist. Now where did you add the employees?
Apr 19 '07 #6
sandyw
122 100+
Alright. Look at it this way. You want to go over each of the employees in the employees arraylist. Now where did you add the employees?
No sure this is right.
Expand|Select|Wrap|Line Numbers
  1. public void createEmployeeDB() {
  2.             employees = new ArrayList<Employee_listing>();
  3.             Employee_listing temp = new Employee_listing("DW12341");
  4.             employees.add(temp);
  5.             temp.setFname("Damon");
  6.             temp.setLname("Wong");
  7.             temp.setStreet("12345 My St");
  8.             temp.setCity("La Habra");
  9.             temp.setZip("90631");
  10.             temp.setYW(2);
  11.             temp.setPR(5000.00);
sandyw
Apr 19 '07 #7
r035198x
13,262 8TB
No sure this is right.
Expand|Select|Wrap|Line Numbers
  1. public void createEmployeeDB() {
  2.             employees = new ArrayList<Employee_listing>();
  3.             Employee_listing temp = new Employee_listing("DW12341");
  4.             employees.add(temp);
  5.             temp.setFname("Damon");
  6.             temp.setLname("Wong");
  7.             temp.setStreet("12345 My St");
  8.             temp.setCity("La Habra");
  9.             temp.setZip("90631");
  10.             temp.setYW(2);
  11.             temp.setPR(5000.00);
sandyw
1.) You need to set the values first before you add to the emloyees list.
2.) You need to call the createEmployeeDB method first in your program before you start getting employees from the employees list.

It is generally a good idea if you are still starting to write down your program flow first before writing the code.
Apr 19 '07 #8
sandyw
122 100+
1.) You need to set the values first before you add to the emloyees list.
2.) You need to call the createEmployeeDB method first in your program before you start getting employees from the employees list.

It is generally a good idea if you are still starting to write down your program flow first before writing the code.

is this right first part right?
Expand|Select|Wrap|Line Numbers
  1.         public void createEmployeeDB() {
  2.             employees = new ArrayList<Employee_listing>();
  3.             Employee_listing temp = new Employee_listing("DW12341");
  4.             temp.setFname("Damon");
  5.             temp.setLname("Wong");
  6.             temp.setStreet("12345 My St");
  7.             temp.setCity("La Habra");
  8.             temp.setZip("90631");
  9.             temp.setYW(2);
  10.             temp.setPR(5000.00);
  11.             employees.add(temp);
  12.         }
Apr 19 '07 #9
r035198x
13,262 8TB
is this right first part right?
Expand|Select|Wrap|Line Numbers
  1.         public void createEmployeeDB() {
  2.             employees = new ArrayList<Employee_listing>();
  3.             Employee_listing temp = new Employee_listing("DW12341");
  4.             temp.setFname("Damon");
  5.             temp.setLname("Wong");
  6.             temp.setStreet("12345 My St");
  7.             temp.setCity("La Habra");
  8.             temp.setZip("90631");
  9.             temp.setYW(2);
  10.             temp.setPR(5000.00);
  11.             employees.add(temp);
  12.         }
Looks better. That will create the employees list, create one employee and add that employee to the employees list.
Apr 20 '07 #10
sandyw
122 100+
OK...
I still can not figure out this Exception in thread "main" java.lang.NullPointerException

so if someone has the time to help me
or tell me how to fix it by example that would be great.
here is my complete code.

Expand|Select|Wrap|Line Numbers
  1. package company;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6.  *
  7.  * @author  ** 
  8.  
  9.  */
  10.  
  11. /**
  12.  * This program will maintain Employee Data for a company. The user will be able
  13.  * to enter new employee data and search on existing employee data. This program
  14.  * will use Arrays or an ArrayList to maintain the information. You will create
  15.  * an Employee Class and from this class create Employee Objects. These objects
  16.  * will be stored in an Array or ArrayList in such a way that they can be
  17.  * searched by the user.
  18.  */
  19.  
  20. class Employee_listing {
  21.  
  22.     private String empid;
  23.  
  24.     private String lname;
  25.  
  26.     private String fname;
  27.  
  28.     private String street;
  29.  
  30.     private String city;
  31.  
  32.     private String zip;
  33.  
  34.     private Integer yearsworked;
  35.  
  36.     private Double payrate;
  37.  
  38.     public Employee_listing(String id) {
  39.         empid = id;
  40.     }// close the employeeclass loop
  41.  
  42.     /**
  43.      * Constructors.
  44.      */
  45.  
  46.     public Employee_listing(String id, String fn, String ln, String st,
  47.             String ct, String zp, Double pr, Integer yw) {
  48.         empid = id;
  49.         fname = fn;
  50.         lname = ln;
  51.         street = st;
  52.         city = ct;
  53.         zip = zp;
  54.         yearsworked = yw;
  55.         payrate = pr;
  56.     }// close public EmployeeClass
  57.  
  58.     /*
  59.      * accessors make info available
  60.      */
  61.     public String getEmpId() {
  62.         return empid;
  63.     }
  64.  
  65.     public String getFname() {
  66.         return fname;
  67.     }
  68.  
  69.     public String getLname() {
  70.         return lname;
  71.     }
  72.  
  73.     public String getStreet() {
  74.         return street;
  75.     }
  76.  
  77.     public String getCity() {
  78.         return city;
  79.     }
  80.  
  81.     public String getZip() {
  82.         return zip;
  83.     }
  84.  
  85.     public Integer getYearsworked() {
  86.         return yearsworked;
  87.     }
  88.  
  89.     public Double getPayrate() {
  90.         return payrate;
  91.     }
  92.  
  93.     /*
  94.      * Mutator Methods
  95.      */
  96.     public void setEmpId(String id) {
  97.         empid = id;
  98.     }
  99.  
  100.     public void setFname(String fn) {
  101.         fname = fn;
  102.     }
  103.  
  104.     public void setLname(String ln) {
  105.         lname = ln;
  106.     }
  107.  
  108.     public void setStreet(String st) {
  109.         street = st;
  110.     }
  111.  
  112.     public void setCity(String ct) {
  113.         city = ct;
  114.     }
  115.  
  116.     public void setZip(String zp) {
  117.         zip = zp;
  118.     }
  119.  
  120.     public void setYW(Integer yw) {
  121.         yearsworked = yw;
  122.     }
  123.  
  124.     public void setPR(Double pr) {
  125.         payrate = pr;
  126.     }
  127. }
  128.     /**
  129.      * The EmployeeDatabase class stores each Employee in an arraylist. Methods
  130.      * exist to add new Employees, search Employees, and print Employees to the
  131.      * console.
  132.      */
  133.     public class Employee {
  134.  
  135.         public ArrayList<Employee_listing> employees;// ArrayList of
  136.  
  137.         // contact
  138.  
  139.  
  140.  
  141.         public void createEmployeeDB() {
  142.             employees = new ArrayList<Employee_listing>();
  143.             Employee_listing temp = new Employee_listing("DW12341");
  144.             temp.setFname("Sandy");
  145.             temp.setLname("Wong");
  146.             temp.setStreet("12345 My St");
  147.             temp.setCity("Fullerton");
  148.             temp.setZip("90631");
  149.             temp.setYW(2);
  150.             temp.setPR(5000.00);
  151.             employees.add(temp);
  152.         }
  153.  
  154.         /**
  155.          * displayMatch inputs a keyword from the user. It then iterates through
  156.          * the ArrayList of Employees and outputs each one to the screen if the
  157.          * Employee information contains the keyword.
  158.          */
  159.  
  160.         public void inputEmployee() {
  161.  
  162.             Employee_listing MyEmployee;
  163.  
  164.             // prompt for data
  165.  
  166.             Scanner input_flag = new Scanner(System.in);
  167.             System.out.println("Enter Employee Id Number AB1234==>");
  168.             String empid = input_flag.next();
  169.  
  170.             boolean notfound = true;
  171.             for (Employee_listing e : employees) {
  172.                 System.out.println(" a"+e.getLname());
  173.                 String emp = e.getEmpId();
  174.  
  175.                 if (emp.equals(empid)) {
  176.                     System.out.println("Hello" + ("e.lname"));
  177.                     notfound = false;
  178.                 }
  179.             }
  180.             if (notfound == true) {
  181.  
  182.             System.out.print("Enter Last Name Doe==>");
  183.             String ln = input_flag.next();
  184.  
  185.             System.out.print("Enter First Name John==>");
  186.             String fn = input_flag.next();
  187.  
  188.             System.out.print("Enter Street ==>");
  189.             String st = input_flag.next();
  190.  
  191.             System.out.print("Enter City ==>");
  192.             String ct = input_flag.next();
  193.  
  194.             System.out.print("Enter Zip ==>");
  195.             String zp = input_flag.next();
  196.  
  197.             System.out.print("Enter years worked ==>");
  198.             Integer yw = input_flag.nextInt();
  199.  
  200.             System.out.print("Enter payrate as double ==>");
  201.             Double pr = input_flag.nextDouble();
  202.  
  203.             // make an object
  204.             MyEmployee = new Employee_listing(empid, ln, fn, st, ct, zp, pr, yw);
  205.             employees.add(MyEmployee);
  206.  
  207.             System.out.println("The are this many Employee in the System:");
  208.             System.out.println(employees.size());
  209.             }
  210.             // still need to add a counter
  211.  
  212.         }// close the displayMatch
  213.  
  214.         /**
  215.          * displayMatch inputs a keyword from the user. It then iterates through
  216.          * the ArrayList of Employees and outputs each one to the screen if the
  217.          * Employee information contains the keyword.
  218.          * @return
  219.          */
  220.         public void displayMatch() {
  221.  
  222.             Scanner input_flag = new Scanner(System.in);
  223.             System.out.println("Enter Employee Id Number AB1234==>");
  224.             String empid = input_flag.next();
  225.             boolean notfound = true;
  226.             for (Employee_listing e : employees) {
  227.                 String emp = e.getEmpId();
  228.                 if (emp.equals(empid)) {
  229.                     System.out.println("You need to go back to Main Menu");
  230.                     notfound = false;
  231.                 }
  232.             }
  233.             if (notfound == true) {
  234.  
  235.             for (Employee_listing e : employees) {
  236.                 System.out.printf("Employee Id: %s \n", e.getEmpId());
  237.                 System.out.printf("Name is %s - %s \n", e.getFname(), e
  238.                         .getLname());
  239.                 System.out.printf("Address is %s  \n", e.getStreet());
  240.                 System.out.printf("City is %s \n", e.getCity());
  241.                 System.out.printf("Zip is %s  \n", e.getZip());
  242.                 System.out.printf("Pay is %s  \n", e.getPayrate());
  243.                 System.out.printf("Years worked are %d\n", e.getYearsworked());
  244.                 System.out.println("--------------------");
  245.             }
  246.             }
  247.         }// close the displayMatch
  248.  
  249.         /**
  250.          * @param Add
  251.          *            a employee to the db. using a scanner to see if an
  252.          *            Employee is in the db if no add them
  253.          */
  254.  
  255.         public static void main(String[] args) {
  256.             Employee emp = new Employee();
  257.             Scanner kbd = new Scanner(System.in);
  258.             int choice;
  259.             System.out.println("Make a Section: ");
  260.             System.out.println("1. Enter ");
  261.             System.out.println("2. Find ");
  262.             System.out.println("3. Exit ");
  263.             System.out.print("\nPlease press Enter afer each response");
  264.             System.out.println("Enter your chose please: ");
  265.             choice = kbd.nextInt();
  266.             kbd.nextLine();
  267.             if (choice == 1) { // if 1 is select go to makePerson
  268.                 emp.inputEmployee();
  269.  
  270.             } // close the if loop
  271.  
  272.             if (choice == 2) { // if 2 is select go to find
  273.                 emp.displayMatch();
  274.  
  275.             }// close the choice==2
  276.             if (choice == 3) {
  277.                 System.out.printf("Good bye");
  278.             }// close the choice == 3
  279.  
  280.         }// close public static void
  281.  
  282.     }// close public class Employee
Sandyw
so near yet so far away.
Apr 20 '07 #11
sandyw
122 100+
I still need help see above
Apr 25 '07 #12
r035198x
13,262 8TB
I still need help see above
Look at your program flow again from the main method. When do you initialize the employees list? You should find that you are trying to access the list before it is initialized.
Apr 26 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's...
1
by: Andy Howells | last post by:
Can anybody help me on this? I am getting the below error but have not got a clue why. The file in my classpath eing used has the class that it says is not defined. Any ideas? I am running java...
9
by: jith87 | last post by:
What is ths error.????? I m nw loading the JDBC driver for connection with MySql. Exception in thread "main" java.lang.NoClassDefFoundError: MySQLJDBCDriverTest
1
by: GHKASHYAP | last post by:
Hi this is the exception i am getting when i am trying to run this application: Exception in thread "main" java.lang.NullPointerException thanks in advance.. package...
3
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
9
by: tiyaramunna | last post by:
I am trying to configure my system with Java program just to practice on the coding....when i compile a test.java program i am able to see the class file but i cant run the program ... I am getting...
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
1
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
3
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at...
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.