Results 1 to 6 of 6
- 02-17-2011, 04:38 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Simple swing program, help please!
Hi,
This is a simple swing program with just a label and a button.
When I click the button I want the label to change to "a".
But instead it throws a null pointer exception at runtime (compiles fine)
please help!
The program looks big because i am also learning GridBagLayout, I have bolded the line that I want to change.
Java Code:import java.awt.BorderLayout; public class Test extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Test frame = new Test(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Test() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); GridBagLayout gbl_contentPane = new GridBagLayout(); gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0}; gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0}; gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; contentPane.setLayout(gbl_contentPane); [COLOR="Red"][B]JLabel lblNewLabel = new JLabel("New label");[/B][/COLOR] GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0); gbc_lblNewLabel.gridx = 3; gbc_lblNewLabel.gridy = 2; contentPane.add(lblNewLabel, gbc_lblNewLabel); JButton btnNewButton = new JButton("New button"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { but1ActionPerformed(arg0); } }); GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); gbc_btnNewButton.insets = new Insets(0, 0, 0, 5); gbc_btnNewButton.gridx = 1; gbc_btnNewButton.gridy = 5; contentPane.add(btnNewButton, gbc_btnNewButton); } private javax.swing.JLabel lblNewLabel; private void but1ActionPerformed(ActionEvent arg0) { lblNewLabel.setText("a"); } }Last edited by N00Bie; 02-17-2011 at 04:42 PM.
- 02-17-2011, 04:44 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
You both defined a local variable in your constructor and as a member variable. You initialize that local variable but leave the member variable equal to null. Remove that local variable lblNewLabel from your constructor.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-17-2011, 05:02 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
if I remove this:
from the constructor I am getting more errors...JLabel lblNewLabel = new JLabel("New label");
- 02-17-2011, 05:06 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
-
What Jos is saying is don't declare the variable twice. Declare it once in the class:
This is variable declaration and initialization
And initialize it once by calling new XXX() once either in the class or the constructor.Java Code:MyFoo myFoo; // this is variable declaration myFoo = new MyFoo(); // this is variable initialization or construction MyFoo myFoo = new MyFoo(); // this is both variable initialization and declaration combined
Think about why this matters -- when you declare it again in the constructor as you're doing, you're creating a completely new variable, one that is completely distinct from the one declared in the class itself. The variable declared in the constructor is only visible within the constructor and does not exist outside of it.
Edit: Did not see Jos' reply. Yeah, what he said.
- 02-17-2011, 05:16 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
Please help with simple program.. Very simple.
By jonytek in forum New To JavaReplies: 7Last Post: 02-14-2011, 12:44 AM -
Simple program help
By jtyler in forum New To JavaReplies: 3Last Post: 09-20-2010, 07:43 AM -
Java Swing Simple Addition help
By dbasenoob in forum New To JavaReplies: 4Last Post: 04-27-2010, 01:26 PM -
simple program
By blastoff in forum New To JavaReplies: 5Last Post: 04-14-2010, 11:25 PM -
Java Swing JTable Simple Doubt
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 11-26-2008, 01:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks