Help With NullPointerExceptionError

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
 
K

Krmathieu

By "all" I meant anybody listening, not all as in all newsgroups. At any rate,
did you happen to see what I was doing wrong? I noticed that you said that I
could find your answer at c.l.j.help. What is that? I would be very interested
in any help that you might be able to provide. Thanks!

Kevin
 
P

Paul Lutus

Krmathieu said:
By "all" I meant anybody listening, not all as in all newsgroups. At any
rate, did you happen to see what I was doing wrong? I noticed that you
said that I could find your answer at c.l.j.help. What is that?

"What is that"? You mean you don't even remember where you have already
multiposted a copy of an identical message?

Without first trying to find out how Usenet works, you have posted multiple
identical messages in multiple groups. You have exhibited all attitude, no
gratitude, and no clue.

Good luck with your life.
 
D

D

This is not the answer to your problem, but there are several places
in your program where you do something like this:

String name;
if(name == "")
....

When comparing objects, including String objects, you need to use
name.equals(""), or even better, "".equals(name). In general, use ==
for primatives only. The == for objects does not mean does this string
have the same contents as another string, but are they EXACTLY the
same object i.e. are they stored in the same bit of memory.
 
S

Stefan Schulz

This is not the answer to your problem, but there are several places
in your program where you do something like this:

String name;
if(name == "")
...

When comparing objects, including String objects, you need to use
name.equals(""), or even better, "".equals(name). In general, use ==
for primatives only. The == for objects does not mean does this string
have the same contents as another string, but are they EXACTLY the
same object i.e. are they stored in the same bit of memory.

Also, be careful with primitives of type float or double. Those
have the nasty habit of not being equal when you'd expect them to
be. To verify if two floating-point numbers are "equal" use something
like (Math.abs(a - b) < SMALL_CONSTANT)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top