K
Kevin
All,
I have created two classes as part of a school project and when I
run the main application and click on one of my interface components
(a JButton) I get an NullPointerExceptionError. Basically, I used one
of the classes to create a user menus (AppToolBar.java, first class
below) that creates a JToolBar that allows an individual to click a
button to execute a function. I then put in a call to a method in
another class as a test to see if the actionlistener in AppToolBar
will in fact invoke a method in the other class (UserView.java which
contains methods for manipulating user input, second class below.
Like I said, when I click on the delete button, I get the error,
but when I click on the insert of update button I don't. I realize
that the error is somehow related to the fact that I am calling the
method from another class. Did I do the contructor correctly in the
AppToolBar class? Bottomline, I am having problems creating objects
within the classes and then using the object reference to call the
methods of the object's class. Any help that anybody might be able to
provide would be greatly appreciated. Thanks.
Kevin
(e-mail address removed)
//AppToolBar Class
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class AppToolBar extends JToolBar {
protected UserView userView;
public AppToolBar( UserView userView ) {
this.userView = userView;
JButton insertButton = new JButton( "Insert" );
insertButton.addActionListener (
new ActionListener() {
public void actionPerformed ( ActionEvent event )
{
//invoked methods go here
insertVideo();
}
}//End ActionListener
);//End insertButton listeners and methods
JButton deleteButton = new JButton( "Delete" );
deleteButton.addActionListener (
new ActionListener() {
public void actionPerformed ( ActionEvent event )
{
deleteVideo();
}
}//End ActionListener
);//End deleteButton listeners and methods
JButton updateButton = new JButton( "Update" );
updateButton.addActionListener (
new ActionListener() {
public void actionPerformed ( ActionEvent event )
{
updateVideo();
}
}//End ActionListener
);//End updateButton listeners and methods
setLayout( new GridLayout( 1, 3 ));
add( insertButton );
add( deleteButton );
add( updateButton );
};//End AppToolBar constructor
//This method calls the methods for inserting a record into
//the video table.
public void insertVideo()
{
JOptionPane.showMessageDialog(null, "Insert Function",
"This is a test of the insert button.",
JOptionPane.INFORMATION_MESSAGE);
}
//This method calls the methods for updating the video table.
public void updateVideo()
{
JOptionPane.showMessageDialog(null, "Update Function",
"Test Update Button", JOptionPane.INFORMATION_MESSAGE );
}
//This method calls the methods for deleting a video from
//the video table. This deletion is based on the primary
//key of the video object (video name).
public void deleteVideo()
{
//JOptionPane.showMessageDialog(null, "Delete Function",
//"Test Delete Button", JOptionPane.INFORMATION_MESSAGE );
userView.processDeleteInput();
}
}//End AppToolBar class
//UserView class
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class UserView extends JToolBar {
JTextField VideoName, Type, Year, Price, Quantity;
String name;
String type;
String year;
int quantity;
double price;
public UserView() {
JTextField VideoName = new JTextField( 30 );
JLabel VLabel = new JLabel( "Video Name" );
JTextField Type = new JTextField( 15 );
JLabel TLabel = new JLabel( "Type" );
JTextField Year = new JTextField( 4 );
JLabel YLabel = new JLabel( "Year" );
JTextField Price = new JTextField( 8 );
JLabel PLabel = new JLabel( "Price" );
JTextField Quantity = new JTextField( 5 );
JLabel QLabel = new JLabel( "Quantity" );
//Set layout of userView JPanel and add components
setLayout( new GridLayout( 5, 2 ));
add( VLabel );
add( VideoName );
add( TLabel );
add( Type );
add( YLabel );
add( Year );
add( PLabel );
add( Price );
add( QLabel );
add( Quantity );
};//End userView constructor
public void processInsertInput()
{
String name = VideoName.getText();
String type = Type.getText();
String year = Year.getText();
int quantity = Integer.parseInt( Quantity.getText() );
double price = Double.parseDouble( Price.getText() );
if ( name == "" || type == "" || year == "" || Price.getText() ==
"" || Quantity.getText() == "" )
{
//Display an error message if any of the input fields are blank
//as all information is required to add a record to the video
table.
JOptionPane.showMessageDialog( null, "Please all of the information
for the video and click insert again.", "Insert Operation Failed",
JOptionPane.INFORMATION_MESSAGE );
}
else
{
//1. Code to create a video object.
//2. Call to method to add vidoe object to video table.
//3. fire tablemodelchanged type function to update the video table
display?
//4. Display following message.
//JOptionPane.showMessageDialog( null, "This video has been added
to the store database",
//"Insert Operation Successful", JOptionPane.INFORMATION_MESSAGE );
}
}//End method processInsertInput
public void processDeleteInput()
{//Begin processDeleteInput method
if ( VideoName.getText() == "" )//Check to see if a primary key
(Video Name) has been entered
{
JOptionPane.showMessageDialog( null, "Please enter the name of the
video to be deleted.", "Delete Operation Failed",
JOptionPane.INFORMATION_MESSAGE );
}//End if statement.
else
{//Begin else statement.
//1. Code to find the record to deleted.
//2. Call to function to delete row here.
//3. Fire tablemodelchanged type function to update the video table
display?
//4. Display following message.
//JOptionPane.showMessageDialog( null, "This video has been deleted
from the store database",
//"Delete Operation Successful", JOptionPane.INFORMATION_MESSAGE );
}//End else statement
}//End processDeleteInput function.
public void processUpdateInput()
{
}
}//End UserView class
I have created two classes as part of a school project and when I
run the main application and click on one of my interface components
(a JButton) I get an NullPointerExceptionError. Basically, I used one
of the classes to create a user menus (AppToolBar.java, first class
below) that creates a JToolBar that allows an individual to click a
button to execute a function. I then put in a call to a method in
another class as a test to see if the actionlistener in AppToolBar
will in fact invoke a method in the other class (UserView.java which
contains methods for manipulating user input, second class below.
Like I said, when I click on the delete button, I get the error,
but when I click on the insert of update button I don't. I realize
that the error is somehow related to the fact that I am calling the
method from another class. Did I do the contructor correctly in the
AppToolBar class? Bottomline, I am having problems creating objects
within the classes and then using the object reference to call the
methods of the object's class. Any help that anybody might be able to
provide would be greatly appreciated. Thanks.
Kevin
(e-mail address removed)
//AppToolBar Class
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class AppToolBar extends JToolBar {
protected UserView userView;
public AppToolBar( UserView userView ) {
this.userView = userView;
JButton insertButton = new JButton( "Insert" );
insertButton.addActionListener (
new ActionListener() {
public void actionPerformed ( ActionEvent event )
{
//invoked methods go here
insertVideo();
}
}//End ActionListener
);//End insertButton listeners and methods
JButton deleteButton = new JButton( "Delete" );
deleteButton.addActionListener (
new ActionListener() {
public void actionPerformed ( ActionEvent event )
{
deleteVideo();
}
}//End ActionListener
);//End deleteButton listeners and methods
JButton updateButton = new JButton( "Update" );
updateButton.addActionListener (
new ActionListener() {
public void actionPerformed ( ActionEvent event )
{
updateVideo();
}
}//End ActionListener
);//End updateButton listeners and methods
setLayout( new GridLayout( 1, 3 ));
add( insertButton );
add( deleteButton );
add( updateButton );
};//End AppToolBar constructor
//This method calls the methods for inserting a record into
//the video table.
public void insertVideo()
{
JOptionPane.showMessageDialog(null, "Insert Function",
"This is a test of the insert button.",
JOptionPane.INFORMATION_MESSAGE);
}
//This method calls the methods for updating the video table.
public void updateVideo()
{
JOptionPane.showMessageDialog(null, "Update Function",
"Test Update Button", JOptionPane.INFORMATION_MESSAGE );
}
//This method calls the methods for deleting a video from
//the video table. This deletion is based on the primary
//key of the video object (video name).
public void deleteVideo()
{
//JOptionPane.showMessageDialog(null, "Delete Function",
//"Test Delete Button", JOptionPane.INFORMATION_MESSAGE );
userView.processDeleteInput();
}
}//End AppToolBar class
//UserView class
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class UserView extends JToolBar {
JTextField VideoName, Type, Year, Price, Quantity;
String name;
String type;
String year;
int quantity;
double price;
public UserView() {
JTextField VideoName = new JTextField( 30 );
JLabel VLabel = new JLabel( "Video Name" );
JTextField Type = new JTextField( 15 );
JLabel TLabel = new JLabel( "Type" );
JTextField Year = new JTextField( 4 );
JLabel YLabel = new JLabel( "Year" );
JTextField Price = new JTextField( 8 );
JLabel PLabel = new JLabel( "Price" );
JTextField Quantity = new JTextField( 5 );
JLabel QLabel = new JLabel( "Quantity" );
//Set layout of userView JPanel and add components
setLayout( new GridLayout( 5, 2 ));
add( VLabel );
add( VideoName );
add( TLabel );
add( Type );
add( YLabel );
add( Year );
add( PLabel );
add( Price );
add( QLabel );
add( Quantity );
};//End userView constructor
public void processInsertInput()
{
String name = VideoName.getText();
String type = Type.getText();
String year = Year.getText();
int quantity = Integer.parseInt( Quantity.getText() );
double price = Double.parseDouble( Price.getText() );
if ( name == "" || type == "" || year == "" || Price.getText() ==
"" || Quantity.getText() == "" )
{
//Display an error message if any of the input fields are blank
//as all information is required to add a record to the video
table.
JOptionPane.showMessageDialog( null, "Please all of the information
for the video and click insert again.", "Insert Operation Failed",
JOptionPane.INFORMATION_MESSAGE );
}
else
{
//1. Code to create a video object.
//2. Call to method to add vidoe object to video table.
//3. fire tablemodelchanged type function to update the video table
display?
//4. Display following message.
//JOptionPane.showMessageDialog( null, "This video has been added
to the store database",
//"Insert Operation Successful", JOptionPane.INFORMATION_MESSAGE );
}
}//End method processInsertInput
public void processDeleteInput()
{//Begin processDeleteInput method
if ( VideoName.getText() == "" )//Check to see if a primary key
(Video Name) has been entered
{
JOptionPane.showMessageDialog( null, "Please enter the name of the
video to be deleted.", "Delete Operation Failed",
JOptionPane.INFORMATION_MESSAGE );
}//End if statement.
else
{//Begin else statement.
//1. Code to find the record to deleted.
//2. Call to function to delete row here.
//3. Fire tablemodelchanged type function to update the video table
display?
//4. Display following message.
//JOptionPane.showMessageDialog( null, "This video has been deleted
from the store database",
//"Delete Operation Successful", JOptionPane.INFORMATION_MESSAGE );
}//End else statement
}//End processDeleteInput function.
public void processUpdateInput()
{
}
}//End UserView class