I was writing this simple loop and it's giving me trouble. It's supposed to count through the permutations in a deck, so Suit is supposed to go as high as Suit: 3 and Rank is supposed to go as high as Rank: 12, but it isn't doing that. My program output, followed by my code is below.

Welcome to DrJava. Working directory is /Volumes/LA-PUBLIC/TestProg2
> run loopNumbers
Rank: 0and Suit: 0
Rank: 0and Suit: 1
Rank: 0and Suit: 2
Rank: 1and Suit: 0
Rank: 1and Suit: 1
Rank: 1and Suit: 2
Rank: 2and Suit: 0
Rank: 2and Suit: 1
Rank: 2and Suit: 2
Rank: 3and Suit: 0
Rank: 3and Suit: 1
Rank: 3and Suit: 2
Rank: 4and Suit: 0
Rank: 4and Suit: 1
Rank: 4and Suit: 2
Rank: 5and Suit: 0
Rank: 5and Suit: 1
Rank: 5and Suit: 2
Rank: 6and Suit: 0
Rank: 6and Suit: 1
Rank: 6and Suit: 2
Rank: 7and Suit: 0
Rank: 7and Suit: 1
Rank: 7and Suit: 2
Rank: 8and Suit: 0
Rank: 8and Suit: 1
Rank: 8and Suit: 2
Rank: 9and Suit: 0
Rank: 9and Suit: 1
Rank: 9and Suit: 2
Rank: 10and Suit: 0
Rank: 10and Suit: 1
Rank: 10and Suit: 2
Rank: 11and Suit: 0
Rank: 11and Suit: 1
Rank: 11and Suit: 2
>

public class loopNumbers{
  public static void main(String args[]){
int index=0;
    for (int rank = 0; rank < 12; ++rank) { 
      for (int suit = 0; suit < 3; ++suit){ 
          System.out.println("Rank: "+ rank +"and Suit: "+ suit);
        index++; 
     } 
    } 
  }
  }

what problem is it giving? is it throwing an exception, do you get an error message, .. ?

what problem is it giving? is it throwing an exception, do you get an error message, .. ?

Sorry, the problem is a little more complex. I'm writing a larger program than this which involves an identical loop. But I didn't include that program because I didn't think it would be appropriate to ask someone to wade through my code. The idea is, when I run the for loops with the expression rank <= 13 and suit <= 3, instead of the < operator it counts as far as I need it too but it throws a null pointer exception. It's probably too big a problem to ask about on this forum. I'm using the loop to assign values to objects, but I don't see why it's one or the other. If this is too messy a problem for this board, its fine, once you asked your question, I realized that asking might not have been a good idea.

how do you expect us to find where you have a nullpointerexception in your code, if you don't show the actual code in which you get that exception?

As far as I can see, there is nothing in the code you posted that can throw a nullpointerexception.

post the entire code, and the entire error message for clarity, since it normally tells you exactly which line of the code throws the exception.

commented: If you hadn't told me to post all my code anyway, I probably wouldn't have gotten a solution. +1
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prog2;

/**
 *
 * @author ???
 */
public class Prog2 {
    /**
     * @param args the command line arguments
     */
    public static void main(String [] args){
    Card card = new Card(Rank.QUEEN,Suit.SPADES);
    System.out.println(card); 
    System.out.println(card.toStringShort()); 
    System.out.println("_____________________________"); 
    //Rank.getRank(1);
    //Suit.getSuit(1);
    //Rank rankle = new Rank(4);
    //Rank.getrankIndex(rankle);
    //Rank.setrankIndex(rankle,88);
    //Rank.getrankIndex(rankle);
    Deck deck = new Deck(); 
    deck.shuffle(); 
 for (int i = 0; i < 50; ++i) {
        System.out.println(deck.dealCard());
}
/* 
    System.out.println(deck); //prints the remaining cards
    System.out.println("_____________________________");
    Shoe shoe = new Shoe(4);
    shoe.shuffle();  
    for (int i = 0; i < 50; ++i) {
        System.out.println(shoe.dealCard());
    } 

System.out.println("_____________________________"); */
}
}
    class Card{ //outputs info for a single instance of a card
       public Rank singleRank;
       public Suit singleSuit;
       public static int rankParameter=0;
       public static int suitParameter=0;

     // Item 1: a two arg constructor that passes in a rank and suit which will initialize the card.   
       public Card(Rank rank, Suit suit){
       singleRank = rank;
       singleSuit = suit;    
       }
       //Item 2: a two arg constructor that takes two integer parameter, one representing a rank (from 0-12) and one representing a suit (from 0-3). Use these two variables to initialize the Rank and Suit instance variables.  
public Card(int x, int y){
rankParameter= x;
suitParameter= y;
    }
    //Item 3: getter methods for the Card class that return Rank and Suit.
       public static Rank getRank(){
           Rank cardInstance = Rank.getRank(rankParameter);
           return cardInstance;}
       public static Suit getSuit(){
           Suit cardInstance = Suit.getSuit(suitParameter);
           return cardInstance;}
    //Item 4: a toString() method, which will return the value of the card as follows: Rank of Suit such as Two of Diamonds.
    @Override
 public String toString () {
   return Rank.getName(rankParameter)+" of "+Suit.getName(suitParameter);
}
    //Item 5: an equals method that will return true if both cards have the same rank and suit.
       public static boolean equals(Card c1, Card c2){return (c1.rankParameter == c2.suitParameter && c1.rankParameter == c2.rankParameter);}
    //Item 6: a toStringShort() method which will return the value of the card as follows: abbreviated Rank (no space) abbreviated Suit such as 2D. 
 public String toStringShort () {
   return Rank.getAbbrev(rankParameter)+Suit.getAbbrev(suitParameter);
}  
    } // end Card class
    
        
    class Deck{ //simulates a deck of cards
       //Item 1: a no arg constructor that stores a complete set of cards (13 cards in each suit).  Hold the cards in an array of Card. 
     Card[] deck = new Card[52];  
        public Deck(){
    
    System.out.println("begin no arg constructor: Deck");
    int index = 0; 
    for (int rank = 0; rank <= 12; rank++) { 
      for (int suit = 0; suit <= 3; suit++) { 
        deck[index] = new Card (Rank.getRank(rank),Suit.getSuit(suit));
        System.out.println(Rank.getName(Rank.getrankIndex(deck[index].singleRank))+" of "+Suit.getName(Suit.getsuitIndex(deck[index].singleSuit)));
        index++;
      } 
     } 
    
            //Rank.getrankIndex(deck[35].singleRank);
            //Rank.setrankIndex(deck[1].singleRank, 213);
            //Rank.getrankIndex(deck[1].singleRank);
            //System.out.println("no arg constructor end connection: Deck");

      }
       //Item 2: a method called shuffle, that will shuffle the 52 cards in the deck. Use java.util.Random to select your random numbers. 
        public void shuffle(){
        //Rank.setrankIndex(deck[i].singleRank, math.R);
        }
       //Item 3: a method called dealCard, that returns the next card in the deck.
        public String dealCard(){ 
          // Rank.getName(Rank.getrankIndex(deck[1].singleRank));
           
        String fill;
        int i=0;
        i += 1;

        fill=(Rank.getName(Rank.getrankIndex(deck[1].singleRank)) +" of "+Suit.getName(Suit.getsuitIndex(deck[1].singleSuit)));
        return fill;
        }
       //Item 4: a method called getCardsRemaining(), which returns the number of remaining cards in the deck that have not been played.
        
       //Item 5: a toString method, that will return a string with the remaining cards in the deck. Note toString() prints the abbreviated version of the card. Example: AD QS 4D 9H....
        
    }
    
    
    class Shoe{ //simulates storage of 2 to 8 decks of cards
        // Create a no arg constructor that stores one deck of cards in the shoe. 
        
        // Create a one arg constructor that takes in an integer that states the number of decks in the shoe. 

        // Create a method called shuffle, that shuffles all the decks in the shoe. 

        // Create a method called dealCard, that returns the next card in the shoe. 

        // Create a method called getCardsRemaining(), which returns the number of remaining cards in the shoe that have not been played. 
    
    }
    
    
    
    class Rank{
        public Rank(){//initializes new instance
            
        }
        // Item 1: a private static array of Strings and initialize it to include the names of each rank ("Ace","Two",....."King")
              private static String[] rankNames={"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
        // Item 2: a private static array of Strings and initialize it to include the abbreviated rank name for each rank ("A","2","3",...,"Q","K").
              private static String[] rankNameAbbrev={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        // Item 3: a private instance variable of type int that holds a value from 0-12 representing one of the 13 ranks
              private int rankIndex=0;
        // Item 4: a public constructor with one integer parameter that is used to initialize the instance variable
              public Rank(int enterRankIndex){ rankIndex=enterRankIndex;}
        // Item 5: a public getter that uses the instance variable to return the rank name as a String.
              public static String getName(int rankIndex)
              {return rankNames[rankIndex]; }
        // Item 6: a public getter that uses the instance variable to return the abbreviated rank name as a String
              public static String getAbbrev(int rankIndex)
              {return rankNameAbbrev[rankIndex]; }
        // Item 7: a private static array with 13 elements of type Rank and initialize it with Rank objects corresponding to each of the 13 indices. Create array of objects section 8.11
              private static Rank[] populateRank(){Rank rankArray[] = new Rank[13];
              rankArray[0] = new Rank(0);
              for(int i=0; i < 12; i++)
              {
        rankArray[i] = new Rank(i);
        //System.err.println("Rank: "+ i +"populated with the instance number" + rankArray[i].rankIndex );
                            }
              
              return rankArray;
              }
        // Item 8: a public static getter that takes a single integer as a parameter and returns the corresponding Rank object from the static array
        public static Rank getRank(int rankIndex){
            Rank[] rankRange = populateRank(); 
            Rank outputRank = rankRange[rankIndex];
            //System.out.println("the instance variable rankIndex remains conserved and it is: "+outputRank.rankIndex);
            return outputRank;
        }
        public static int getrankIndex(Rank obj){
            System.out.println("Location: Rank.getrankIndex(), rankIndex reads: "+obj.rankIndex);
            int objectOfRankInstanceIs=obj.rankIndex;
            System.out.println("Location: Rank.getrankIndex(), objectOfRankInstanceIs reads: "+objectOfRankInstanceIs);
            return objectOfRankInstanceIs;
        }
         public static void setrankIndex(Rank obj, int setRank){
            System.out.println("Location: Rank.setrankIndex(), rankIndex reads: "+obj.rankIndex);
            obj.rankIndex=setRank;
            System.out.println("Location: Rank.setrankIndex(), rankIndex reads: "+obj.rankIndex);
        }
        // Item 9: List of Public Static Final Int variables representing the ranks
        public static final int ACE=0;
        public static final int TWO=1;
        public static final int THREE=2;
        public static final int FOUR=3;
        public static final int FIVE=4;
        public static final int SIX=5;
        public static final int SEVEN=6;
        public static final int EIGHT=7;
        public static final int NINE=8;
        public static final int TEN=9;
        public static final int JACK=10;
        public static final int QUEEN=11;
        public static final int KING=12;
        
    } // end Rank Class
    
    
    
    
    
    class Suit{
         public Suit(){//initializes new instance
            
        }
               // Item 1: a private static array of Strings and initialize it to include the names of the suits ("Hearts",...)
              private static String[] suitNames={"Hearts","Spades","Diamonds","Clubs"};
        // Item 2: a private static array of Strings and initialize it to include the abbreviated rank name for each suit ("H","S","D","C").
              private static String[] suitNameAbbrev={"H","S","D","C"};
        // Item 3: a private instance variable of type int that holds a value from 0-3 representing one of the 4 suits
              private int suitIndex=0;
        // Item 4: a public constructor with one integer parameter that is used to initialize the instance variable
              public Suit(int enterSuitIndex){ suitIndex=enterSuitIndex; }
        // Item 5: a public getter that uses the instance variable to return the suit name as a String.
              public static String getName(int suitIndex)
              {return suitNames[suitIndex]; }
        // Item 6: a public getter that uses the instance variable to return the abbreviated suit name as a String
              public static String getAbbrev(int suitIndex)
              {return suitNameAbbrev[suitIndex]; }
        // Item 7: a private static array with 4 elements of type Suit and initialize it with Suit objects corresponding to each of the 4 indices. Create array of objects section 8.11
             private static Suit[] populateSuit(){Suit suitArray[] = new Suit[4];
              suitArray[0] = new Suit(0);
              for(int i=0; i < 3; i++)
              {
        suitArray[i] = new Suit(i);
        //System.err.println(suitArray[i].suitIndex + "populateSuit");
                            }
              
              return suitArray;
              }
        // Item 8: a public static getter that takes a single integer as a parameter and returns the corresponding Suit object from the static array
      public static Suit getSuit(int suitIndex){
            Suit[] suitRange = populateSuit();
            Suit outputSuit = suitRange[suitIndex];
            //System.out.println(outputSuit.suitIndex + "getSuit method is functioning just fine");
            return outputSuit;
        }
       public static int getsuitIndex(Suit obj){
            System.out.println("Location: Suit.getsuitIndex(), suitIndex reads: "+obj.suitIndex);
            int objectOfSuitInstanceIs=obj.suitIndex;
            System.out.println("Location: Rank.getsuitIndex(), objectOfSuitInstanceIs reads: "+objectOfSuitInstanceIs);
            return objectOfSuitInstanceIs;
        }
         public static void setsuitIndex(Suit obj, int setSuit){
            System.out.println("Location: Rank.setsuitIndex(), suitIndex reads: "+obj.suitIndex);
            obj.suitIndex=setSuit;
            System.out.println("Location: Rank.setsuitIndex(), suitIndex reads: "+obj.suitIndex);
        }
        // Item 9: List of Public Static Final Int variables representing the suits
   
        public static final int SPADES = 1;
        public static final int HEARTS = 0;
        public static final int CLUBS = 3;
        public static final int DIAMONDS = 2;
    } // end suit class

Error Message is interspersed with console output:

run:
Queen of Spades
Exception in thread "main" java.lang.NullPointerException
at prog2.Suit.getsuitIndex(Prog2.java:243)
QS
at prog2.Deck.<init>(Prog2.java:90)
_____________________________
at prog2.Prog2.main(begin no arg constructor: Deck
Prog2.java:26)
Location: Rank.getrankIndex(), rankIndex reads: 0
Location: Rank.getrankIndex(), objectOfRankInstanceIs reads: 0
Location: Suit.getsuitIndex(), suitIndex reads: 0
Location: Rank.getsuitIndex(), objectOfSuitInstanceIs reads: 0
Ace of Hearts
Location: Rank.getrankIndex(), rankIndex reads: 0
Location: Rank.getrankIndex(), objectOfRankInstanceIs reads: 0
Location: Suit.getsuitIndex(), suitIndex reads: 1
Location: Rank.getsuitIndex(), objectOfSuitInstanceIs reads: 1
Ace of Spades
Location: Rank.getrankIndex(), rankIndex reads: 0
Location: Rank.getrankIndex(), objectOfRankInstanceIs reads: 0
Location: Suit.getsuitIndex(), suitIndex reads: 2
Location: Rank.getsuitIndex(), objectOfSuitInstanceIs reads: 2
Ace of Diamonds
Location: Rank.getrankIndex(), rankIndex reads: 0
Location: Rank.getrankIndex(), objectOfRankInstanceIs reads: 0
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

in your Deck constructor, change

for (int rank = 0; rank <= 12; rank++) {
for (int suit = 0; suit <= 3; suit++) {

by:

for (int rank = 0; rank < 12; rank++) {
for (int suit = 0; suit < 3; suit++) {

Around lines 60 and 228 the initialisation loops stop one short, and leave the last element of each array null.

for(int i=0; i < 12; i++) { // initialises the first 12 elements of 13
   rankArray[i] = new Rank(i);
for(int i=0; i < 3; i++) {        // initialises the first 3 elements of 4
  suitArray[i] = new Suit(i);

I think stultuske was wrong in this individual case. The problem is not that you were accessing non-existant array elements, it's just that you failed to inialise them all earlier.
With 13 ranks and 4 suits your loops should all look like these:

for (int rank=0; rank < 13; rank++) ...
for (int suit=0; suit < 4; suit++) ...
commented: Your solution worked beautifully +1

Around lines 60 and 228 the initialisation loops stop one short, and leave the last element of each array null.

for(int i=0; i < 12; i++) { // initialises the first 12 elements of 13
   rankArray[i] = new Rank(i);
for(int i=0; i < 3; i++) {        // initialises the first 3 elements of 4
  suitArray[i] = new Suit(i);

I think stultuske was wrong in this individual case. The problem is not that you were accessing non-existant array elements, it's just that you failed to inialise them all earlier.
With 13 ranks and 4 suits your loops should all look like these:

for (int rank=0; rank < 13; rank++) ...
for (int suit=0; suit < 4; suit++) ...

Thank You, I applied your solution and it worked beautifully.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.