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(); -
Here are my
-
-
-
/**
-
* Constructors.
-
*/
-
-
public Employee_listing(String id, String fn, String ln, String st,
-
String ct, String zp, Double pr, Integer yw) {
-
empid = id;
-
fname = fn;
-
lname = ln;
-
street = st;
-
city = ct;
-
zip = zp;
-
yearsworked = yw;
-
payrate = pr;
-
}// close public EmployeeClass
-
-
/*
-
* accessors make info available
-
*/
-
public String getEmpId() {
-
return empid;
-
}
-
-
public String getFname() {
-
return fname;
-
}
-
-
public String getLname() {
-
return lname;
-
}
-
-
public String getStreet() {
-
return street;
-
}
-
-
public String getCity() {
-
return city;
-
}
-
-
public String getZip() {
-
return zip;
-
}
-
-
public Integer getYearsworked() {
-
return yearsworked;
-
}
-
-
public Double getPayrate() {
-
return payrate;
-
}
-
-
-
public void inputEmployee() {
-
-
Employee_listing MyEmployee;
-
-
// prompt for data
-
-
Scanner input_flag = new Scanner(System.in);
-
System.out.println("Enter Employee Id Number AB1234==>");
-
String empid = input_flag.next();
-
String id_flag = "";
-
boolean notfound = true;
-
for (Employee_listing e : employees) {
-
String emp = e.getEmpId();
-
if (emp.equals(id_flag)) {
-
System.out.println("Hello" + ("e.lname"));
-
notfound = false;
-
}
-
}
-
if (notfound == true) {
-
.....
-
}
-
public static void main(String[] args) {
-
Employee emp = new Employee();
-
Scanner kbd = new Scanner(System.in);
-
int choice;
-
System.out.println("Make a Section: ");
-
System.out.println("1. Enter ");
-
System.out.println("2. Find ");
-
System.out.println("3. Exit ");
-
System.out.print("\nPlease press Enter afer each response");
-
System.out.println("Enter your chose please: ");
-
choice = kbd.nextInt();
-
kbd.nextLine();
-
if (choice == 1) { // if 1 is select go to makePerson
-
emp.inputEmployee();
-
....
-
}
-
thanks
sandyw
12 6654
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(); -
Here are my
-
-
-
/**
-
* Constructors.
-
*/
-
-
public Employee_listing(String id, String fn, String ln, String st,
-
String ct, String zp, Double pr, Integer yw) {
-
empid = id;
-
fname = fn;
-
lname = ln;
-
street = st;
-
city = ct;
-
zip = zp;
-
yearsworked = yw;
-
payrate = pr;
-
}// close public EmployeeClass
-
-
/*
-
* accessors make info available
-
*/
-
public String getEmpId() {
-
return empid;
-
}
-
-
public String getFname() {
-
return fname;
-
}
-
-
public String getLname() {
-
return lname;
-
}
-
-
public String getStreet() {
-
return street;
-
}
-
-
public String getCity() {
-
return city;
-
}
-
-
public String getZip() {
-
return zip;
-
}
-
-
public Integer getYearsworked() {
-
return yearsworked;
-
}
-
-
public Double getPayrate() {
-
return payrate;
-
}
-
-
-
public void inputEmployee() {
-
-
Employee_listing MyEmployee;
-
-
// prompt for data
-
-
Scanner input_flag = new Scanner(System.in);
-
System.out.println("Enter Employee Id Number AB1234==>");
-
String empid = input_flag.next();
-
String id_flag = "";
-
boolean notfound = true;
-
for (Employee_listing e : employees) {
-
String emp = e.getEmpId();
-
if (emp.equals(id_flag)) {
-
System.out.println("Hello" + ("e.lname"));
-
notfound = false;
-
}
-
}
-
if (notfound == true) {
-
.....
-
}
-
public static void main(String[] args) {
-
Employee emp = new Employee();
-
Scanner kbd = new Scanner(System.in);
-
int choice;
-
System.out.println("Make a Section: ");
-
System.out.println("1. Enter ");
-
System.out.println("2. Find ");
-
System.out.println("3. Exit ");
-
System.out.print("\nPlease press Enter afer each response");
-
System.out.println("Enter your chose please: ");
-
choice = kbd.nextInt();
-
kbd.nextLine();
-
if (choice == 1) { // if 1 is select go to makePerson
-
emp.inputEmployee();
-
....
-
}
-
thanks
sandyw
Where have you declared and initialized the employees collection/array?
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
Where have you declared and initialized the employees collection/array?
public class Employee {
public ArrayList<Employee_listing> employees;// ArrayList of
}
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
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?
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. - public void createEmployeeDB() {
-
employees = new ArrayList<Employee_listing>();
-
Employee_listing temp = new Employee_listing("DW12341");
-
employees.add(temp);
-
temp.setFname("Damon");
-
temp.setLname("Wong");
-
temp.setStreet("12345 My St");
-
temp.setCity("La Habra");
-
temp.setZip("90631");
-
temp.setYW(2);
-
temp.setPR(5000.00);
sandyw
No sure this is right. - public void createEmployeeDB() {
-
employees = new ArrayList<Employee_listing>();
-
Employee_listing temp = new Employee_listing("DW12341");
-
employees.add(temp);
-
temp.setFname("Damon");
-
temp.setLname("Wong");
-
temp.setStreet("12345 My St");
-
temp.setCity("La Habra");
-
temp.setZip("90631");
-
temp.setYW(2);
-
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.
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? - public void createEmployeeDB() {
-
employees = new ArrayList<Employee_listing>();
-
Employee_listing temp = new Employee_listing("DW12341");
-
temp.setFname("Damon");
-
temp.setLname("Wong");
-
temp.setStreet("12345 My St");
-
temp.setCity("La Habra");
-
temp.setZip("90631");
-
temp.setYW(2);
-
temp.setPR(5000.00);
-
employees.add(temp);
-
}
is this right first part right? - public void createEmployeeDB() {
-
employees = new ArrayList<Employee_listing>();
-
Employee_listing temp = new Employee_listing("DW12341");
-
temp.setFname("Damon");
-
temp.setLname("Wong");
-
temp.setStreet("12345 My St");
-
temp.setCity("La Habra");
-
temp.setZip("90631");
-
temp.setYW(2);
-
temp.setPR(5000.00);
-
employees.add(temp);
-
}
Looks better. That will create the employees list, create one employee and add that employee to the employees list.
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. - package company;
-
-
import java.util.*;
-
-
/**
-
*
-
* @author **
-
-
*/
-
-
/**
-
* This program will maintain Employee Data for a company. The user will be able
-
* to enter new employee data and search on existing employee data. This program
-
* will use Arrays or an ArrayList to maintain the information. You will create
-
* an Employee Class and from this class create Employee Objects. These objects
-
* will be stored in an Array or ArrayList in such a way that they can be
-
* searched by the user.
-
*/
-
-
class Employee_listing {
-
-
private String empid;
-
-
private String lname;
-
-
private String fname;
-
-
private String street;
-
-
private String city;
-
-
private String zip;
-
-
private Integer yearsworked;
-
-
private Double payrate;
-
-
public Employee_listing(String id) {
-
empid = id;
-
}// close the employeeclass loop
-
-
/**
-
* Constructors.
-
*/
-
-
public Employee_listing(String id, String fn, String ln, String st,
-
String ct, String zp, Double pr, Integer yw) {
-
empid = id;
-
fname = fn;
-
lname = ln;
-
street = st;
-
city = ct;
-
zip = zp;
-
yearsworked = yw;
-
payrate = pr;
-
}// close public EmployeeClass
-
-
/*
-
* accessors make info available
-
*/
-
public String getEmpId() {
-
return empid;
-
}
-
-
public String getFname() {
-
return fname;
-
}
-
-
public String getLname() {
-
return lname;
-
}
-
-
public String getStreet() {
-
return street;
-
}
-
-
public String getCity() {
-
return city;
-
}
-
-
public String getZip() {
-
return zip;
-
}
-
-
public Integer getYearsworked() {
-
return yearsworked;
-
}
-
-
public Double getPayrate() {
-
return payrate;
-
}
-
-
/*
-
* Mutator Methods
-
*/
-
public void setEmpId(String id) {
-
empid = id;
-
}
-
-
public void setFname(String fn) {
-
fname = fn;
-
}
-
-
public void setLname(String ln) {
-
lname = ln;
-
}
-
-
public void setStreet(String st) {
-
street = st;
-
}
-
-
public void setCity(String ct) {
-
city = ct;
-
}
-
-
public void setZip(String zp) {
-
zip = zp;
-
}
-
-
public void setYW(Integer yw) {
-
yearsworked = yw;
-
}
-
-
public void setPR(Double pr) {
-
payrate = pr;
-
}
-
}
-
/**
-
* The EmployeeDatabase class stores each Employee in an arraylist. Methods
-
* exist to add new Employees, search Employees, and print Employees to the
-
* console.
-
*/
-
public class Employee {
-
-
public ArrayList<Employee_listing> employees;// ArrayList of
-
-
// contact
-
-
-
-
public void createEmployeeDB() {
-
employees = new ArrayList<Employee_listing>();
-
Employee_listing temp = new Employee_listing("DW12341");
-
temp.setFname("Sandy");
-
temp.setLname("Wong");
-
temp.setStreet("12345 My St");
-
temp.setCity("Fullerton");
-
temp.setZip("90631");
-
temp.setYW(2);
-
temp.setPR(5000.00);
-
employees.add(temp);
-
}
-
-
/**
-
* displayMatch inputs a keyword from the user. It then iterates through
-
* the ArrayList of Employees and outputs each one to the screen if the
-
* Employee information contains the keyword.
-
*/
-
-
public void inputEmployee() {
-
-
Employee_listing MyEmployee;
-
-
// prompt for data
-
-
Scanner input_flag = new Scanner(System.in);
-
System.out.println("Enter Employee Id Number AB1234==>");
-
String empid = input_flag.next();
-
-
boolean notfound = true;
-
for (Employee_listing e : employees) {
-
System.out.println(" a"+e.getLname());
-
String emp = e.getEmpId();
-
-
if (emp.equals(empid)) {
-
System.out.println("Hello" + ("e.lname"));
-
notfound = false;
-
}
-
}
-
if (notfound == true) {
-
-
System.out.print("Enter Last Name Doe==>");
-
String ln = input_flag.next();
-
-
System.out.print("Enter First Name John==>");
-
String fn = input_flag.next();
-
-
System.out.print("Enter Street ==>");
-
String st = input_flag.next();
-
-
System.out.print("Enter City ==>");
-
String ct = input_flag.next();
-
-
System.out.print("Enter Zip ==>");
-
String zp = input_flag.next();
-
-
System.out.print("Enter years worked ==>");
-
Integer yw = input_flag.nextInt();
-
-
System.out.print("Enter payrate as double ==>");
-
Double pr = input_flag.nextDouble();
-
-
// make an object
-
MyEmployee = new Employee_listing(empid, ln, fn, st, ct, zp, pr, yw);
-
employees.add(MyEmployee);
-
-
System.out.println("The are this many Employee in the System:");
-
System.out.println(employees.size());
-
}
-
// still need to add a counter
-
-
}// close the displayMatch
-
-
/**
-
* displayMatch inputs a keyword from the user. It then iterates through
-
* the ArrayList of Employees and outputs each one to the screen if the
-
* Employee information contains the keyword.
-
* @return
-
*/
-
public void displayMatch() {
-
-
Scanner input_flag = new Scanner(System.in);
-
System.out.println("Enter Employee Id Number AB1234==>");
-
String empid = input_flag.next();
-
boolean notfound = true;
-
for (Employee_listing e : employees) {
-
String emp = e.getEmpId();
-
if (emp.equals(empid)) {
-
System.out.println("You need to go back to Main Menu");
-
notfound = false;
-
}
-
}
-
if (notfound == true) {
-
-
for (Employee_listing e : employees) {
-
System.out.printf("Employee Id: %s \n", e.getEmpId());
-
System.out.printf("Name is %s - %s \n", e.getFname(), e
-
.getLname());
-
System.out.printf("Address is %s \n", e.getStreet());
-
System.out.printf("City is %s \n", e.getCity());
-
System.out.printf("Zip is %s \n", e.getZip());
-
System.out.printf("Pay is %s \n", e.getPayrate());
-
System.out.printf("Years worked are %d\n", e.getYearsworked());
-
System.out.println("--------------------");
-
}
-
}
-
}// close the displayMatch
-
-
/**
-
* @param Add
-
* a employee to the db. using a scanner to see if an
-
* Employee is in the db if no add them
-
*/
-
-
public static void main(String[] args) {
-
Employee emp = new Employee();
-
Scanner kbd = new Scanner(System.in);
-
int choice;
-
System.out.println("Make a Section: ");
-
System.out.println("1. Enter ");
-
System.out.println("2. Find ");
-
System.out.println("3. Exit ");
-
System.out.print("\nPlease press Enter afer each response");
-
System.out.println("Enter your chose please: ");
-
choice = kbd.nextInt();
-
kbd.nextLine();
-
if (choice == 1) { // if 1 is select go to makePerson
-
emp.inputEmployee();
-
-
} // close the if loop
-
-
if (choice == 2) { // if 2 is select go to find
-
emp.displayMatch();
-
-
}// close the choice==2
-
if (choice == 3) {
-
System.out.printf("Good bye");
-
}// close the choice == 3
-
-
}// close public static void
-
-
}// close public class Employee
Sandyw
so near yet so far away.
I still need help see above
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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;
|
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...
|
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...
|
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
*/
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |