By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

Like if there's a ghost in my program :) - Objects are created later

P: 3
There's something strange about it.
In my InitGui() methode he thinks my game object still has null values but in my actionperformed method I can use the game object.
Expand|Select|Wrap|Line Numbers
  1. public class FightJPanel extends javax.swing.JPanel {
  2.     private TestFrame main;
  3.     private HeroQuest game;
  4.     private JLabel jLabelHero;
  5.     private JButton jButtonDice;
  6.  
  7.     public FightJPanel(TestFrame frame, HeroQuest hero) {
  8.         super();
  9.         main = frame;
  10.         game = hero;
  11.         initGui();
  12.  
  13.  
  14.     }
  15.  
  16.     private void initGui() 
  17.     {
  18.         game.getType(); // THIS DOESN'T WORK, IT GIVES A NULL //POINTEREXCEPTION
  19.         try {
  20.             //SOME LAYOUT STUFF WAS HERE
  21.             {
  22.                 jButtonDice = new JButton();
  23.                 this.add(jButtonDice, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  24.                 jButtonDice.setPreferredSize(new java.awt.Dimension(91, 55));
  25.                 jButtonDice.setText("roll dice");
  26.                 jButtonDice.setBorder(new LineBorder(new java.awt.Color(0,0,0), 4, true));
  27.                 jButtonDice.setBackground(new java.awt.Color(255,255,255));
  28.                 jButtonDice.addActionListener(new ActionListener() {
  29.                     public void actionPerformed(ActionEvent evt) {
  30.                         jButtonDiceActionPerformed(evt);
  31.                     }
  32.                 });
  33.             }
  34.             {
  35.                 jLabelHero = new JLabel();
  36.                 this.add(jLabelHero, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  37.                 //jLabelHero.setText(game.getActionPoints())> THIS //DOESNT WORK ASWELL > NULLPOINTER EXCEPTION;
  38.                 jLabelHero.setOpaque(true);
  39.                 jLabelHero.setBackground(Color.WHITE);
  40.                 jLabelHero.setForeground(Color.BLACK);
  41.  
  42.                 jLabelHero.setPreferredSize(new java.awt.Dimension(58, 20));
  43.             }
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.  
  48.     }
  49.  
  50.     private void jButtonDiceActionPerformed(ActionEvent evt) {
  51.         jButtonDice.setText(game.getType());//GUESS WHAT IT WORKS HERE 
  52.  
  53.  
  54.  
  55.     }
  56.  
  57.  
  58.  
  59. }
Apr 30 '07 #1
Share this Question
Share on Google+
6 Replies


Expert 10K+
P: 11,448
There's something strange about it.
In my InitGui() methode he thinks my game object still has null values but in my actionperformed method I can use the game object.
It might seem a lame answer, but what happens if you rename your 'initGUI'
method to, say, 'initMyGUI'?

kind regards,

Jos
Apr 30 '07 #2

P: 3
It might seem a lame answer, but what happens if you rename your 'initGUI'
method to, say, 'initMyGUI'?

kind regards,

Jos
That doesn't work either :)
Apr 30 '07 #3

Expert 10K+
P: 11,448
That doesn't work either :)
Then print out the value of the 'hero' parameter in your constructor and see
what gets printed.

kind regards,

Jos
Apr 30 '07 #4

P: 3
Then print out the value of the 'hero' parameter in your constructor and see
what gets printed.

kind regards,

Jos
It prints out the right values...
Apr 30 '07 #5

Expert 10K+
P: 11,448
It prints out the right values...
Ok, we're gonna find that little rascal. Now print out the value of your 'game'
variable at the top of your initGui() method. Nothing had happened in between
with your variable so we must've cornered it now ;-)

kind regards,

Jos
Apr 30 '07 #6

10K+
P: 13,264
There's something strange about it.
In my InitGui() methode he thinks my game object still has null values but in my actionperformed method I can use the game object.
Expand|Select|Wrap|Line Numbers
  1. public class FightJPanel extends javax.swing.JPanel {
  2.     private TestFrame main;
  3.     private HeroQuest game;
  4.     private JLabel jLabelHero;
  5.     private JButton jButtonDice;
  6.  
  7.     public FightJPanel(TestFrame frame, HeroQuest hero) {
  8.         super();
  9.         main = frame;
  10.         game = hero;
  11.         initGui();
  12.  
  13.  
  14.     }
  15.  
  16.     private void initGui() 
  17.     {
  18.         game.getType(); // THIS DOESN'T WORK, IT GIVES A NULL //POINTEREXCEPTION
  19.         try {
  20.             //SOME LAYOUT STUFF WAS HERE
  21.             {
  22.                 jButtonDice = new JButton();
  23.                 this.add(jButtonDice, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  24.                 jButtonDice.setPreferredSize(new java.awt.Dimension(91, 55));
  25.                 jButtonDice.setText("roll dice");
  26.                 jButtonDice.setBorder(new LineBorder(new java.awt.Color(0,0,0), 4, true));
  27.                 jButtonDice.setBackground(new java.awt.Color(255,255,255));
  28.                 jButtonDice.addActionListener(new ActionListener() {
  29.                     public void actionPerformed(ActionEvent evt) {
  30.                         jButtonDiceActionPerformed(evt);
  31.                     }
  32.                 });
  33.             }
  34.             {
  35.                 jLabelHero = new JLabel();
  36.                 this.add(jLabelHero, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  37.                 //jLabelHero.setText(game.getActionPoints())> THIS //DOESNT WORK ASWELL > NULLPOINTER EXCEPTION;
  38.                 jLabelHero.setOpaque(true);
  39.                 jLabelHero.setBackground(Color.WHITE);
  40.                 jLabelHero.setForeground(Color.BLACK);
  41.  
  42.                 jLabelHero.setPreferredSize(new java.awt.Dimension(58, 20));
  43.             }
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.  
  48.     }
  49.  
  50.     private void jButtonDiceActionPerformed(ActionEvent evt) {
  51.         jButtonDice.setText(game.getType());//GUESS WHAT IT WORKS HERE 
  52.  
  53.  
  54.  
  55.     }
  56.  
  57.  
  58.  
  59. }
How are you running the whole thing?
Apr 30 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.