nullpointexception error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brandon222
    New Member
    • Jan 2009
    • 4

    nullpointexception error

    ok well i keep getting this nullpointexcept ion error, can someone tell me whats wrong with it. here is my code:

    Code:
    /**
     * The purpose of this program is to recieve the input from the text file.
     * Then shuffle the cards according to that input
     * 
     * @author Brandon Morales
     * @version 1.0
     */ 
    
    import java.util.Scanner;
    import java.io.*;
    
    public class CardShuffle{
      
      public static String [] finalDeck = new String [52];
      
      public static String [] westHand = new String [ 13 ];
      
      public static String [] suit = {"C" , "D" , "H" , "S"};
      
      public static String [] deck = {"2" , "3", "4" , "5" , "6" , "7" , "8" , "9" , "T" , "J" , "Q" , "K" , "A" };
      
      public static void main(String [] args) throws IOException{
        
        
        Scanner textIn = new Scanner (new File ("shuffler.txt"));
        
        while (textIn.hasNext()){
          
          String [] westHand = new String [ 13 ];
          
          String [] deck = {"2" , "3", "4" , "5" , "6" , "7" , "8" , "9" , "T" , "J" , "Q" , "K" , "A" };
          
          String [] suit = {"C" , "D" , "H" , "S"};
          
          boolean loop = true;
          int hand = 0;
          
          for (int x = 0; x < suit.length; x++){
            for (int y = 0; y < deck.length; y++){  
              finalDeck [ x * deck.length + y] = (deck [y] + " of "  + suit [x]);
            }
          }
          
          String line = textIn.nextLine();
          String [] details = line.split(" " , 10);
          
          for (int j = 0; j < details.length; j++){
            shuffle(Integer.parseInt(details[j]));
          }
          
          System.out.println("************West*Hand*******");
          for (int i = 0; i < finalDeck.length; i ++){
            if ( ( i + 1 ) % 4 == 0 ){
              westHand [ hand ] = finalDeck [ i ];
              
              hand ++;
            }
          } 
          
          System.out.println();
          
          hand = 0;
          
          sorter();
          System.out.println("**************************");
        }//text input and print 
      }//Main method
      
      /**
       * The purpose of this method is to take the input of the text and put all the index numbers of the cards
       * and put them in a new array then, and then shuffle the rest of the cards putting one up and one down.
       * 
       * @param shufflePattern The input of the text file
       */ 
      public static void shuffle (int shufflePattern){
        
        String [] choosen = new String [52/shufflePattern];
        
        String [] leftOver = new String [52 - choosen.length];
        
        String [] top = new String [leftOver.length/2];
        
        String [] bottom = new String [(leftOver.length/2) + (leftOver.length%2)];
        
        int deck2 = 0;
        int counter = 0;
        int topCounter = top.length - 1;
        int bottomCounter = 0;
        int finalCount = 0;
        int splitDeck2 = 0;
        boolean loop = true;
        
        
        for (int x = 0; x < finalDeck.length; x++){
          if ((x + 1) % shufflePattern == 0){
            choosen[counter] = finalDeck[x];
            counter ++;
          }
          else { 
            leftOver[deck2] = finalDeck[x];
            deck2++;
          }
        }
        
        for (int i = 0; i < leftOver.length; i++){
          if ((i + 1) % 2 == 0){
            top [topCounter] = leftOver[i];
            topCounter--;
          }
          else {
            bottom [bottomCounter] = leftOver[i];
            bottomCounter++;
          }
        }
        
        for (int i = 0; i < top.length;  i ++) {
          finalDeck[finalCount] = top [i];
          finalCount++;
        }
        for (int i = 0; i < choosen.length; i ++) {
          finalDeck[finalCount] = choosen [i];
          finalCount++;
        }
        for (int i = 0; i < bottom.length; i ++) {
          finalDeck[finalCount] = bottom [i];
          finalCount++;
        }
      }//shuffle method
      
      public static void sorter (){
        
        String [] hearts = new String [13];
        String [] diamonds = new String [13];
        String [] spades = new String [13];
        String [] clubs = new String [13];
    
        int heart =0;
        int spade = 0;
        int club = 0;
        int diamond = 0; 
        char value;
        
        for (int i = 0; i < westHand.length; i ++){
          value = westHand[i].charAt(westHand[i].length()-1);
          
          if (value == 'H'){
            hearts[heart] = westHand[i];
            heart ++;
          }
          else if (value == 'S'){
            spades[spade] = westHand[i];
            spade ++;
          }
          else if (value == 'D'){
            diamonds[diamond] = westHand[i];
            diamond ++;
          }
          else if (value == 'C'){
            clubs [club] = westHand[i];
            club ++;
          }
        }
        for (int i = 0; i < spade; i ++){
          System.out.println(spades[i]);
        }
        for (int i = 0; i < heart; i ++){
          System.out.println(hearts[i]);
        }
        for (int i = 0; i < diamond; i ++){
          System.out.println(diamonds[i]);
        }
        for (int i = 0; i < club; i ++){
          System.out.println(clubs[i]);
        }
      }  
    }//CardShuffle Class
    Last edited by Dormilich; Jan 21 '09, 05:11 AM. Reason: added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that's a Java question (not Javascript, there's a subtle difference). I'll move it to the Java forum.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      @Dormilich : It's actually a huge difference

      @OP : Read the exception trace. It tells you exactly where the nullpointer was thrown at (line number).

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by r035198x
        @OP : Read the exception trace. It tells you exactly where the nullpointer was thrown at (line number).
        @r035198x: tell him that sprinkling in a few System.out.prin tln() calls here and there, showing some information about the objects, does wonders to get rid of those darn NullPointerExce ptions.

        kind regards,

        Jos

        Comment

        • brandon222
          New Member
          • Jan 2009
          • 4

          #5
          Originally posted by r035198x
          @Dormilich : It's actually a huge difference

          @OP : Read the exception trace. It tells you exactly where the nullpointer was thrown at (line number).
          o im sorry i put i tin the wrong place, and my nullpointexcept ion is on line 144

          value = westHand[i].charAt(westHan d[i].length()-1);

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by brandon222
            o im sorry i put i tin the wrong place, and my nullpointexcept ion is on line 144

            value = westHand[i].charAt(westHan d[i].length()-1);
            so variable 'westHand' equals null ...

            kind regards,

            Jos

            Comment

            • brandon222
              New Member
              • Jan 2009
              • 4

              #7
              Originally posted by JosAH
              so variable 'westHand' equals null ...

              kind regards,

              Jos
              o ok i think i figured it out. I think that my array westHand isnt being read in my sorter method, how can i call an array from my main method into my sorter method?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by brandon222
                o ok i think i figured it out. I think that my array westHand isnt being read in my sorter method, how can i call an array from my main method into my sorter method?
                You can't 'call' an array, it won't listen ;-) You can pass it as a parameter to another method though:
                Code:
                static public void anotherMethod(int[] anArray) {
                   // play with the 'anArray' array here ...
                }
                public static void main(String[] args) {
                   int[] a= { 1, 2, 3 }; // an array
                   anotherMethod(a); // pass the array a around
                }
                kind regards,

                Jos

                Comment

                • brandon222
                  New Member
                  • Jan 2009
                  • 4

                  #9
                  Originally posted by JosAH
                  You can't 'call' an array, it won't listen ;-) You can pass it as a parameter to another method though:
                  Code:
                  static public void anotherMethod(int[] anArray) {
                     // play with the 'anArray' array here ...
                  }
                  public static void main(String[] args) {
                     int[] a= { 1, 2, 3 }; // an array
                     anotherMethod(a); // pass the array a around
                  }
                  kind regards,

                  Jos
                  hey thanx a lot...it worked

                  Comment

                  Working...