• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception on ActionPerformes (may be a beginner question)

 
elizabeth thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to Java and any help will be appreciated.
The main application calls this ContolPanel class together with another class. When I try to click on the help button,I get a NullPonter Exception ("exception occured while dispatching the event").
Could anybody help e please!!!
package database_search_update_and_add;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class ContolPanel extends JPanel {
private GridLayout gridLayout1 = new GridLayout();
public JTextArea output;

public ContolPanel( JTextArea t) {
try {
output =t;
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {

this.setLayout(gridLayout1);
Help.setText("Help");
Help.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Help_actionPerformed(e);
}
});
this.add(Help, null);
}
void Help_actionPerformed(ActionEvent e) {
output.append ("\nClick");
}
}
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! I'm not entirely sure what is causing the error because it looks like some code is missing (for instance, Help is not instantiated or declared in the code you posted). However, a good place to look is at the variable "output". In java, when you declare a reference as a class member but don't instantiated it, it gets the null value. So, at the line:
public JTextArea output;
the reference "output" points to null. If you declared Help as a JButton (public JButton Help , the same thing will happen the first time you try to call a method on Help.
Change the declaration to this:
public JTextArea output = new JTextArea();
public JButton Help = new JButton();
Also, check to make sure that in your constructor that the variable "t" is not null before you assign it to "output".
hope that helps. Also, if you click on the UBB button "Code" when posting, and paste your source code between the tags, it will preserve your indentation.
[ June 12, 2003: Message edited by: Jon Dornback ]
 
elizabeth thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. It was exactly the problem. I constructed the class before the output is instantiatedint the main application
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic