Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Null pointer exception error

843854Feb 8 2004 — edited Feb 9 2004
I'm not sure if this has anything to do with my database but I have a feeling that it does. I made a 'submit' button that will put data into MySQL, my program compiles and runs fine but everytime I click the submit button it gives me a null pointer exception error and the data wont be put into the database. any help would be much appreciated.
heres my code, the submit button implementation is at the bottom.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Dbase extends JFrame
{
	static JTextField tName, tComp, tSpec, tItems;
	static JLabel name, comp, spec, items;
	static JButton submit, clear, modify, reset, close;
	static JPanel panel1, panel2;
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DATABASE_URL = "jdbc:mysql://localhost/Employee?user=mike&password=blitz84";
	private Connection conn;
	private Statement stat;
	Container container = getContentPane();
	//container.setLayout(new FlowLayout());
	public void init()
	{
		getContentPane().setLayout(new FlowLayout());
		name = new JLabel("Name");
		comp = new JLabel("Company");
		spec = new JLabel("Specialization");
		items = new JLabel("Items");
		tName = new JTextField(10);
		tComp = new JTextField(10);
		tSpec = new JTextField(10);
		tItems = new JTextField(10);
		submit = new JButton("Submit");
		clear = new JButton("Clear");
		modify = new JButton("Modify");
		reset = new JButton("Reset");
		close = new JButton("Close");
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(4,2));
		panel1.add(name);
		panel1.add(tName);
		panel1.add(comp);
		panel1.add(tComp);
		panel1.add(spec);
		panel1.add(tSpec);
		panel1.add(items);
		panel1.add(tItems);
		container.add(panel1);
		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(1,5));
		panel2.add(submit);
		panel2.add(clear);
		panel2.add(modify);
		panel2.add(reset);
		panel2.add(close);
		container.add(panel2);
		ButtonHandler handler = new ButtonHandler();
		submit.addActionListener(handler);
		clear.addActionListener(handler);
		modify.addActionListener(handler);
		reset.addActionListener(handler);
		close.addActionListener(handler);
		setSize(375,150);
		setVisible(true);
	}
	public static void main(String args[])
	{
		Dbase go = new Dbase();
		go.init();
		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	private class ButtonHandler implements ActionListener 
	{
		public void actionPerformed(ActionEvent ae)
		{
			if (ae.getActionCommand() == "Submit")
			{
				String name = tName.getText();  
				String comp = tComp.getText();
				String spec = tSpec.getText();
				String items = tItems.getText();
				try
				{
					stat = conn.createStatement();
					String query = "insert into Emp values("+name+","+comp+","+spec+","+items+")";
					stat.executeUpdate(query);
					stat.close();
				}
				catch(Exception e)
				{
					System.out.println("Error at "+e);
				}
			}
			
			
			
			
			
		}
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 8 2004
Added on Feb 8 2004
5 comments
843 views