1
//TestEmployeesProgram driver with menu & object array.
import java.util.*;
public class TestEmployeesProgram {

public static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
    final int MAX = 7;

    Employee employee[] = new Employee[MAX];

    int choice,k;
    String name;
    boolean notFound;

    employee[1] = new Manager("Joe Bloggs","gdr",4,32.5);
    employee[2] = new Admin("Mary Jennings","nnv",35.3,88.5,34.3);
    employee[3] = new Clerk("Brian Jones","bbl",42.4,78.5,23.5,45.3);
    employee[4] = new Manager("John Bloggs","gvr",5,33.5);
    employee[5] = new Admin("Bridget Jennings","nvv",45.3,98.5,36.3);
    employee[6] = new Clerk("Jack Jones","bbb",43.4,78.5,23.5,47.3);


    //Initial Read
    choice = showMenu();

    //Continue Until 4/Exit
    while (choice != MAX)
    {
        switch (choice)
        {
        case 1://Manager

            System.out.println();
            System.out.printf("%s %-16s %-10s %6s","Name","Id","Hours Worked","Pay");
            System.out.println("\n==================================================");

            for (k = 0; k < MAX; ++k)
            {
                if (employee[k] instanceof Manager){ //use of string method instance of.


                    System.out.println(employee[k].toString());
                }
            }
            break;

        case 2://Administration

            System.out.println();
            System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota");
            System.out.println("\n==================================================");

            for (k = 0; k < MAX; ++k)
            {
                if (employee[k] instanceof Admin){
                System.out.println(employee[k].toString());

                }
            }
            break;

        case 3://Clerk

            System.out.println();
            System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold");
            System.out.println("\n==================================================");

            for (k = 0; k < MAX; ++k)
            {
                if (employee[k] instanceof Clerk){
                System.out.println(employee[k].toString());
                }
            }
            break;

switch 语句中的这种情况给了我NullPointerException. 我已经检查了所有内容以查看它是否已正确初始化,但似乎无法找到问题所在。此外,如果未找到名称,搜索功能会绕过名称搜索以获取默认消息。一些指导将不胜感激。

        case 4://Name search

            System.out.print("Enter employee name: ");
            name = console.nextLine();

            k = -1;
            notFound = true;



            while ((k < MAX-1) && (notFound))
            {
                ++k;
                if (name == employee[k].getName()){

                    System.out.println();
                    System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold");
                    System.out.println("\n==================================================");

                    System.out.println(employee[k].toString());
                    System.out.println();
                    notFound = false;
                }


            }//end of case 4 while.
             if (notFound){
                System.out.println("Employee name not found\n");
            }
            break;

        case 7://exit
            System.out.println("Program exiting...");
            System.exit(0);

        default:
            System.out.println("Invalid menu choice 1..3 of 7 to Exit");    
        }//end of switch

        //sub read 
        choice = showMenu();

    }//end of while 
}//end of main

//Menu method for employee selection.
public static int showMenu()
{

    int choice;
    System.out.println();

    System.out.println("Employee Program Menu");

    System.out.println("1.Show Manager pay details ");
    System.out.println("2.Show Admin pay details ");
    System.out.println("3.Show Clerk pay details ");
    System.out.println("4.Search by employee name ");
    System.out.println("7.Exit");

    System.out.print("Enter option: ");
    choice = console.nextInt();

    return choice;
}
}
4

3 回答 3

3

您没有在0th索引处设置任何值,因此if (name == employee[0].getName()){}在尝试访问时生成异常employee[0].getName()

您应该将值设置为索引 0,因为数组以索引 0 开始并以大小 -1 结束...

employee[0] = new Manager("Joe Bloggs","gdr",4,32.5);
employee[1] = new Admin("Mary Jennings","nnv",35.3,88.5,34.3);
...
employee[6] = new Clerk("Jack Jones","bbb",43.4,78.5,23.5,47.3)
于 2012-11-16T17:49:10.410 回答
0

在 Java 中可能会变形,但是,您是否忘记将新 Employee 设置为 employee[0] ?

因为在案例 4 中,您从 k 等于 -1 开始,然后将其递增到 0,但是您在employee[0] 处根本没有 Employee....

于 2012-11-16T17:51:28.673 回答
0

编辑再看一遍后,你到处都这样做。因此,请检查您的代码,并确保您的数组已正确初始化。您也可以尝试为您的 switch 语句使用命名常量,我发现它们有助于提高可读性(如果您是像我这样的学生,不会因为幻数而失去分数)。

    final int MANAGER = 1;

    final int CLERK = 2; 

    switch(choice)
    {
          case MANAGER: foo;
              break;
           case CLERK: foo;
              break;
    }

这是您的问题代码。当控制流到达 if 语句时,k 等于 0,并且你的数组中没有第 0 个元素,所以 java 抛出空指针异常。

另外为了将来参考,请尝试查看编译器给您的实际错误。编译器会为您提供一个行号,这应该可以让您很容易地追踪错误。当涉及到空指针时,Java 对你来说真的很好,c++ 将继续运行,使用它在错误位置找到的任何值。

        k = -1;
        notFound = true;



        while ((k < MAX-1) && (notFound))
        {
            ++k;
            if (name == employee[k].getName())
于 2012-11-16T17:55:49.427 回答