Results 1 to 6 of 6
  1. #1
    ecorvo07 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Help with doubly circular linked list Exception in thread "main" java.lang.NullPoint

    I WAS ASKED TO DO THE FOLLOWING:
    Programming Assignment due 02/01/2011
    Review of Programming 2 Data Structures
    You must write your own data structures (linked list, stack…) necessary for this assignment. You cannot use Data Structures provided in Java API.
    What to submit:
    Zipped:
    1. Run directory with your code and output file.
    2. A file with the following statement:
    I attest that I, YOUR NAME, and no one else, had written up this assignment. I have discussed the following problems with NAMES. I have not copied code from any site other than the ones specifically allowed and references on the moodle.

    Problem: Elimination game.
    Given:
    1. List of players, each with name and elimination parameter
    2. The player to start the game.
    3. The starting list must be in alphabetical order

    Elimination parameter consists of a
    1. int nextEliminate: the position of the next player to be eliminated from the current player.
    2. string direction; F or B: forward or backword
    Elimination rules:
    1. move nextEliminate positions in the direction provided by ‘direction’
    2. you cannot run out of players, i.e. loop to the ‘first’ from the ‘last’ if moving forward, or to the ‘last’ from the ‘first’ if moving backward.
    Example:
    Starting list: (Anne, 3F), (Charlie, 2F), (Eduardo, 2B), (Philip, 5B)
    Starting player: Anne
    Elimination order: Phillip, Charlie, Anne: Winner Eduardo


    What to do:
    Read in and parse data from a file prog1Data.txt. Each line contains the name of the person and the number and direction for the next player to be eliminated.
    Since I/O is very time expensive, you may read in this file only once.
    You must use LinkedList (either Single, Double or Circular) to store players in and to play the game.
    You cannot use any sorting Java API.
    Each game starts with players being alphabetically ordered.
    Print into a file this starting order.
    Print into a file reverse starting order list.
    • Play the elimination game starting with first player.
    Print into a file order at which players were eliminated.
    Print into a file reverse order at which players were eliminated.
    • Play the elimination game starting with the last player.
    Print into a file order at which players were eliminated.
    Print into a file reverse order at which players were eliminated.
    • Remove players Bob and Grace from the list.

    • Play the elimination game starting with the first player.
    Print into a file order at which players were eliminated.
    Print into a file reverse order at which players were eliminated.


    HERE IS MY CODE:

    import java.util.ListIterator;
    import java.io.*;
    import java.util.Scanner;

    public class Game
    {
    private MyLinkedList Mainlist;
    private MyLinkedList list1;
    private MyLinkedList list2;

    public Game()
    {
    Mainlist = new MyLinkedList();
    list1 = new MyLinkedList();
    list2 = new MyLinkedList();
    try{
    Scanner scan = new Scanner( new File ( "Game.txt" ) ) ;

    while (scan.hasNext())
    {
    String text = scan.nextLine();
    Scanner parser = new Scanner(text);

    String n = parser.next();
    String direct = parser.next();
    String dir = direct.substring(1,2);
    int value = Integer.parseInt(direct.substring(0,1));

    Player p1 = new Player(n,value,dir);

    Mainlist.insertSorted(p1);
    list1.insertSorted(p1);
    }

    }catch(IOException ioe){
    //Handle exception here
    }
    this.Mainlist.printList("File.txt");
    this.Mainlist.printRevList("File.txt");
    }

    public MyLinkedList getList1()
    {
    return list1;
    }

    /*
    public void writeToFile(MyLinkedlist list, int direction )
    {
    FileWriter writer = new FileWriter("output.txt") ;
    PrintWriter outfile = new PrintWriter(writer) ;
    ListIterator itr = list.listIterator();

    if (direction == 1)
    {
    while(itr.hasNext())
    {
    outfile.out.println(itr.next());
    }
    else if (direction == 0)
    {
    while(itr.hasPrevious())
    {
    outfile.out.println(itr.previous());
    }
    }
    }
    }
    */
    public void playGame(Player p)
    {

    int nextEliminate = p.getValue();
    String dir = p.getMove();

    Player nP = list1.elimination(nextEliminate,dir);
    System.out.println(p.getName() + " " +p.getValue());
    playGame(nP);

    }

    public void elimination(int nextEliminate, String dir)
    {


    }
    }





    import java.io.*;


    public class MyLinkedList
    {
    private Node first;
    private Node crnt; //iterator
    int size;

    public MyLinkedList()
    {
    this.first = null;
    this.crnt = first;
    this.size = 0;
    }
    public Player getFirst()
    {
    return this.first.getData();
    }

    public Player getCrnt()
    {
    return this.crnt.getData();
    }


    public boolean isEmpty()
    {
    return this.first == null;
    }

    public void insertSorted(Player item)
    {
    if(first==null)
    { first = new Node(item);
    first.next = first;
    first.prev = first;


    }
    else if (item.getName().compareTo(first.data.getName())<0)
    {
    Node n = new Node(first.getData());
    first.setData(item);
    n.next = first.next;
    n.prev = first;
    n.next.prev = n;
    first.next = n;

    }
    else
    {
    crnt = first;
    while(crnt.next != first &&
    (item.getName().compareTo(crnt.next.data.getName() )>0))
    crnt = crnt.next;

    Node n = new Node(item);
    n.next = crnt.next;
    n.prev = crnt;
    n.next.prev = n;
    crnt.next = n;

    }
    size++;

    }

    public Player removeRev(int pos)
    {
    Node pointer = this.crnt;
    for(int i = pos+1; i>=0; i--)
    {
    pointer = pointer.prev;
    }
    Player retPlayer = pointer.prev.getData();
    pointer.prev = pointer.prev.prev;
    pointer.next = pointer.next.next;
    return retPlayer;
    }


    public Player remove(int pos)
    {

    for(int i = 0; i <= pos+1; i++)
    {
    crnt = crnt.next;
    }

    Player retPlayer = crnt.getData();
    crnt.next = crnt.next.next;
    crnt.next = crnt.next.prev;
    return retPlayer;
    }



    public Player elimination(int nextEliminate, String dir)
    {
    if(dir == "F")
    {
    for(int i = 0; i <= nextEliminate; i++)
    {
    crnt = crnt.next;
    }
    if(crnt.data.getMove() == "F"){
    crnt.next=crnt.next.next;
    return (crnt.getData());}
    else if(crnt.data.getMove() == "B")
    { crnt.prev=crnt.prev.prev;
    return (crnt.getData());
    }

    else if(dir == "B")
    {
    for(int i = nextEliminate; i >=0; i--)
    {
    crnt = crnt.prev;
    }
    if(crnt.data.getMove() == "F"){
    crnt.next=crnt.next.next;
    return (crnt.getData());}
    else if(crnt.data.getMove() == "B")
    { crnt.prev=crnt.prev.prev;
    return (crnt.getData());
    }

    }

    }
    return null;
    }


    // public void addFirst(Object item)
    // {
    // Node newbee = new Node(item);
    // newbee.next = this.first;
    // this.first = newbee;
    // }

    // public Object removeFirst()
    // {
    // if (this.first == null)
    // return null;

    // Object item = this.first.data;
    // this.first = this.first.next;

    // return item;
    //}

    public int size()
    {
    int count = 0;

    Node pointer = this.first;
    while (pointer != null)
    {
    count++;
    pointer = pointer.next;
    }

    return count;
    }

    public void printList()
    {
    Node pointer = this.first;
    while (pointer.next != this.first)
    {
    System.out.println(pointer.getData());
    pointer = pointer.next;
    }
    System.out.println(pointer.getData());

    }
    public void printList(String file)
    {

    Node pointer = this.first;

    try{
    PrintWriter outfile = new PrintWriter(new FileWriter (file,true)) ;
    outfile.println("List in starting order");
    while (pointer.next != this.first)
    {
    outfile.println(pointer.getData());
    pointer = pointer.next;
    }
    outfile.println(pointer.getData());
    outfile.println('\n');

    outfile.close();
    }
    catch (IOException exc)
    {
    exc.printStackTrace();
    }
    }

    public void printRevList(String file)
    {

    Node pointer = this.first;

    try{
    PrintWriter outfile = new PrintWriter(new FileWriter (file,true)) ;
    outfile.println("List in reverse order");
    do
    {
    outfile.println(pointer.prev.getData());
    pointer = pointer.prev;
    }while (pointer != this.first);
    outfile.println('\n');
    outfile.close();
    }
    catch (IOException exc)
    {
    exc.printStackTrace();
    }
    }


    public boolean contains(Object item)
    {
    Node pointer = this.first;
    while (pointer != null && !pointer.data.equals(item))
    pointer = pointer.next;

    return (pointer != null) && (pointer.data.equals(item));
    }

    //public void addLast(Object item)
    // {
    // Node newbee = new Node(item);

    // Node pointer = this.first;
    // Node trailer = null;

    // while (pointer != null)
    // {
    // trailer = pointer;
    // pointer = pointer.next;
    // }

    //if (trailer == null)
    // this.first = newbee;
    // else
    // trailer.next = newbee;
    //}

    //public Object removeLast()
    //{
    // if (this.first == null)
    // return null;

    // Node pointer = this.first;
    // Node trailer = null;
    // while (pointer.next != null)
    // {
    // trailer = pointer;
    // pointer = pointer.next;
    // }

    //if (trailer == null)
    // this.first = null;
    // else
    // trailer.next = null;

    //return pointer.data;
    //}

    /*public void addAfter(Object incumbent, Object newItem)
    {
    Node pointer = this.first;
    while (pointer != null && !pointer.data.equals(incumbent) )
    pointer = pointer.next;

    if (pointer == null)
    this.addFirst(newItem);
    else
    {
    Node newbee = new Node(newItem);
    newbee.next = pointer.next;
    pointer.next = newbee;
    }
    }

    public void addBefore(Object incumbent, Object newItem)
    {
    Node pointer = this.first;
    Node trailer = null;
    while (pointer != null && !pointer.data.equals(incumbent) )
    {
    trailer = pointer;
    pointer = pointer.next;
    }

    if (pointer == null)
    this.addFirst(newItem);
    else
    {
    Node newbee = new Node(newItem);
    newbee.next = pointer;
    if (trailer == null)
    this.first = newbee;
    else
    trailer.next = newbee;
    }
    }

    public void remove(Object item)
    {
    Node pointer = this.first;
    Node trailer = null;
    while (pointer != null && !pointer.data.equals(item))
    {
    trailer = pointer;
    pointer = pointer.next;
    }

    if (pointer != null)
    if (trailer == null)
    this.first = pointer.next;
    else
    trailer.next = pointer.next;
    }

    public String toString()
    {
    String image = this.size() + " Items";

    Node pointer = this.first;
    while (pointer != null)
    {
    image += "\n" + pointer.data;
    pointer = pointer.next;
    }

    return image;
    } */
    private class Node
    {
    private Player data;
    public Node next;
    public Node prev;;

    public Node(Player item)
    {
    this.data = item;
    this.next = null;
    this.prev = null;
    }
    public Player getData()
    {
    return this.data;
    }
    public void setData(Player p)
    {
    this.data = p;
    }
    }
    }







    public class gameTester{
    public static void main (String [] args)
    {
    Game e = new Game();
    e.playGame(e.getList1().getFirst());
    }
    }





    public class Player
    {
    private String name;
    private int value;
    private String move;


    public Player(String name, int value, String move)
    {
    this.name=name;
    this.value=value;
    this.move=move;
    }

    public String toString()
    {
    String s = this.name + " " + this.value + " " + this.move;
    return s;
    }

    public String getName()
    {
    return this.name;
    }

    public int getValue()
    {
    return this.value;
    }

    public String getMove()
    {
    return this.move;
    }

    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    1. Assignment dump - Check
    2. Code dump - Check
    2.a Unformatted code - Check
    2.b Gazillion lines of code - Check
    3. No error message - Check
    4. No question - Check

  3. #3
    ecorvo07 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Im sorry I am new to this. Can you explain what you mean?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Don't just dump your assignment on us and expect us to do it for you.
    Don't just dump your code on us and expect us to fix it for you.
    If you post code then use code tags. Place [code] at the start and then [ /code ] (with no spaces) at the end.
    Make a SSCCE - google it.
    Copy and paste the EXACT error message(s) you get
    The error message states on which line it occurs. Indicate that line to us.
    Most importantly of all ask a specific question. "My code doesn't work" is not a specific question.

  5. #5
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Don't just dump your assignment on us and expect us to do it for you.
    Don't just dump your code on us and expect us to fix it for you.
    Agreed on that.

    Java Code:
    while (pointer.next != this.first){
    ....
    }
    Now, this is giving you NullPointerException. To know why, just go through your own code, you'll find the reason.
    -Kind Regards
    Prajin

  6. #6
    ecorvo07 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Im sorry
    the error i get is Exception in thread "main" java.lang.NullPointerException
    and it is happening here int nextEliminate = p.getValue();

    i thing it is becuase this method

    public Player elimination(int nextEliminate, String dir)
    {
    if(dir == "F")
    {
    for(int i = 0; i <= nextEliminate; i++)
    {
    crnt = crnt.next;
    }
    if(crnt.data.getMove() == "F"){
    crnt.next=crnt.next.next;
    return (crnt.getData());}
    else if(crnt.data.getMove() == "B")
    { crnt.prev=crnt.prev.prev;
    return (crnt.getData());
    }

    else if(dir == "B")
    {
    for(int i = nextEliminate; i >=0; i--)
    {
    crnt = crnt.prev;
    }
    if(crnt.data.getMove() == "F"){
    crnt.next=crnt.next.next;
    return (crnt.getData());}
    else if(crnt.data.getMove() == "B")
    { crnt.prev=crnt.prev.prev;
    return (crnt.getData());
    }

    }

    }
    return null;
    }

    is not returning the way it should be and instead it is jumping to the return null statement


    the idea is to have a circular linked list of players: "name" "Step" "Direction"

    ex: John 4F meaning move 4 steps forward and eliminate the player in which you land. then get that players info to make your next move. you get it?

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 3
    Last Post: 06-01-2010, 03:01 PM
  3. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  4. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •