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

Thread: Could use some beginners tip here..

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Could use some beginners tip here..

    Hey! So i'm trying to learn some stuff with this code. The code is suppose to be a game wich u can play Blackjack in vs the computer. And also give u a decent GUI. Thing is, i've got the code approved to that point that i can compile the program. Altho when im about to run it i get this message:

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1043)
    at java.awt.Container.add(Container.java:363)
    at Blackjack.<init>(Blackjack.java:141)
    at Blackjack.main(Blackjack.java:58)

    I Do understand that there is some problem at line 141 and 58. But i just can't figure out what it is..
    Please help me solve this!

    Here is the code:
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Blackjack extends JFrame implements ActionListener {
     
    	JButton [] start;
    	//JButton [] bet;
    	JButton [] top;
    	JButton [] bottom;
    	JLabel [] stats;
    	JLabel [] playCards;
    	JTextField [] display;
    	BorderLayout layout;
    	JPanel gameField;
    	JPanel startOption;
    	JPanel topDisplay;
    	JPanel sideButton;
    	JPanel bottomButton;
    	BufferedImage[] pCards;
    	BufferedImage[] dCards;
    	String [] getCards;
    	String [] pCardImg;
    	String [] dCardImg;
    	int [] dface;
    	int [] dvalue;
    	int [] dsuit;
    	int [] pface;
    	int [] pvalue;
    	int [] psuit;
    	double betTotal;
    	double winLoss;
    	double cash;
    	int total;
    	int dtotal;
    	int stay;
    	int initial;
    	int dinitial;
     
    	public static void main (String[] args) {
    		Blackjack Gui = new Blackjack();
     
    		Gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    	}
     
    	public Blackjack(){
     
    		//startvärden när spelet startar
    		initial = 0;
    		dinitial = 5;
    		cash = 100.00;
    		winLoss = 0.00;
    		total = 0;
    		dtotal = 0;
    		stay = 0;
    		pCardImg = new String [6];
    		dCardImg = new String [6];
    		dface = new int [6];
    		dvalue = new int [6];
    		dsuit = new int [6];
    		getCards = new String [10];
    		pface = new int [6];
    		pvalue = new int [6];
    		psuit = new int [6];
     
     
    		betTotal = 0;
    		startOption = new JPanel();
    		topDisplay = new JPanel();
    		sideButton = new JPanel();
    		bottomButton = new JPanel();
    		gameField = new JPanel();
     
    		//de olika delarna i GUI
    		startOption.setLayout(new GridLayout(1, 1));
    		topDisplay.setLayout(new GridLayout(1, 1));
    		sideButton.setLayout(new GridLayout(5, 1));
    		bottomButton.setLayout(new GridLayout(1, 3));
    		gameField.setLayout(new GridLayout(2, 5));
     
    		//själva spelytan
    		playCards = new JLabel[10];
    		playCards[0] = new JLabel ("");
    		playCards[1] = new JLabel ("");
    		playCards[2] = new JLabel ("");
    		playCards[3] = new JLabel ("");
    		playCards[4] = new JLabel ("");
    		playCards[5] = new JLabel ("");
    		playCards[6] = new JLabel ("");
    		playCards[7] = new JLabel ("");
    		playCards[8] = new JLabel ("");
    		playCards[9] = new JLabel ("");
     
    		//starta spelet
    		start = new JButton[1];
    		start[0] = new JButton("START");
     
    		//välkommen
    		display = new JTextField[1];
    		display [0] = new JTextField("Välkommen");
    		display [0].setEditable(false);
     
    		//spelar statistik
    		stats = new JLabel[5];
    		stats[0] = new JLabel("Totalt: 0", 10);
    		stats[1] = new JLabel("Satsat: €0.00", 10);
    		stats[0] = new JLabel("Kort Kvar: 52", 10);
    		stats[0] = new JLabel("Pengar Kvar: €" + cash, 10);
    		stats[0] = new JLabel("Total V/F: €" + winLoss, 10);
     
    		//nedre knapparna
    		bottom = new JButton[3];
    		bottom[0] = new JButton("Ta Kort");
    		bottom[1] = new JButton("Stanna");
    		bottom[2] = new JButton("Dela");
     
    		//layouten och lägger till komponenter
    		layout = new BorderLayout(5, 5);
     
    		startOption.add(start[0]);
    		topDisplay.add(display[0]);
    		for (int i = 0; i < 5; i++){
    			sideButton.add(stats[i]);
    		}
    		for (int i = 0; i < 3; i++){
    			bottomButton.add(bottom[i]);
    		}
    		for (int i = 0; i < 10; i++){
    			gameField.add(playCards[i]);
    		}
    		add(gameField, BorderLayout.CENTER);
    		add(startOption, BorderLayout.WEST);
    		add(topDisplay, BorderLayout.NORTH);
    		add(sideButton, BorderLayout.EAST);
    		add(bottomButton, BorderLayout.SOUTH);
     
    		//lyssnare
     
    		start[0].addActionListener(this);
     
    		for(int i = 0; i < 3; i++){
    			bottom[i].addActionListener(this);
    		}
     
    		//inställningar för fönstret
    		setTitle("Blackjack");
    		setSize(800, 500);
    		setVisible(true);
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		Object[] check = {"Ja", "Nej"};
    		Object[] bet = {"€5.00", "€10.00", "€15.00"};
    		JButton c = (JButton)(e.getSource());
    		if(c.getText().equals("Hit")){
    			initial += 1;
    			JOptionPane.showMessageDialog(this,"Dealern gav dig ett kort");
    			dealCards(initial);
    			stats[0].setText("Totalt: " + total);
    			if(total > 21){
    				JOptionPane.showMessageDialog(this, "Du blev tjock");
    				total = 0;
    				dtotal = 0;
    				stats[0].setText("Totalt: " + total);
    				betTotal = 0.00;
    				stats[1].setText("Satsat: €" + betTotal);
    				clearHand();
    				gotoContinue();
    			}
    		}
    		if(c.getText().equals("Stanna")){
    			/*for (int i = 0; i > 6; i++){
    				System.out.println{pface(i)};
    				*/
    				stay = 1;
    				JOptionPane.showMessageDialog(this, "Du stannar");
    				if (stay == 1){
    					dealerDraws();
    					delay(1000);
    				}
    				if (dtotal < 17 && stay == 1){
    					dealerDraws();
    					delay(1000);
    				}
    			}
    			if(c.getText().equals("Dela")){
    				int chk = JOptionPane.showOptionDialog(this,"Är du säker?", "Dubbel kolla", JOptionPane.YES_NO_OPTION,
    				JOptionPane.QUESTION_MESSAGE,
    				null,
    				check,
    				check[2]);
    			}
    			if(c.getText().equals("START")){
     
    				int b = JOptionPane.showOptionDialog(this, "Satsa minsta belopp eller högre.", "Var vänlig satsa", JOptionPane.YES_NO_CANCEL_OPTION,
    			JOptionPane.QUESTION_MESSAGE,
    			null,
    			bet,
    			bet[2]);
     
     
    				if (b == 0){
    					betTotal += 5.00;
    				}
    				if (b == 1){
    					betTotal += 10.00;
    				}
    				if (b == 2){
    					betTotal += 15.00;
    				}
    				stats[1].setText("Satsat: €" + betTotal);
    				betTotal -= betTotal;
    			    stats[3].setText("Pengar Kvar: €" + total);
    			    stay = -1;
    			    int i = 0;
    			    dealCards(i);
    			    System.out.println("TEST");
    			    stats[0].setText("Totalt: €€" + total);
    		}
        }	
     
    		public void dealCards(int j){
    			String [] temp = new String [4];
    			if (j == 0){
    				Shuffle myDealer = new Shuffle ();
    				myDealer.ShuffleCards();
    				for (int i = 0; i < 4; i++){
    					getCards[i] = myDealer.Dealer();
    					temp[i] = getCards[i];
    					System.out.println(getCards[i]);
    				}
    				printCards(temp, j);
    			}
     
    			if (j >= 1){
    				Shuffle myDealer = new Shuffle();
    				myDealer.ShuffleCards();
    				for (int i = 0; i < 2; i++){
    					getCards[i] = myDealer.Dealer();
    					temp[i] = getCards[i];
    					System.out.println(getCards[i]);
    				}
    				printCards(temp, j);
    			}
    		}
     
    		public void printCards(String [] temp, int j){
    			if (j == 0){
    				String [] elements0 = temp[0].split(" ");
    				String [] elements1 = temp[1].split(" ");
    				String [] elements2 = temp[2].split(" ");
    				String [] elements3 = temp[3].split(" ");
    				//spelarens hand
    				pface[0] = Integer.parseInt(elements0[0]);
    				pface[1] = Integer.parseInt(elements1[0]);
    				psuit[0] = Integer.parseInt(elements0[1]);
    				psuit[1] = Integer.parseInt(elements0[1]);
    				//dealerns hand
    				dface[0] = Integer.parseInt(elements0[0]);
    				dface[1] = Integer.parseInt(elements0[0]);
    				dsuit[0] = Integer.parseInt(elements0[1]);
    				dsuit[1] = Integer.parseInt(elements0[1]);
     
    				for (int i = 0; i < 2; i++){
    					if (dface[i] > 10){
    						dvalue[i] = 10;
    					}
    					else{
    						dvalue[i] = dface[i];
    				    }
    				    if(pface[i] > 10){
    						pvalue[i] = 10;
    					}
    					else{
    						pvalue[i] = pface[i];
    					}
    					//spelarens ess värde
    					if(pface[i] == 1 && total <= 11){
    						pvalue[i] += 10;
    					}
    					if(pface[0] == 1 && pface[1] == 1){
    						total = 12;
    					}
    					//dealerns ess värde
    					if(dface[i] == 1 && dtotal <= 11){
    						dvalue[i] += 10;
    					}
    					if(dface[0] == 1 && dface[1] == 1){
    						dtotal = 12;
    					}
    					System.out.println("Spelare: " + pface[i] +" " + psuit[i] + " " + pvalue[i]);
    				    total += pvalue[i];
    				    System.out.println(total);
    				    System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
    				    dtotal += dvalue[i];
    				    System.out.println(dtotal);
    				    getPSuit();
    				    getDSuit();
    				}
    				//Kollar efter Blackjack (en perfekt 21a)och gångrar din satsning med tre, vid eventuell vinst
    				if (total == 21 && dtotal < 21){
    					JOptionPane.showMessageDialog(this,"BLACKJACK!");
    					cash += betTotal * 3;
    					winLoss += betTotal = 3;
    					stats[3].setText("Pengar kvar: €" + (cash));
    					stats[4].setText("Total V/F: €" + winLoss);
    					total = 0;
    					dtotal = 0;
    					stats[0].setText("Totalt: " + total);
    					betTotal = 0;
    					stats[1].setText("Total satsning: €" + betTotal);
    					clearHand();
    					gotoContinue();
    				}
    			}
    			if (j == 1){
    				String [] elements0 = temp [0].split(" ");
    				pface[2] = Integer.parseInt(elements0[0]);
    				psuit[2] = Integer.parseInt(elements0[1]);
     
    				for (int i = 2; i < 3; i++){
    					if (pface[i] > 10){
    						pvalue[i] = 10;
    					}
    					else{
    						pvalue[i] = pface[i];
    					}
    					//spelarens ess värde
    					if (pface[i] == 1 && total <= 11){
    						pvalue[i] += 10;
    					}
    					for ( int k = 0; k < 6; k++){
    						if( pface[k] == 1 && total > 21) {
     
    							pvalue[k] -= 10;
    						}
    					}
    					System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
    					total += pvalue[i];
    					System.out.println(total);
    				}
    				getPSuit();
    			}
    			if (j == 2) {
    				String [] elements0 = temp[0].split(" ");
    				pface[3] = Integer.parseInt(elements0[0]);
    				psuit[3] = Integer.parseInt(elements0[1]);
     
    				for (int i = 3; i < 4; i++){
    					if(pface[i] > 1){
    						pvalue[i] = 10;
    					}
    					else{
    						pvalue[i] = pface[i];
    					}
    					//spelarens ess värde
    					if (pface[i] == 1 && total <= 10){
    						pvalue[i] += 10;
    					}
    					for ( int k = 0; k < 6; k++){
    						if ( pface[k] == 1 && total > 21){
     
    							pvalue[k] -= 10;
    						}
    					}
    					System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
    					total += pvalue[i];
    					System.out.println(total);
    				}
    				getPSuit();
    			}
    			if (j == 3) {
    				String [] elements0 = temp[0].split(" ");
    				pface[4] = Integer.parseInt(elements0[0]);
    				psuit[4] = Integer.parseInt(elements0[1]);
     
    				for (int i = 4; i < 5; i++){
    					 if(pface[i] > 10){
    						pvalue[i] = 10;
    					}
    					else{
    						pvalue[i] = pface[i];
    					}
    					//spelarens ess värde
    					if (pface[i] == 1 && total <= 10);{
    						pvalue[i] += 1;
    					}
    					for ( int k = 0; k < 6; k++){
    						if (pface[k] == 1 && total > 21){
     
    							pvalue[k] -= 10;
    						}
    					}
    					System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
    					total += pvalue[i];
    					System.out.println(total);
    				}
    				getPSuit();
    			}
    			if (j == 4) {
    				String [] elements0 = temp[0].split(" ");
    				pface[5] = Integer.parseInt(elements0[0]);
    				psuit[5] = Integer.parseInt(elements0[1]);
     
    				for (int i = 5; i < 6; i++) {
    					if(pface[i] > 10){
    						pvalue[i] = 10;
    					}
    					else{
    						pvalue[i] = pface[i];
    					}
    					//spelarens ess värde
    					if (pface[i] == 1 && total <= 10){
    						pvalue[i] += 10;
    					}
    					for ( int k = 0; k < 6; k++){
    						if ( pface[k] == 1 && total > 21){
    							pvalue[k] -= 10;
    						}
    					}
    					System.out.println(pface[i] + " " + psuit[i] + " " + pvalue[i]);
    					total += pvalue[i];
    					System.out.println(total);
    				}
    				getPSuit();
    			}
    			if (j == 5){
    				String [] elements0 = temp[0].split(" ");
    				dface[2] = Integer.parseInt(elements0[0]);
    				dsuit[2] = Integer.parseInt(elements0[1]);
     
    				for (int i = 2; i < 3; i++){
    					if (dface[i] > 1){
    						dvalue[i] = 10;
    					}
    					else{
    						dvalue[i] = dface[i];
    					}
    					//dealerns ess värde
    					if (dface[i] == 1 && dtotal <= 10){
    						dvalue[i] += 10;
    					}
    					for ( int k = 0; k < 6; k++){
    						if ( dface[k] == 1 && dtotal > 21){
     
    							dvalue[k] -= 10;
    						}
    					}
     
    					System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
    					dtotal += dvalue[i];
    					System.out.println(dtotal);
    				}
    				getPSuit();
    			}
    			if (j == 6){
    				String [] elements3 = temp[0].split(" ");
    				dface[3] = Integer.parseInt(elements3[0]);
    				dsuit[3] = Integer.parseInt(elements3[1]);
     
    				for (int i = 3; i < 4; i++){
    					if (dface[i] > 10){
    						dvalue[i] = 10;
    					}
    					else{
    						dvalue[i] = dface[i];
    					}
    					// dealerns ess värde
    					if (dface[i] == 1 && dtotal <= 10){
    						dvalue[i] += 10;
    					}
     
    					if (dface[i] == 1 && dtotal <= 21){
    						dvalue[i] -= 10;
    				}
    				System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
    				dtotal += dvalue [i];
    				System.out.println(dtotal);
    			}
    			getPSuit();
    		}
    		if (j == 7) {
    			String [] elements3 = temp[0].split(" ");
    			dface[4] = Integer.parseInt(elements3[0]);
    			dsuit[4] = Integer.parseInt(elements3[1]);
     
    			for (int i = 4; i < 5; i++){
    				if (dface[i] > 10){
    					dvalue[i] = 10;
    				}
    				else{
    					dvalue[i] = dface[i];
    				}
    				//dealerns ess värde
    				if (dface[i] == 1 && dtotal <= 10){
    					dvalue[i] += 10;
    				}
     
    				if (dface[i] == 1 && dtotal <= 21){
    					dvalue[i] -= 10;
    				}
    				System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
    				dtotal += dvalue [i];
    				System.out.println(dtotal);
    			}
    			getDSuit();
    		}
    		if (j == 8) {
    			String [] elements3 = temp[0].split(" ");
    			dface[5] = Integer.parseInt(elements3[0]);
    			dsuit[5] = Integer.parseInt(elements3[1]);
     
    			for (int i = 5; i < 6; i++){
    				 if (dface[i] > 10){
    					 dvalue[i] = 10;
    				}
    				else{
    					dvalue[i] = dface[i];
    				}
    				//dealerns ess värde
    				if (dface[i] == 1 && dtotal <= 10){
    					dvalue[i] += 10;
    				}
     
    				if (dface[i] == 1 && dtotal <= 21){
    					dvalue[i] -= 10;
    				}
    				System.out.println("Dealer: " + dface[i] + " " + dsuit[i] + " " + dvalue[i]);
    				dtotal += dvalue [i];
    				System.out.println(dtotal);
    			}
    			getDSuit();
    		}
    	}
     
    	public void dealerDraws(){
     
    		if (dtotal < 17 && stay == 1){
    			dealCards(dinitial);
    			dinitial += 1;
    		}
    		if (dtotal >= 17 && dtotal <= 21){
    			delay(1000);
    			JOptionPane.showMessageDialog(this, "Dealern stannar");
    			checkHandWin();
    		}
    		if (dtotal > 21){
    			JOptionPane.showMessageDialog(this,"Dealern blev tjock");
    			checkHandWin();
    		}
    	}
     
    	public void checkHandWin(){
     
    		if (total > dtotal || dtotal > 21){
    			JOptionPane.showMessageDialog(this,"Du Vann!");
    			initial = 0;
    			cash += betTotal * 2;
    			winLoss += betTotal;
    			stats[3].setText("Pengar Kvar: €" + (cash));
    			stats[4].setText("Total V/F: €" + winLoss);
    			total = 0;
    			dtotal = 0;
    			stats[0].setText("Totalt: €" + total);
    			betTotal = 0;
    			stats[1].setText("Total Satsning: €" + betTotal);
    			clearHand();
    			gotoContinue();
    		}else{
    			checkHandLost();
    		}
     
    	}
     
    	public void checkHandLost(){
    		if(dtotal > total){
    			JOptionPane.showMessageDialog(this,"Du Förlorade!");
    			initial = 0;
    			dinitial = 5;
    			winLoss -= betTotal;
    			stats[3].setText("Pengar Kvar: €" + (cash));
    			stats[4].setText("Total V/F: €" + winLoss);
    			total = 0;
    			dtotal = 0;
    			stats[0].setText("Totalt: €" + total);
    			betTotal = 0;
    			stats[1].setText("Total Satsning: €" + betTotal);
    			clearHand();
    			gotoContinue();
    		}else{
    			checkHandTie();
    		}
    	}
     
    	public void checkHandTie(){
    		if (dtotal == total){
    			JOptionPane.showMessageDialog(this, "Lika");
    			initial = 0;
    			dinitial = 5;
    		    cash += betTotal;
    			stats[3].setText("Pengar Kvar: €" + (cash));
    			total = 0;
    			dtotal = 0;
    			stats[0].setText("Totalt: €" + total);
    			betTotal = 0;
    			stats[1].setText("Total Satsning: €" + betTotal);
    			clearHand();
    			gotoContinue();
    		}
    		else{
    			initial = 0;
    			dinitial = 5;
    			total = 0;
    			dtotal = 0;
    			stats[0].setText("Totalt: €" + total);
    			cash += betTotal * 2;
    			stats[3].setText("Pengar Kvar: €" + (cash));
    			betTotal = 0.00;
    			stats[1].setText("Total Satsning: €" + betTotal);
    			clearHand();
    			gotoContinue();
    		}
    	}
     
    	public void gotoContinue(){
    		initial = 0;
    		dinitial = 5;
    		total = 0;
    		dtotal = 0;
    		stats[0].setText("Totalt: €" + total);
    		betTotal = 0;
    		stats[1].setText("Total Satsning: €" + betTotal);
    		Object[] check = {"Ja", "Nej"};
    		Object[] bet = {"€5.00", "€10.00", "€15.00"};
    		int a = JOptionPane.showOptionDialog(this,"Vill du spela en ny hand?", "Fortsätta?", JOptionPane.YES_NO_OPTION,
    				JOptionPane.QUESTION_MESSAGE,
    				null,
    				check,
    				check[2]);
     
    		if (a == 0){
     
    			int b = JOptionPane.showOptionDialog(this, "Satsa minsta belopp eller högre.", "Var vänlig satsa", JOptionPane.YES_NO_CANCEL_OPTION,
    			JOptionPane.QUESTION_MESSAGE,
    			null,
    			bet,
    			bet[2]);
     
    			if (b == 0){
    				betTotal += 5.00;
    			}
    			if (b == 1){
    				betTotal += 10.00;
    			}
    			if (b == 2){
    				betTotal += 15.00;
    			}
     
    			stats[1].setText("Total Satsning: €" + betTotal);
    			cash -= betTotal;
    			stats[3].setText("Pengar Kvar: €" + (cash));
    			stay = -1;
    			int i = 0;
    			dealCards(i);
    			System.out.println("TEST");
    			stats[0].setText("Totalt: €" + total);
    		}
     
    		if (a == 1) {
    			if (cash > 50.00){
    				double winnings = cash - 50.00;
    				JOptionPane.showMessageDialog(this, "Du vann €" + winnings );
    			}
    			if (cash < 50.00) {
    				double losses = 50.00 - cash;
    				JOptionPane.showMessageDialog(this, "Du förlorade €" + losses );
    			}
    			clearHand();
    			initial = 0;
    			dinitial = 0;
    			cash = 50.00;
    			winLoss = 0.00;
    			total = 0;
    			dtotal = 0;
    			stats[0].setText("Totalt: €" + total);
    			betTotal = 0.00;
    			stats[1].setText("Total Satsning: €" + betTotal);
    			stats[3].setText("Pengar Kvar: €" + (cash));
    			stats[4].setText("Total V/F: €" + winLoss);
    		}
    	}
     
    	public void clearHand(){
    		for (int i = 0; i < 6; i++){
    			dface[i] = 0;
    			pface[i] = 0;
    			dvalue[i] = 0;
    			pvalue[i] = 0;
    			dsuit[i] = 0;
    			psuit[i] = 0;
    		}
    	}
     
    	public void getDSuit(){
    		String [] suit = new String [5];
    		for (int i = 0; i < 5; i++){
    			if (dsuit[i] == 1){
    				 suit[i] = "D";
    			 }
    			 if (dsuit[i] == 2){
    				 suit[i] = "C";
    			 }
    			 if (dsuit[i] == 3){
    				 suit[i] = "H";
    			 }
    			 if (dsuit[i] == 4){
    				 suit[i] = "S";
    			 }
    			 dCardImg[i] = suit[i] + dface[i] + ".jpg";
    		 }
    		 dealerHand();
    	 }
     
    	 public void getPSuit(){
    		 String [] suit = new String [5];
    		 for (int i = 0; i < 5; i++){
    		 	  if (dsuit[i] == 1){
    		 		   suit[i] = "D";
    		 	 }
    		 	  if (dsuit[i] == 2){
    		 		   suit[i] = "C";
    		 	 }
    		 	  if (dsuit[i] == 3){
    		 		   suit[i] = "H";
    		 	 }
    		 	  if (dsuit[i] == 4){
    		 		   suit[i] = "S";
    		 	 }
    			 pCardImg[i] = suit[i] + pface[i] + ".jpg";
    		 }
    		 playerHand();
    	 }
     
    	 public void playerHand(){
    		 pCards = new BufferedImage[5];
    		 for (int i = 0; i < 5; i++){
    			 try {
    				 pCards[i] = ImageIO.read(new File(pCardImg[i]));
    				 playCards[i] = new JLabel(new ImageIcon(pCards[i]));
    				 add (playCards[i]);
    				 repaint();
    			 } catch (IOException e) {
    			 }
    		 }
    	 }
     
    	 public void dealerHand(){
    		 dCards = new BufferedImage[5];
    		 for (int i = 0; i < 5; i++){
    		 	 try {
    		 	     dCards[i] = ImageIO.read(new File(dCardImg[i]));
    		 		 playCards[i] = new JLabel(new ImageIcon(dCards[i]));
    		 		 add (playCards[i]);
    		 		 repaint();
    			 } catch (IOException e) {
    			 }
    		 }
    	 }
     
    	 public void delay(long time){
    		 try{
    			 Thread.sleep(time);
    		 }
    		 catch(Exception e){
    		 }
    	 }
    	 public void paint(Graphics g){
    		 super.paint(g);
     
    		 if (pCards[0] != null){
    			 g.drawImage(pCards[0], 100,300, null);
    		 }
    		 if (pCards[1] != null){
    			 g.drawImage(pCards[1], 150,325, null);
    		 }
    		 if (pCards[2] != null){
    			 g.drawImage(pCards[2], 200,300, null);
    		 }
    		 if (pCards[3] != null){
    			 g.drawImage(pCards[3], 250,325, null);
    		 }
    		 if (pCards[4] != null){
    			 g.drawImage(pCards[4], 300,300, null);
    		 }
     
    		 if (dCards[0] != null){
    			 g.drawImage(dCards[0], 500,55, null);
    		 }
    		 if (dCards[1] != null){
    			 g.drawImage(dCards[1], 450,80, null);
    		 }
    		 if (dCards[2] != null){
    			 g.drawImage(dCards[2], 400,55, null);
    		 }
    		 if (dCards[3] != null){
    			 g.drawImage(dCards[3], 350,80, null);
    		 }
    		 if (dCards[4] != null){
    			 g.drawImage(dCards[4], 300,55, null);
    	     if (dCards[2] != null){
    			 g.drawImage(dCards[2], 400,55, null);
    		 }
    		 if (dCards[3] != null){
    			 g.drawImage(dCards[3], 350,80, null);
    		 }
    		 if (dCards[4] != null){
    			 g.drawImage(dCards[4], 300,55, null);
    		 }
    	 }
     }
    	}

    And here is the shuffler:

    import java.util.Random;
     
    public class Shuffle {
    	private String [] deck;
    	private int currentCard;
    	private int [] suit;
    	private int [] face;
    	private int value;
    	private Random mixCards = new Random();
    	private int deckSize = 52;
     
    	public Shuffle() {
    		face = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    		suit = new int[]{1, 2, 3, 4};
     
    		deck = new String[deckSize];
    		currentCard = 0;
     
    		for (int i = 0; i < deck.length; i++){
    			deck[i] = new String (face[i % 13] + " " + suit[i / 13]);
    		}
    	}
     
    	public void ShuffleCards(){
    		currentCard = 0;
     
    		for (int i = 0; i < deck.length; i++){
    			int f = mixCards.nextInt(deckSize);
     
    			String temp = deck[i];
    			deck[i] = deck[f];
    			deck[f] = temp;
    		}
    	}
     
    	public String Dealer(){
    		if (currentCard < deck.length){
     
    			return deck[currentCard++];
     
    		}
    		return null;
    	}
    }

    It's a pretty long code, but i thought it would be better to post it all, since i don't know exactly what u people might wan't see!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Could use some beginners tip here..

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1043)
    at java.awt.Container.add(Container.java:363)
    at Blackjack.<init>(Blackjack.java:141)
    At line 141 a variable has a null value. Look at line 141, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
    If you can not see which variable is null, use a println statement just before line 141 to print out the values of all the variables used on line 141.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Could use some beginners tip here..

    Thanks for the fast response. Yeah i tried to google a bit and that was the response i found everywhere. The problem i had for last hour is that i just can't track down what variable is null. Im just not that good of a programmer to find "broken" stuff. Any other tip u can give me? to point me more in the right direction

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Could use some beginners tip here..

    track down what variable is null.
    Here is the method I use:
    If you can not see which variable is null, add a println statement just before line 141 to print out the values of all the variables used on line 141.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Could use some beginners tip here..

    Ok, so on row 140 i should do:
    System.out.println(????)
    Because i don't know what the value of that variable is :S

    It's not my code, i'm just trying to learn

    --- Update ---

    This is row 141 in my code:

    sideButton.add(stats[i]);

    So i don't know what the value is.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Could use some beginners tip here..

    Add a println statement after line 140 that prints out the values of the three variables on that line:
    sideButton
    stats[i]
    i
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Could use some beginners tip here..

    And how do i figure out what the values of those 3 is

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Could use some beginners tip here..

    print them. For example:
    System.out.println("i="+i)
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Could use some beginners tip here..

    I just don't get it to work. Damn this is frustrating..

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Could use some beginners tip here..

    If you have errors, copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginners Java Help !
    By Cluelius in forum Loops & Control Statements
    Replies: 4
    Last Post: October 22nd, 2012, 01:24 PM
  2. HELP BEGINNERS
    By javabeginners in forum Java Theory & Questions
    Replies: 13
    Last Post: November 15th, 2011, 12:50 PM
  3. Re: HELP BEGINNERS
    By Olenger in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2011, 07:06 PM
  4. package for Beginners
    By god1gracious in forum Java Theory & Questions
    Replies: 3
    Last Post: October 4th, 2011, 09:24 PM
  5. YAY for beginners
    By derbya in forum Member Introductions
    Replies: 2
    Last Post: August 16th, 2011, 04:54 AM