Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why do I get a null value?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why do I get a null value?

    package wargame;

    import java.util.*;

    public class Table {

    public int n, p, c;
    public LinkedList<String> shuffle = new LinkedList<String>();
    public LinkedList<String> card = new LinkedList<String>();
    public CompleteDeckCard getcard = new CompleteDeckCard();
    LinkedList<String> playerNum = new LinkedList();
    LinkedList<String> shuffledCards = cardShuffle();
    public LinkedList<String> rank = new LinkedList<String>();
    public CompleteDeckCard getrank = new CompleteDeckCard();
    boolean winner;
    //Determining the number of shuffles
    public int tablePlay() {
    System.out.println ("How many shuffles do you want? ");
    Scanner in = new Scanner(System.in);
    n = in.nextInt();
    System.out.println ("Your input is: " + n);
    System.out.println ("Shuffling cards, please wait a moment ...");
    return n;
    }

    //Shuffling of cards
    public LinkedList<String> cardShuffle(){
    card = getcard.cards();

    for (int sc = 0; sc < n; sc++){
    for( int x=0, y=26; x<26; x++,y++){
    shuffle.add(card.get(x));
    shuffle.add(card.get(y));
    }
    card.clear();
    card.addAll(shuffle);
    shuffle.clear();
    }
    //System.out.println("New Deck: " + shuffle);
    return card;
    }

    //Determining the number of players
    public LinkedList<String>[] player() {
    rank = getrank.ranks();
    Scanner in = new Scanner(System.in);
    p = in.nextInt();
    System.out.println ("Number of players: " + p);
    System.out.println ("Creating list of players ... " );
    //return p;

    //Distribution of cards
    shuffledCards = cardShuffle();
    LinkedList<String>[] playerNum = new LinkedList[p];
    for (int z=0; z<p; z++){
    playerNum[z] = new LinkedList<String>();
    System.out.println ("playerNum[" + z + "]");
    }
    while (!shuffledCards.isEmpty()){
    for (int v=0; v<p; v++){
    playerNum[v].addFirst(shuffledCards.pollFirst());
    if (playerNum[v].contains(null))
    playerNum[v].remove(null);
    }
    }
    System.out.println (" ");
    System.out.println ("Distributing cards ...");
    for (int m=0; m<p; m++){
    System.out.println ("playerNum[" + m + "]" + playerNum[m] );
    }

    //Getting the top card

    while (!winner){
    LinkedList<String> getTopCards = new LinkedList<String>();
    for (int g=0; g<p; g++){
    getTopCards.add(playerNum[g].pollFirst());
    }
    System.out.println (" ");
    System.out.println ("Top Cards: " + getTopCards);
    System.out.println (" ");

    //Comparing the values

    LinkedList<Integer> evaluate = new LinkedList<Integer>();
    Collections.reverse(rank);
    System.out.println ("Order of card: " +rank);
    for( int s = 0; s < getTopCards.size(); s++){
    evaluate.add(rank.indexOf(getTopCards.get(s)));
    }
    System.out.println(" ");
    //System.out.println("Ranking: " + evaluate);

    int e = evaluate.indexOf(Collections.max(evaluate));
    System.out.println( "playerNum[" + e + "] is the winner. ");
    System.out.println(" " );

    //Collect the initial cards and give to the winner of the round

    LinkedList<String> entry = new LinkedList<String>();
    for (int d=e; d<p; d++){
    entry.add(getTopCards.get(d));
    }
    if (e!=0){
    for (int f=0; f<e; f++){
    entry.add(getTopCards.get(f));
    }
    }
    playerNum[e].addAll(entry);
    while (playerNum[e].contains(null)){
    playerNum[e].remove(null);
    }

    System.out.println ("Collected cards: " + entry);

    //Determine number of rounds

    LinkedList<Integer> rounds = new LinkedList<Integer> ();


    for (int g=0; g<p; g++){
    System.out.println (playerNum[g]);
    //for (int h=0; h<p; h++){
    if (playerNum[g].size() == 52){
    winner = true;
    break;
    }
    }
    //}
    }
    return playerNum;
    }
    }


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Why do I get a null value?

    It's always good to read the rules of a forum before posting in it. Even without the rules, you should have to common decency and sense to not just dump a wad of code without offering any explanation whatsoever. So, if you want help, there are a few things I suggest you do.

    First, be more specific. If you program is giving null values, it should also be throwing NullPointerExceptions that will terminate your program. Post those here in their entirety. Furthermore, it would be nice to have some context--what are you trying to make? Looking at the exceptions, where is the problem originating? Find the source of your problem, and ask a specific question.

    Second, refine your code. Don't lazily dump it all here; look at your exceptions and problem solve until you've found the section of code causing the problem. Post that section only. The reason for this is simple--it makes forum members' job of helping you easier because there is less to sift through.

    Finally, use the correct code tags when posting code. If you don't know how to do that, read my signature. Using code tags formats your code so that it can be more easily read.

    Trust me, people on this forum want to and will help you, but you have to show that you appreciate their time by making their job as easy as you can.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    copeg (April 29th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why do I get a null value?

    I am sorry about the long code. I thought it would be helpful for readers to see everything. I really don't know yet how to implement the Exceptions.
    I seem to have traced the problem. I just do not know how to tell the program to skip the particular list and proceed to the next entry.

    while (!winner){
    		LinkedList<String> getTopCards = new LinkedList<String>();
    			for (int g=0; g<p; g++){
    				getTopCards.add(playerNum[g].pollFirst());

    I am trying to loop this particular until all the cards go to a single player. What happens is whenever one of the players loses all his cards, the program register null only to be able to proceed to the goal of giving all the cards to only 1 player.

Similar Threads

  1. Null Error
    By Nessera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2011, 01:46 PM
  2. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  3. Null pointers
    By Jared in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 7th, 2010, 06:40 PM
  4. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  5. !=null
    By ss7 in forum Java Theory & Questions
    Replies: 11
    Last Post: October 31st, 2009, 02:48 PM