IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Java Discussion :

Nullpointer avec repaint()


Sujet :

Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5
    Par défaut Nullpointer avec repaint()
    Bonjour,

    je debute avec Swing, et je suis en train de creer un jeu puissance 4.
    Mon probleme est que pour actualiser l'affichage graphique je dois minimiser la fenetre puis la remaximiser. Sinon ca ne s'affiche pas.

    Sur Eclipse, j'ai le probleme suivant

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at uk.ac.cam.nscc2.Connect4.Game.changeGrid(Game.java:71)
    	at uk.ac.cam.nscc2.Connect4.GamePanel.actionPerformed(GamePanel.java:106)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    	at java.awt.Component.processMouseEvent(Component.java:6108)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:5873)
    	at java.awt.Container.processEvent(Container.java:2105)
    	at java.awt.Component.dispatchEventImpl(Component.java:4469)
    	at java.awt.Container.dispatchEventImpl(Container.java:2163)
    	at java.awt.Component.dispatchEvent(Component.java:4295)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    	at java.awt.Container.dispatchEventImpl(Container.java:2149)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4295)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
    Merci d'avance

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    package uk.ac.cam.nscc2.Connect4;
     
    import javax.swing.JPanel;
     
    public class Game {
     
    	private Integer[][] grid= { {0,0,0,0,0,0,0},
    								{0,0,0,0,0,0,0},
    								{0,0,0,1,0,0,0},
    								{0,0,1,2,0,0,0},
    								{0,1,2,2,0,0,0},
    								{1,2,2,2,0,0,0} } ;
     
    	private GamePanel showgame;
    	private Player player1;
    	private Player player2;
     
    	public Game() {
     
    	}
     
    	public static String getPlayerName() {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
    	public void play() {
    		showgame.repaint(); // L'ERREUR EST LA
    	}
     
     
       public Integer[][] getGrid() {
    	   return grid ;
       }
     
       public GamePanel getGame() {
    	   return showgame;
       }
     
       public Player getPlayer1() {
    	   return player1;
       }
     
       public Player getPlayer2() {
    	   return player2;
       }
     
       public GamePanel getGraphics() {
    		return showgame;
    	}
     
    	public void setGraphics(GamePanel showgame) {
    		this.showgame = showgame;
    	}
     
    	public void changeGrid(int j) {
    		for (int i = 0 ; (i<6) ; i++) {
    			if (grid[i][j-1]==0) {
    				grid[i][j-1]=1; // Changer pour autre joueur
    				break;
    			}
    		}
    		showgame.repaint();
    	}
    Je sais que showgame ici n'a pas été initialisé, mais je ne vois pas comment faire d'autant que dans les examples d'interface swing que j'ai vu, il ne l'etait pas non plus?

    et l'autre classe

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
     
    package uk.ac.cam.nscc2.Connect4;
     
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
    public class GamePanel extends JPanel implements ActionListener{
     
    	private Game game ;
    	JButton button1;
    	JButton button2;
    	JButton button3 ;
    	JButton button4;
    	JButton button5;
    	JButton button6;
    	JButton button7; 
     
     
    	public GamePanel(Game game) {
    		super();
    		this.game = game;
    		//showgame = new GameGraphics(game);
    		//game.setGraphics(showgame);
    		FlowLayout layout = new FlowLayout();
    		//GridBagLayout layout = new GridBagLayout();
    		setLayout(layout);
    		//GridBagConstraints c = new GridBagConstraints();
    		button1 = new JButton("Here1");
    		//c.gridx =0;
    		//c.gridy=0;
    		add(button1);
    		button1.addActionListener(this);
    		button2 = new JButton("Here2");
    		//c.gridx=1;
    		//c.gridy=0;
    		add(button2);
    		button2.addActionListener(this);
    		button3 = new JButton("Here3");
    	//	c.gridx=2;
    		add(button3);
    		button3.addActionListener(this);
    		button4 = new JButton("Here4");
    		button4.addActionListener(this);
    	//	c.gridx=3;
    		add(button4);
    		button5 = new JButton("Here5");
    		button5.addActionListener(this);
    	//	c.gridx=4;
    		add(button5);
    		button6 = new JButton("Here6");
    	//	c.gridx=5;
    		add(button6);
    		button6.addActionListener(this);
    		button7 = new JButton("Here7");
    		//c.gridx=6;
    		button7.addActionListener(this);
    		add(button7);
    		//c.gridwidth = GridBagConstraints.REMAINDER ;
    		//c.gridheight = GridBagConstraints.REMAINDER;
    		//add(showgame,c);
     
    	}
     
     
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.blue);
    		g.fill3DRect(15, 70, 560, 480, true); //Size of board
    		g.setColor(Color.black);
    		//Each cell will be 80*80
    		for (int i= 0 ; i<6 ; i++) {
    			for (int j = 0 ; j<7 ; j++) {
    				if ((game.getGrid())[i][j]==1) g.setColor(Color.red);
    				if ((game.getGrid())[i][j]==2) g.setColor(Color.YELLOW);
    				if ((game.getGrid())[i][j]==0) g.setColor(Color.white);
    				g.fillOval(15 + 80*j, 70 + 80*i ,80, 80) ;
    			}
    		}
    	}
     
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		Object source = arg0.getSource();
    		if (source==button1) game.changeGrid(4); 
    		if (source==button2) game.changeGrid(2);
    		if (source==button3) game.changeGrid(3);
    		if (source== button4)game.changeGrid(4);
    		if (source== button5) game.changeGrid(5);
    		if (source==button6) game.changeGrid(6);
    		if (source==button7) game.changeGrid(7);
     
    	} 
     
    }

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 901
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 901
    Billets dans le blog
    54
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at uk.ac.cam.nscc2.Connect4.Game.changeGrid(Game.java:71)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    public void play() {
    		showgame.repaint(); // L'ERREUR EST LA
    	}
    Non.

    =>

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    	public void changeGrid(int j) {
    		for (int i = 0 ; (i<6) ; i++) {
    			if (grid[i][j-1]==0) {
    				grid[i][j-1]=1; // Changer pour autre joueur
    				break;
    			}
    		}
    		showgame.repaint();
    	}
    Et tu fais quand un showGame = new GamePanel() ?
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5
    Par défaut
    Je ne le fais pas. Sur le modele que j'utilisais, ils ne semblaient pas le faire (ce qui me semblait bizarre d'ailleurs :s)

    Mon problème est que je ne vois pas comment faire puisque le constructeur de GamePanel appelle lui meme la classe Game (new GamePanel(Game a)). Donc je ne vois pas comment fiare pour creer une instance de GamePanel à l'intérieur de Game.

    Sinon j'ai une autre classe Panel(qui etend JFrame) qui gere les autres composants

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
     
     
    public class Panel extends JFrame  {
     
    	private GamePanel gamePanel;
     
     public Panel() {
    	 super("Connect 4");
    	 setSize(1000, 600);
    	 setDefaultCloseOperation(EXIT_ON_CLOSE);
    	 setLayout(new BorderLayout());
    	 // Will display 1p, 2p and difficulty level
    	 JComponent optionsPanel = createOptionsPanel();
    	 add(optionsPanel, BorderLayout.WEST);
    	 // Will display the game
    	 JComponent gamePanel = gameDisplay();
    	 add(gamePanel, BorderLayout.CENTER);
    	 // will display statistics
    	 JComponent statPanel = giveStats();
    	 add(statPanel, BorderLayout.EAST);
     }
     
     
     
     private JComponent giveStats() {
    	 JPanel p = new JPanel();
    	 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)) ;
    	 p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    	 p.setBackground(Color.BLACK);
    	 p.setForeground(Color.white);
    	 JLabel player = new JLabel("Game.getPlayerName()"+ "'s turn ") ;
    	 player.setForeground(Color.white);
    	 JLabel timeelapsed = new JLabel("Time elapsed: " + Statistics.timeElapsed());
    	 timeelapsed.setForeground(Color.white);
    	 JLabel numberturns = new JLabel("Number of turns: " + Statistics.numberTurns());
    	 numberturns.setForeground(Color.white);
    	 JLabel predicted = new JLabel("The predicted winner is: " + Statistics.predictedWinner());
    	 predicted.setForeground(Color.white);
    	 JLabel numberhints = new JLabel("Number of hints given: " + Statistics.numberHints());
    	 numberhints.setForeground(Color.white);
    	 p.add(player);
         p.add(timeelapsed);
         p.add(numberturns);
         p.add(predicted);
         p.add(numberhints);
    	 return p;
    }
     
    private JComponent gameDisplay() {
    	Game play = new Game();
    	GamePanel game = new GamePanel(play);
    	game.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.RED));
        game.setBackground(Color.black);
        game.setMinimumSize(new Dimension(400,600));
        gamePanel = game;
        game.setVisible(true);
    	return game;
    }
     
    private JComponent createOptionsPanel() {
    	JPanel p = new JPanel();
    	p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    	p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    	p.setBackground(Color.BLACK);
    	p.setForeground(Color.white);
        JButton oneplayer = new JButton("1 player");
        p.add(oneplayer);
        JButton twoplayer = new JButton("2 players");
        p.add(twoplayer);
        JButton rules = new JButton("Rules of the Game");
        p.add(rules);
        JButton hints = new JButton("Hints");
        p.add(hints);
    	return p;
    }
     
     
     
    public static void main(String[] args) {
    	 Panel game = new Panel();
    	 game.setVisible(true);
    	 game.setResizable(false);
     }
     
     
     
     
     
     
    }
    Merci d'avance de votre aide

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 901
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 901
    Billets dans le blog
    54
    Par défaut
    showGame = new GamePanel(this) ?
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 5
    Par défaut
    Citation Envoyé par bouye Voir le message
    showGame = new GamePanel(this) ?
    Merci beaucoup, j'ai maintenant résolu mon probleme (en effacant le showgame.repaint() et en mettant un repaint() dans la classe GamePanel au niveau de l'event handler(desolee je ne connais pas le mot francais).

    Mais en tout cas merci pour l'aide

Discussions similaires

  1. erreur NULLpointer avec CardLayout
    Par zangaloni dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 12/05/2009, 18h49
  2. Réponses: 8
    Dernier message: 14/01/2009, 13h41
  3. Problème avec repaint() et Thread.sleep
    Par fab13 dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 12/08/2008, 23h02
  4. Réponses: 4
    Dernier message: 07/01/2008, 23h48
  5. Probleme avec repaint()
    Par LinuxUser dans le forum AWT/Swing
    Réponses: 15
    Dernier message: 02/01/2008, 00h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo