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 2 of 2

Thread: Null Pointer Exception-Freecell

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception-Freecell

    Hello, I am looking for some help for a class I have, I am currently getting a null pointer exception on line 10 of my Freecell.java class

    Here's my code:

    Freecell.java:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    public class Freecell extends JApplet{
    	Deck a = new Deck();
    	public static ArrayList [] piles = new ArrayList[8];
    	public void init() {
    		a = Deck.shuffle(a);
    		for(int i = 0; i<8; i++){
    			piles[i].add(new ArrayList());
    			Pile.makePile(a, i);
    		}
    		repaint();
    	}
    	public void paint (Graphics g){
     
    	}
    }

    Card.java:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    public class Card {
    	public int suite;
    	public int rank;
    	public Card(){
    		suite= 0;
    		rank=0;
    	}
    	public Card(int rnk, int sui){
    		rank = rnk;
    		suite = sui;
    	}
    	public static String cardString(Card crd){
    		int rnk = crd.rank;
    		int sui = crd.suite;
    		String output = new String();
    		if(rnk == 2){
    			output = "Two";
    		}else if(rnk == 3){
    			output = "Three";
    		}else if(rnk == 4){
    			output = "Four";
    		}else if(rnk == 5){
    			output = "Five";
    		}else if(rnk == 6){
    			output = "Six";
    		}else if(rnk == 7){
    			output = "Seven";
    		}else if(rnk == 8){
    			output = "Eight";
    		}else if(rnk == 9){
    			output = "Nine";
    		}else if(rnk == 10){
    			output = "Ten";
    		}else if(rnk == 11){
    			output = "Jack";
    		}else if(rnk == 12){
    			output = "Queen";
    		}else if(rnk == 13){
    			output = "King";
    		}else if(rnk == 14){
    			output = "Ace";
    		}else if(rnk == 0){
    			output = "Zero";
    		}else {
    			output = "" + rnk;
    		}
    		output += " of ";
    		if(sui == 1){
    			output += "Diamonds";
    		}else if(sui == 2){
    			output += "Hearts";
    		}else if(sui == 3){
    			output += "Clubs";
    		}else if(sui == 4){
    			output += "Spades";
    		}else if(sui == 0){
    			output += "Nothing";
    		}else{
    			output += sui;
    		}
    		return output;
    	}
    	public static String cardFileString(Card crd){
    		int rnk = crd.rank;
    		int sui = crd.suite;
    		String output = new String();
    		if(rnk == 2){
    			output = "2";
    		}else if(rnk == 3){
    			output = "3";
    		}else if(rnk == 4){
    			output = "4";
    		}else if(rnk == 5){
    			output = "5";
    		}else if(rnk == 6){
    			output = "6";
    		}else if(rnk == 7){
    			output = "7";
    		}else if(rnk == 8){
    			output = "8";
    		}else if(rnk == 9){
    			output = "9";
    		}else if(rnk == 10){
    			output = "t";
    		}else if(rnk == 11){
    			output = "j";
    		}else if(rnk == 12){
    			output = "q";
    		}else if(rnk == 13){
    			output = "k";
    		}else if(rnk == 14){
    			output = "a";
    		}else if(rnk == 0){
    			output = "0";
    		}else {
    			output = "e";
    		}
    		if(sui == 1){
    			output += "d";
    		}else if(sui == 2){
    			output += "h";
    		}else if(sui == 3){
    			output += "c";
    		}else if(sui == 4){
    			output += "s";
    		}else if(sui == 0){
    			output += "0";
    		}else{
    			output += "e";
    		}
    		output += ".gif";
    		return output;
    	}
    	public static Card getCard(int numb){
    		Card crd = new Card((((numb-1)%13)+2),(((numb -1)/13)+1));
    		return crd;
    	}
    }

    Deck.java:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    public class Deck {
    	public Card [] deck = new Card [52];
    	Deck(){
    		for(int i = 1; i < 53; i++){
    			Card crd = new Card();
    			crd = Card.getCard(i);
    			deck[i - 1] = crd;
    		}
    	}
    	public static Deck shuffle (Deck dk){
    		Deck tmp = dk;
    		for(int i = 0; i < 100; i++){
    			Random r = new Random();
    			int rnd1 = r.nextInt(51)+1;
    			int rnd2 = r.nextInt(51)+1;
    			Card temp = tmp.deck[rnd1];
    			tmp.deck[rnd1] = tmp.deck[rnd2];
    			tmp.deck[rnd2] = temp;
    		}
    		return tmp;
    	}
    }

    Pile.java:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    public class Pile {
    	public static ArrayList <Card> pile = new ArrayList();
    	public Pile() {
    		pile = pile;
    	}
    	public static void makePile (Deck dk, int num){
    		if(num < 1 || num > 8){
    			return;
    		}else{
    			for(int i = 44; i%8+num > -1; i--){
    				Freecell.piles[num].add(dk.deck[i]);
    			}
    		}
    	}
    }

    Any help would be appreciated,

    Thanks


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Null Pointer Exception-Freecell

    String[] words = new String[100];
    System.out.println(words[20].charAt(0));
    What do you expect the output to be?

    Let me put it another way, I am going to pick some apples but first I need to make a box to put them in. Once I have made the box how many apples are inside?
    Improving the world one idiot at a time!

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

    mnpeep (November 10th, 2011)

Similar Threads

  1. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  2. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  3. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  4. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM