473,770 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception in thread "main" java.lang.NullP ointerException

122 New Member
Exception in thread "main" java.lang.NullP ointerException
at company.Employe e.inputEmployee (Employee.java: 172)
at company.Employe e.main(Employee .java:267)
which I got before...
Can someone please help me.
This is where the error occures line 172
for (Employee_listi ng e : employees) {
String emp = e.getEmpId();

line 267
if (choice == 1) { // if 1 is select go to makePerson
emp.inputEmploy ee();
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 6682
r035198x
13,262 MVP
Exception in thread "main" java.lang.NullP ointerException
at company.Employe e.inputEmployee (Employee.java: 172)
at company.Employe e.main(Employee .java:267)
which I got before...
Can someone please help me.
This is where the error occures line 172
for (Employee_listi ng e : employees) {
String emp = e.getEmpId();

line 267
if (choice == 1) { // if 1 is select go to makePerson
emp.inputEmploy ee();
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 Recognized Expert MVP
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 New Member
Where have you declared and initialized the employees collection/array?

public class Employee {

public ArrayList<Emplo yee_listing> employees;// ArrayList of
}
Apr 19 '07 #4
sandyw
122 New Member
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 MVP
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 New Member
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 MVP
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 createEmployeeD B 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 New Member
1.) You need to set the values first before you add to the emloyees list.
2.) You need to call the createEmployeeD B 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 MVP
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

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

Similar topics

0
3010
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 webpage and installed it using SGI's swmgr application. The installation was very straight forward and there were no errors when I installed the package. Then I ran /usr/java2/bin/javac SelectSockets.java to make the SelectSockets.class file.
1
9122
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 version 1.4.0 and WMQ Series version 5.3 with CSD04. Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/server/MQSESSION at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:67) at...
9
3128
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
1788
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 com.inventive.StockMarketTrading; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent;
3
6444
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
3285
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 the following error Exception in thread "main" java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at java.net.URLClassLoader$1.run(Unknown Source) at...
4
14730
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 "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar" -jar CurrentStrobe.jar Exception in thread "main"...
1
13740
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
7659
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 sortmergejoin.MergeJoin.Field(MergeJoin.java:204) at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84) at sortmergejoin.MergeJoin.<init>(MergeJoin.java:34) at sortmergejoin.Main.main(Main.java:24) Java Result: 1"
1
6757
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 "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:365) at orderingsystem.OrderingSystem.<init>(OrderingSystem.java:261) at...
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.