• 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

Java error

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everybody,
I wonder cud anybody help me.I have a java application connected to the JdbcOdbc driver and using ms -access for database.My 'addTitle' class compiles with no errors and so when i run my main menu class and put information into the textfields i get this error--->
Add new Title
comboBoxChanged
comboBoxChanged
java.lang.NullPointerException
at addTitle.actionPerformed(addTitle.java:218)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
atADD TITLE
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5093)
at java.awt.Component.processEvent(Component.java:4890)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3598)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1585)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
which i think the error is in my code somewhere but i cant seem to see what the problem is.
Here is the code for 'addTitle':
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class addTitle extends JPanel implements ActionListener {
//Container content;
JPanel p1, custOpt, cards;
CardLayout cl;
JLabel newLabel;
JButton button=new JButton();
static JButton addT;
JComboBox cb12 = new JComboBox();
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox yearOfRel = new JComboBox();

String name,genre,cert,rating,yearOfRelease,synopsisOfVideo;
JTextField nameTF,certTF,genreTF,ratingTF,yearOfReleaseTF,synopsisOfVideoTF;


public addTitle(CardLayout cl, JPanel p) { //Constructor for Update Stock
setSize(500,500);
this.cl = cl;
cards = p;
p1 = new JPanel();
p1.setLayout(new GridLayout(0,2));

JLabel nameLabel = new JLabel("Name:");
JTextField nameTF = new JTextField(15);
p1.add(nameLabel);
p1.add(nameTF);

JLabel genreLabel = new JLabel("Genre:");
cb14.addItem("Action");
cb14.addItem("Adventure");
cb14.addItem("Comedy");
cb14.addItem("Crime");
cb14.addItem("Drama");
cb14.addItem("Family");
cb14.addItem("Fantasy");
cb14.addItem("Horror");
cb14.addItem("Musical");
cb14.addItem("Romance");
cb14.addItem("Sci-Fi");
cb14.addItem("Thriller");
cb14.addItem("War");
cb14.addItem("Western");
cb14.addItem("Other");
cb14.setMaximumRowCount(4);
p1.add(genreLabel);
p1.add(cb14);

JLabel certLabel = new JLabel("Certificate");
cb13.addItem("U");
cb13.addItem("PG");
cb13.addItem("12");
cb13.addItem("15");
cb13.addItem("18");
cb13.setMaximumRowCount(3);
p1.add(certLabel);
p1.add(cb13);

JLabel ratingLabel = new JLabel("Rating:");
cb12.addItem("*");
cb12.addItem("**");
cb12.addItem("***");
cb12.addItem("****");
cb12.addItem("*****");
cb12.setMaximumRowCount(3);
cb12.addActionListener(this);
p1.add(ratingLabel);
p1.add(cb12);
p1.add(new JScrollPane(cb12));



JLabel yearOfReleaseLabel = new JLabel("Year of Release");
yearOfRel.addItem("2003");
yearOfRel.addItem("2002");
yearOfRel.addItem("2001");
yearOfRel.addItem("2000");
yearOfRel.addItem("1999");
yearOfRel.addItem("1998");
yearOfRel.addItem("1997");
yearOfRel.addItem("1996");
yearOfRel.addItem("1995");
yearOfRel.addItem("1994");
yearOfRel.addItem("1993");
yearOfRel.addItem("1992");
yearOfRel.addItem("1991");
yearOfRel.addItem("1990");
yearOfRel.addItem("1989");
yearOfRel.addItem("1988");
yearOfRel.addItem("1987");
yearOfRel.addItem("1986");
yearOfRel.addItem("1985");
yearOfRel.addItem("1984");
yearOfRel.addItem("1983");
yearOfRel.addItem("1982");
yearOfRel.addItem("1981");
yearOfRel.addItem("1980");
yearOfRel.setMaximumRowCount(5);
yearOfRel.addActionListener(this);
p1.add(yearOfReleaseLabel);
p1.add(yearOfRel);


JLabel synopsisLabel = new JLabel("Synopsis of Video:");
JTextField synopsisOfVideoTF = new JTextField (20);
p1.add(synopsisLabel);
p1.add(synopsisOfVideoTF);

button = new JButton("Main Menu");
button.setActionCommand("pic");
button.addActionListener(this);
p1.add(button);

addT = new JButton("ADD TITLE");
addT.setActionCommand("ADD TITLE");
addT.addActionListener(this);
p1.add(addT);
connect();

add(p1);

}

public void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("Error creating class: "+e.getMessage());
e.printStackTrace();
return;
}
}
public void clear(){

nameTF.setText("");
genreTF.setText("");
certTF .setText("");
ratingTF.setText("");
yearOfReleaseTF.setText("");
synopsisOfVideoTF.setText("");
}
public void insert()
{ try
{
Connection conn = DriverManager.getConnection("jdbc dbc:acidmenuDatabase", "", "");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO TitleTable ( name, genre, cert,rating, yearOfRelease, synopsis ) Values(?,?,?,?,?,?)");
pstmt.clearParameters();
System.out.println(name);

pstmt.setString(1,name);
pstmt.setString(2,genre);
pstmt.setString(3,cert);
pstmt.setString(4,rating);
pstmt.setString(5,yearOfRelease);
pstmt.setString(6,synopsisOfVideo);
pstmt.executeUpdate();
System.out.println("a bit further");
pstmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
return;
}
}

public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
cl = (CardLayout)(cards.getLayout());
cl.show(cards,command );
System.out.println(command);
//connect();

if( command.equals("ADD TITLE"))
{
name = nameTF.getText();
System.out.println(name);
genre =(String)cb14.getItemAt(cb14.getSelectedIndex());
System.out.println(genre);
cert =(String)cb13.getItemAt(cb13.getSelectedIndex());
System.out.println(cert);
rating =(String)cb12.getItemAt(cb12.getSelectedIndex());
System.out.println(rating);
yearOfRelease =(String)yearOfRel.getItemAt(yearOfRel.getSelectedIndex());
System.out.println(yearOfRelease);
synopsisOfVideo = synopsisOfVideoTF.getText();
connect();
insert();
clear();
JOptionPane.showConfirmDialog( this, "Title has been Added To System", "ACID Systems", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE );
}
}
}

Does anybody know what it is cos i'm sick of trying to figure it out and getting nowhere.
Thanks alot ;-)
Donna
 
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
Hi Donna,
Welcome to Java Ranch!
Well, the stack trace says that the error is a NullPointerException at line 218 in this file, in the actionPerformed() method. A NullPointerException happens when you try to access an object via a null variable.
Now, you haven't shown us which line is #218, but if you did, then we could probably tell you what's wrong.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Iam guessing the error should be in the line ****if( command.equals("ADD TITLE"))*****.
Whenever a string is compared , It should be checked if it is not null.
try this ,
if(command != null && command.equals("ADDTITLE"))
Regards,
Haripriya
 
Ernest Friedman-Hill
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

Originally posted by haripriya rajesh:

Whenever a string is compared , It should be checked if it is not null.


Not if the string comes from a source that doesn't return null, as is the case here. The getActionCommand() method never returns null, so I doubt this is the problem.
Even if this were the problem, it's a lot nicer to simply write

which is shorter, clearer, and handles nulls automatically.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this 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