This is the only part of my assignment that has problem.
Can anyone tell me what's wrong?
I debug it, it shows me NullPointerException...
please give me a hint, i urgently need this to be done.
Its due today.
So, I am supposed to write a method captured that will re-arrange my original list.
This method records the capture of the person with the given name, transferring the person from the play ring to the captured list. This operation should not change the play ring order of printPlayRing (i.e., whoever used to be printed first should still be printed first unless that’s the person who was caputred, in which case the person who used to be printed second should now be printed first). Throw an IllegalArgumentException if the given name is not part of the current play ring and throw an IllegalStateException if the game is over (it doesn’t matter which it throws if both are true). Ignore case in comparing names

public void capture(String name){
     
    Node current = front;
    // if player's name is not in the list, throw new IllegalArgumentException
    if(playRingContains(name) != true){
      throw new IllegalArgumentException();
    }
    // if game is over, throw IllegalStateException
    if(gameOver() == true){
      throw new IllegalStateException();
    }
    // Go through the list when game is not over and ring contains the player name
    while(playRingContains(name) && !gameOver()){   
        if(current.item.equals(name)){
                Node temp = current;
                Node helper = current;
                
                helper.next = null;
                if(captured == null){
                    captured = helper;
                }else{
                    captured.next = helper;
                    captured = captured.next;
                }
                
                if(current.prev == null){
                    Node previous = current;
                    while(previous.next != null){
                        previous = previous.next;
                    }
                    captured.prevItem = previous.item;
                    temp = temp.next;
                    previous.setGold(captured.gold);
                }
                if(current != null && current.prev != null){
                    if(current.next == null){
                        temp.prev.next = null;
                    }
                    temp.prev.next = temp.next;
                    temp.prev.setGold(captured.gold);
                }
                }
        }
        current = current.next;    
        }

I know roughly what I am supposed to do.
1. Store the node to the new list.
2. Delete the node from the old list by making the previous node in the list pointing to the next list.
3. If the new list is empty, just store the node. If the new list is not empty, store the node to the next of the list.

it shows me NullPointerException...

Please post the full text of the error message. It has needed info about your code.

Did you find the variable that is null? Did you look back through the code to see why that variable is null and change the code so it is not null?

If you want any help with this code, you need to post a small complete program that compiles and executes. No way to debug an isolated method.

Edited 5 Years Ago by NormR1: n/a

I have some problem understanding why it is null. As i change my local variable, it changes the list too! If I make the next node of my local variable, it changes the whole list.
I have rewritten the code, and now it's working, it can print who the current hunter that got hunted, and the amount of gold changes too. The problem is that, I dont think I am creating a list for "those who got hunted".

public void capture(String name){
      Node current = front;
    // if player's name is not in the list, throw new IllegalArgumentException
    if(playRingContains(name) != true){
      throw new IllegalArgumentException();
    }
    // if game is over, throw IllegalStateException
    if(gameOver() == true){
      throw new IllegalStateException();
    }
    // Go through the list when game is not over and ring contains the player name
    while(playRingContains(name) && !gameOver() && current != null){

        if(current.item.equals(name)){
        if(current.next == null){
            Node helper = current;
            if(captured == null){
                captured = helper;
                captured.next = null;
            }else{
                captured.next = helper;
                captured = captured.next;
            }
        
            Node temp = current;
            temp.prev.next = null;
            temp.prev.setGold(captured.gold);
        }
        
        if(current != null && current.next != null && current.prev != null){
            Node helper = current;
            if(captured == null){
                captured = helper;
                captured.next = null;
            }else{
                captured.next = helper;
                captured = captured.next;
            }
            
            Node temp = current;
            temp.prev.next = temp.next; 
            temp.prev.setGold(captured.gold);
            
            }
            
        if(current.prev == null){
            Node helper = current;
             if(captured == null){
                captured = helper;
                captured.prev = helper.prev;
                captured.next = null;
            }else{
                captured.next = helper;
                captured = captured.next;
            }
            Node temp = current;
            Node previous = current;
            while(previous.next != null){
                previous = previous.next;
            }
            temp.prevItem = previous.item;
            previous.setGold(captured.gold);
        }
    }
    current = current.next;
  }
}

If you want help you need to post a small program that compiles and executes and shows your problem. Be sure to also post the program's output with comments on what is wrong with the output and show what it needs to be.
Your isolated method can't be used for testing.

This article has been dead for over six months. Start a new discussion instead.