• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why does this code fail?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ne ideas on why this snippet fails to when I click on any of the buttons?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoButtons {
JFrame frame;
JLabel label;
public static void main ( String [] args ) {
TwoButtons gui = new TwoButtons();
gui.go();
}
public void go () {
JFrame frame = new JFrame();
JButton labelButton = new JButton( "change label" );
labelButton.addActionListener( new LabelListener() );
JButton colorButton = new JButton( "change colours" );
colorButton.addActionListener( new ColorListener() );
JLabel label = new JLabel( "I am a label" );
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add( BorderLayout.SOUTH, colorButton );
frame.getContentPane().add( BorderLayout.EAST, labelButton );
frame.getContentPane().add( BorderLayout.CENTER, drawPanel );
frame.getContentPane().add( BorderLayout.WEST, label );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 420, 300 );
frame.setVisible( true );
}
class LabelListener implements ActionListener {
public void actionPerformed( ActionEvent event ) {
label.setText("Ouch" );
}
}
class ColorListener implements ActionListener {
public void actionPerformed( ActionEvent event ) {
frame.repaint();
}
}
}
class MyDrawPanel extends JPanel {
public void paintComponent( Graphics g ) {
g.fillRect(0,0,this.getWidth(), this.getHeight());
// make random colors to fill with
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color randomColor = new Color(red, green, blue);
g.setColor(randomColor);
g.fillOval(70,70,100,100);
}
}
I am getting a NullPointerException
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got both a local variable and a member variable named "label" and "frame". The ones that get initialized in go() are the local ones in that routine. The member variables never get initialized, so they're null, and wheh the actionListeners try to access them, you'll get an NPE.
If you don't understand this, then please take a step back and learn some of the fundamentals of Java; try reading through the first four lessons of this tutorial.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two instance variables, "JFrame frame" and "JLabel label" that are overriden in the "go()" method. The two instance variables are never used and hence are null when you get to the action listeners. You shouldn't re-define them in the "go()" method.
This is what you want:
 
deep venu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gee! thanx.. was playing around with this code a lot and didn't realize the mistake. Thanx lot.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic