开发者

Java - adding person to a linked list via text UI

When trying to add a person, it just says null pointer error at personList.AddPersonSort. Please help.

    import java.util.*;

    /**
    *en meny
    * @author Henning
    */

    public class Menutext
    {

    private PersonList personList;

     /**
     * mainMenu()
     * skriver ut hovedmeny
     * @param   none
     * @return  none
     */
    public void mainMenu()
    {
        System.out.println("*********************************");
        System.out.println(" Main Menu");
        System.out.println("");
        System.out.println(" 1: Add a person");
        System.out.println(" 2: Print all the persons");
        System.out.println(" 3: End Program");
        System.out.println(" Please choose an option! ");
    }    

    public void question()
    {   System.out.println("Would you like to proceed or quit?");
        System.out.println("To proceed enter 9.");
        System.out.println("If you wish to quit enter 0.");
        Scanner sc = new Scanner(System.in);
        switch (sc.nextInt())   
        {       
            case 0:     
            System.out.println ("Thank you and godbye.");
            break;      
            case 9:     
            System.out.println ("Please proceed.");
            new Menutext();
            break;
            default:
            System.err.println ( "Unrecognized option" );
            break;
        }
    }


    /**
     * Menu()
     * Starter systemet. Dette will skrive ut en velkomst beskjed og starte
     * en dialog med brukeren via en meny inntil brukeren avslutter dialogen
     * @param   none
     * @return  none
     */
    public Menutext()
    {

        Scanner sc = new Scanner ( System.in );

        mainMenu();
            switch ( sc.nextInt() )
            {
                case 1:
                  newPersonMenu();
                  //question();
                    break;

                case 2:
                    System.out.println("All the people in the list:\n");
                   personList.toString();
                   question();
                    break;



                default:
                System.out.println ( "Unrecognized option" );
                question();
                break;
            }   


开发者_StackOverflow    }

    /**
     * newPersonMenu()
     * legger til en person
     * @param   none
     * @return  none
     */
    public void newPersonMenu()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please register a new person");
        System.out.print(" Please write firstname: ");
        String  fn = sc.nextLine();

        System.out.print(" Please write surname: ");
        String  sn = sc.nextLine();

        System.out.print(" Please write birthyear: ");
        int bir = sc.nextInt();

        System.out.print(" Please write deathyear (or 0 if still alive): ");
        int det = sc.nextInt();

        //System.out.print(" You entered: " fn + " " + sn " (" + bir + "-" + det + ").");

         personList.addPersonSort(fn, sn, bir, det);


        question();
    }   



    public class PersonList extends DNodeList<Person>
    {


    /**
     * Constructor for objects of class LinkedListEvents
     */
    public PersonList()
    {
        super();
    }

     /**
     * lager en string representasjon av listen
     */

    @Override
    public String toString()
    {
        String listPerson = "";
        if (!isEmpty())
        {
            listPerson = "Persons:\n";
            DNode<Person> hRef = header.getNext();
            while (hRef != trailer)
            {
                listPerson += hRef.getIndex() + "  " + hRef.getElement().getFirstName() + "  " +    hRef.getElement().getSurname() + "(" + hRef.getElement().getbirthyear() + " - " + hRef.getElement().getdeathyear() + ")";
                hRef = hRef.getNext();
                if (hRef != trailer)
                {
                    listPerson += "\n";
                }
            }
        }
        else
        {
            listPerson = "No people registered.";
        }
        return listPerson;
    }




    /**
     * legger til en person etter fødsels år
     *
     * @param fn fornavn
     * @param sn etternavn
     * @param bir fødselsår
     * @param det året personen døde
     */
    public void addPersonSort(String fn, String sn, int bir, int det)
    {
        DNode<Person> hRef = null;
        if (isEmpty())
        {
            addLast(new Person(fn, sn, bir, det));
        }
        else
        {
            hRef = getFirst();
            while (hRef.getNext().getElement().getbirthyear() < bir && hRef.getNext() != trailer)
            {
                hRef = hRef.getNext();
            }
            addAfter(hRef, new Person(fn, sn, bir, det));
        }
    }




    /**
     *
     */
    public Person searchPerson(String fn)
    {
        return search(fn).getElement();
    }

    /**
     * søker etter hendelse
     *
     * @param desc hendelsen vi søker etter
     * @return hendelsen om den er der
     */
    public DNode<Person> search(String fn)
    {
        DNode<Person> hRef = header;
        DNode<Person> e = null;
        if (!isEmpty())
        {
            do
            {
                hRef = hRef.getNext();
            }
            while (!hRef.getElement().getFirstName().equalsIgnoreCase(fn));
            if (hRef != trailer && hRef.getElement().getFirstName().equals(fn))
            {
                e = hRef;
            }
            else
            {
                System.out.println("No such person.");
            }
        }
        return e;
    }

    /**
     * sorterer listen etter årstall
     */
    public void sortPersons()
    {
        if (size() <= 1)
        {
            return;
        }
        DNode<Person> pivot;
        DNode<Person> ins;
        DNode<Person> end = (DNode<Person>) getFirst();
        while(end != getLast())
        {
            pivot = end.getNext();
            removeElement(pivot);
            ins = end;
            while (hasPrev(ins) && ins.getElement().getbirthyear() > pivot.getElement().getbirthyear())
            {
                ins = ins.getPrev();
            }
            addAfter(ins, pivot.getElement());
            if (ins == end)
            {
                end = end.getNext();
            }
        }
        indexNodes();
    }


    }

@Override

public String toString()
    {
        String listPerson = "";
        if (!isEmpty())
        {
            listPerson = "Persons:\n";
            DNode<Person> hRef = header.getNext();
            while (hRef != trailer)
            {
                listPerson += hRef.getIndex() + "  " + hRef.getElement().getFirstName() + "  " + hRef.getElement().getSurname() + "(" + hRef.getElement().getbirthyear() + " - " + hRef.getElement().getdeathyear() + ")";
                hRef = hRef.getNext();
                if (hRef != trailer)
                {
                    listPerson += "\n";
                }
            }
        }
        else
        {
            listPerson = "No people registered.";
        }
        return listPerson;
    }

It doesn't give any people registered after I add 1 person in the text UI.


In Menutext you should initialize your personList before use.

private PersonList personList = new PersonList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜