hello can you help me when i click the add button it will not add to the database.it will generate an error..can you help me please hoping for your positive responds...here's my code,please see my attachment....

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel.*;
import java.sql.*;



public class inqemployee extends JFrame implements ActionListener

{
	
	JLabel Lidno,Ldate;
	JLabel Lname,Llast,Lmiddle,Lposition;
	JPanel p1,p2,p3,p4; 
	JRadioButton Rmale,Rfemale;			
	JButton badd,bexit,bsearch;	
    JTextField Tposition,Tdate,Tname,Tlast,Tmiddle,Tidno;		
		
		
	Connection con;
	Statement st;
	PreparedStatement ps;	
	
	
	public inqemployee()
		
		
	{
		super("Entry of Employee");
		
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		
		  int w = getSize().width;
		  int h = getSize().height;
		  
		  int x = (dim.width-w)/3;
		  int y = (dim.height-h)/3;
		  	
		  setLocation(x,y); 	
		
		
	
		
		Container pane = getContentPane();
		
		pane.setLayout(new GridLayout(6,1));
		
		setSize(380,350);
		
		p1 = new JPanel();
		
		p1.setLayout(new FlowLayout());
	    
		
		
	  JTextField Tidno = new JTextField(10);
	  JTextField Tdate= new JTextField(10);	
	 
	  JTextField Tname = new JTextField(10);
	   JTextField Tlast = new JTextField(10);
	   JTextField Tmiddle= new JTextField(10);
	    Lidno = new JLabel("Id No.                                 ");	  
	  Ldate = new JLabel ("Date Hired");
	  Lname = new JLabel("First Name                ");
	  Llast = new JLabel("Last Name             ");  
	  Lmiddle= new JLabel("middle            ");
	  
	 
	   p1.add(Tidno);  
	   p1.add(Tdate);
	   p1.add(Lidno);
	   p1.add(Ldate);
	   
	    pane.add(p1);
	   
	   
	  p2 = new JPanel();
	  
	  p2.setLayout(new FlowLayout()); 
	   
	   
	  p2.add(Tname);
	  p2.add(Tlast);
	  p2.add(Tmiddle);
	  p2.add(Lname); 
	  p2.add(Llast);
	  p2.add(Lmiddle);
	
	  pane.add(p2);
	  
	
	   p3 = new JPanel();
		
	  p3.setLayout(new FlowLayout());
	  
	  
	  JTextField Tposition = new JTextField(10);
	  
	  
	  Rmale = new JRadioButton("male");
	  Rfemale = new JRadioButton("Female ");
	   Lposition = new JLabel("Position                                          ");
	  
	  p3.add(Tposition );
	  p3.add(Rmale);
	  p3.add(Rfemale);
	  pane.add(p3);   	
	  p3.add(Lposition);
	  
	  
	 p4 = new JPanel();
	 p4.setLayout(new FlowLayout());

	 // p4.setBorder(BorderFactory.createLineBorder(Color.red));
	    
	 
	 badd = new JButton("Add   "); 
	 badd.setMnemonic('A');
	 bexit= new JButton("Exit   ");
	 bexit.setMnemonic('X');
	 bsearch = new JButton("Search");
	 bsearch.setMnemonic('S');
	 	 
	

	p4.add(badd);
	p4.add(bexit);
	p4.add(bsearch);
	pane.add(p4);
		badd.addActionListener(this);
	bexit.addActionListener(this);
	
	
	  
	  setVisible(true);
	  
	 setDefaultCloseOperation(EXIT_ON_CLOSE);
	  
	  
	  try
	  {
	  	
	  		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con = DriverManager.getConnection("jdbc:odbc:eldenz");	
	  	
	  }
	  catch(ClassNotFoundException e) 
	  {
	  	JOptionPane.showMessageDialog(null,"Class not Found","Not Found",JOptionPane.WARNING_MESSAGE);
	  	
	  }
	  catch(SQLException e)
	  {
	  	JOptionPane.showMessageDialog(null,"Please Connect The Driver","Connect Sa Dong!",JOptionPane.WARNING_MESSAGE);
	  	
	  }
	   
	}
	 public void actionPerformed(ActionEvent e)
	  {
	  	
	  	Object source = null;
	  	
	 	source = e.getSource();
	  	
	  	
	  	if (source == badd)
	  		{
	  	
	  		
	  	 try
	  	  {
	  	 	st = con.createStatement();
	  	 	ps = con.prepareStatement("INSERT INTO Employee_tbl  " + " (idno,lastname,firstname,middle,position,datehired)" + " VAlUES(?,?,?,?,?,?)");
	  	 	
	  	 	ps.setString(1,Tidno.getText());
	  	 	ps.setString(2,Tname.getText());
	  	 	ps.setString(3,Tlast.getText());
	  	 	ps.setString(4,Tmiddle.getText());
	  	 	ps.setString(5,Tposition.getText());
	  	 	ps.setString(6,Tdate.getText());
	  	 	ps.executeUpdate();
	  	 	JOptionPane.showMessageDialog(null,"New record has been successfully added.","Record Added",JOptionPane.INFORMATION_MESSAGE);
	  	 	st.close();
	  	 
	  	  }	
	  	 catch(SQLException sqlex)
	  	    {
	  	 	
	  	 	JOptionPane.showMessageDialog(null,"Record have not been added");	
	  	 	
	  	     }
	    
	  	}	
	  		 
	  	   if(source== bexit)
	  	    {
	  	     	System.exit(0);
	  	    } 
	  	 
	  }
	  	
   	
		
		
	
	public static void main (String[] args) 

	{
		new inqemployee();
		
	}
	
	
	
}

what error? Where exactly?
Not going to read through hundreds of lines of code without at least a clear indication of what I'm looking for.

in my add button try to input data and then click the add button...it will generates null pointer exception...please help me on this..hoping for your positive responds.

That means that something isn't initialised properly before you call a method on it.
The exception will show what that is.

sir can you help me, i can't really know where is the problem,can you help me trace where is the problem..and correct my code...hoping for your positive responds.

er paste your exception here.. the null pointer exception stacktrace will show you the line there's a problem, and looking in the code, you'll see the variable on that line that's giving the problem.

eg.

Exception in thread "main" java.lang.NullPointerException
      at Main.displayStuff(main.java:11)
      at Main.main(main.java:17)

would tell you to look in the Main.java class in the method displayStuff(), line 11 of Main.java for the problem

Ok I quickly ran through your code

You have some problems, the null pointer is probably due to you declaring some of your variables at class level and at method level, then modifying 1 and expecting the other to be modified

eg.
You have a CLASS level JTextField called Tidno that is not initiated.

You have a METHOD level variable also called Tidno (in your constructor) that gets added to the panels.. you will see this textfield, but in your actionPerformed, you're accessing the CLASS level variable of the same name, that STILL isn't initialised.

In your constructor, change

JTextField Tidno = new JTextField(10);

to

Tidno = new JTextField(10);

and do this for all the variables you're doing the same to
(ps. naming convention, you should name your variables starting with a lowercase character .. ie Tidno should really be tidno, its just easier to read)

Edited 6 Years Ago by balmark: n/a

oki thanks for helping me...works great..more power to you...

This question has already been answered. Start a new discussion instead.