wood burning stoves 2.0*
The moose likes Java in General and the fly likes Null Pointer Exception on ActionPerformes (may be a beginner question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null Pointer Exception on ActionPerformes (may be a beginner question)" Watch "Null Pointer Exception on ActionPerformes (may be a beginner question)" New topic
Author

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

elizabeth thomas
Greenhorn

Joined: May 12, 2003
Posts: 7
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

Joined: Apr 24, 2002
Posts: 137
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 ]

use the [CODE] tags - it makes it much easier for people to help you.
elizabeth thomas
Greenhorn

Joined: May 12, 2003
Posts: 7
Thanks a lot. It was exactly the problem. I constructed the class before the output is instantiatedint the main application
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Null Pointer Exception on ActionPerformes (may be a beginner question)