Aller au contenu
View in the app

A better way to browse. Learn more.

Next

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Résolu][Java] NullPointerException

Featured Replies

Posté(e)

bonjour j'ai besoin d'aide avec un bou de code dans un programme:

/**
* @(#)Poker_main.java
*
*
* @author 
* @version 1.00 2007/7/14
*/

import java.util.Random; 
public class Poker {
Player[] Players;
boolean[][] cards;
//Poker Game;

/**
    * Creates a new instance of <code>Poker</code>.
    */
   public Poker(String[] n) {
   	Player[] Players = new Player[n.length];
   	for(int i=0; i<n.length; i++) {
   		Players[i] = new Player(n[i]);
      	}
   }

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
       // TODO code application logic here
       if(args.length <= 0) System.out.println("No players specified.");
       else {
       	Poker Game = new Poker(args);
       	startRound();
       }
   }

   public void startRound() {
   	initCardDeck();
   	System.out.println(Players.length);
   	for(int i=0; i<Players.length; i++) {
   		giveHands();
      	}
   }

   public void initCardDeck() {
   	cards = new boolean[13][3];
   	for(int i=0; i<Players.length; i++){
   		Players[i].cards = new int[2][2];
   	}
   }

   public void giveHands() {
   	Random r = new Random();
   	for(int i=0; i<Players.length; i++)
   	{
   		for(int j=0; j<2; j++){
    		int valeur = 0 + r.nextInt(13 - 0 + 1);
			int couleur = 0 + r.nextInt(3 - 0 + 1);
			while(cards[valeur][couleur]) {
				valeur = 0 + r.nextInt(13 - 0 + 1);
				couleur = 0 + r.nextInt(3 - 0 + 1);
			}
			Players[i].cards[j][0]=valeur;
			Players[i].cards[j][1]=couleur;
			cards[valeur][couleur]=true;
    	}
   	}
   	System.out.println("Hands: " + Players[1].cards);
   	System.out.println("Hands: " + Players[2].cards);
   }
}

quand je l'execute cela me donne le message:

Exception in thread "main" java.lang.NullPointerException
   at Poker.initCardDeck(Poker.java:47)
   at Poker.startRound(Poker.java:38)
   at Poker.main(Poker.java:33)

si quelqu'un pourrait m'aider je serait très reconnaissant, merci beaucoup :)

Posté(e)

Déjà pour moi y'a ça qui ne va pas :

Poker Game = new Poker(args);
startRound();

Tu appelles startRound(), qui n'est pas une méthode static, sur aucun objet.

tu devrais essayer "Game.startRound();"

Après, pour ton NullPointerException, si il ne vient pas de là, il va falloir que tu nous forunisses ta class Player.

Posté(e)
  • Auteur

vi j'ai déja essayer de mettre Game.startRound(); mais ça ne fontionne toujours pas ...

voici la classe player:

public class Player {
String name;
int bank;
int[][] cards;

   public Player(String n) {
   	name=n;
   	bank=1000;
   	System.out.println("Created player " + name + " with a bank of " + bank);
   }
   void setBank(int a) {
   	bank+=a;
   	System.out.println(name + "'s bank is now at " + bank);
   }

}

Modifié par DeKay

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.