7 Replies Latest reply: Apr 19, 2007 10:34 AM by 807599 RSS

    Exception in thread "main" java.lang.NullPointerException

    807599
      Hello everyone.
      I need some help I have an error message and I hope someone can help me.

      Exception in thread "main" java.lang.NullPointerException
           at company.Employee.inputEmployee(Employee.java:174)
           at company.Employee.main(Employee.java:270)

      Here is part of my code where the problem is occurring.
      /**
            * 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
      
                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) {
                          String emp = e.getEmpId();
      
                          if (emp.equals(empid)) {
                               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();
      
                     } // close the if loo
      ....
      }
      Thanks
      SandyR
        • 1. Re: Exception in thread "main" java.lang.NullPointerException
          807599
          My wild guess is that the employees variable is either never instantiated, or it's elements are never instantiated. How about pointing out which line is 174 in your file?
          • 2. Re: Exception in thread "main" java.lang.NullPointerException
            807599
            Sorry about that one.
            the first one is

            line 174
            for (Employee_listing e : employees) {
                 String emp = e.getEmpId();
            and
            public static void main(String[] args) {
            .....line 270
            if (choice == 1) { // if 1 is select go to makePerson
                                emp.inputEmployee();
            thanks
            sandyR
            • 3. Re: Exception in thread "main" java.lang.NullPointerException
              807599
              It looks to me that one or more of your entries in the
              employees
              (array?) is null. This could have been set as null, or set at one point, then deleted later. Hmm...

              I've had no luck with Java's take on the "For Each..." loop. I would try something like:
              for(int i = 0; i < employees.length; ++i) {
                Employee e = employees.get(i);
                if(e != null) {
                  // Your code here...
                } else {
                  System.out.println("Yep, that was the problem...");
                }
              }
              Hope that helps.
              • 4. Re: Exception in thread "main" java.lang.NullPointerException
                807599
                It looks to me that one or more of your entries in
                the
                employees
                (array?) is null.
                Or perhaps employees is itself null. I don't see this being assigned to anywhere (or declared, but I'll assume it is since you made it past compilation and on to runtime errors).

                Message was edited by:
                bheilers
                • 5. Re: Exception in thread "main" java.lang.NullPointerException
                  807599
                  No to sure where you want me to add the code you listed above.

                  I tried it after boolean notfound = true;
                  and I got some error messages on
                  for(int i = 0; i < employees.length; ++i) {
                       Employee e = employees.get(i);
                        if(e != null) {
                  Both were error said
                  employees.length cannot be resolved or is not a field
                  employees.length cannot be resolved or is not a field

                  Thanks.
                  Sandyr
                  Is there away I can email you my project?
                  PS I don't want other in my class to see my project if they are look and getting help.
                  • 6. Re: Exception in thread "main" java.lang.NullPointerException
                    807599
                    No to sure where you want me to add the code you
                    listed above.

                    I tried it after boolean notfound = true;
                    and I got some error messages on
                    for(int i = 0; i < employees.length; ++i) {
                         Employee e = employees.get(i);
                          if(e != null) {
                    Both were error said
                    employees.length cannot be resolved or is not a
                    field
                    employees.length cannot be resolved or is not a
                    field

                    Thanks.
                    Sandyr
                    Is there away I can email you my project?
                    PS I don't want other in my class to see my project
                    if they are look and getting help.
                    why not? if you're entitled to cheat and get the forum to do this for you, why aren't they?
                    • 7. Re: Exception in thread "main" java.lang.NullPointerException
                      807599
                      Because I did most of the work. I'm stuck at this point.
                      Why should I do all the work....
                      but then again the Professor will probaby know anyways if it theirs or not. So I quess who cares then.
                      sandyr