hello good people,
i am trying to make a small program that takes input from 4 text fields and adds them to the database(i am using mysql through wamp server) and some other database operations. my Exit and Clear buttons are working. I am using Swing class but have done the coding for components myself(haven't used drag and drop).

PORBLEM: The problem i am having is that when i insert the data and click on save button, the data is not inserted in the mysql database. the code seems to be right. i can't figure out the problem because it is NOT generating any exceptions.
this is what i gotta do:

 Develop a GUI based address book.

 A database should be used at the back end for insertion or retrieval of person’s
records.
 You can make a selection of any specific database in accordance with your own
convenience.
ajor Functionalities:

 Save Button:
 Should save the data entered by the user in the database.
 Delete Button:

 Search Button:
 The user should be able to search the records based on Name, id & Email or any
combination of these parameters.

 >> Button:
 Forward navigation

 << Button:
 Backward navigation

 Update Button:
 Enables the user to make changes within any existing record.

 Clear Button:
 Enables the user to clear/reset the fields of the address book, in case the user wants to
enter a new record.

 Exit Button:
 Enables the user to close the program
Hints:
 Make effective use of Scrollable & updatable ResultSet.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package projectactivity1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author Sana
 */
class test extends JFrame implements ActionListener{
static Connection link;
static Statement s;
static ResultSet results;
JTextField t1, t2, t3, t4;


    test(){
        //code for frame
     String msg = "";
     setSize(470,400);
     setBackground(Color.LIGHT_GRAY);
     setVisible( true );
     setTitle("My Address Book");
     System.out.println(getSize());
     setLayout(null);

//code for first row(name)
     JLabel name=new JLabel("Name");
     add(name);
     name.setBounds(50, 50, 70, 10);
     JTextField t1=new JTextField("");
     add(t1);
     t1.setBounds(150, 50, 250, 22); 

//code for 2 row(address)
     JLabel address=new JLabel("Address");
     add(address);
     address.setBounds(50, 80, 70, 10);
     t2=new JTextField("");
     add(t2);
     t2.setBounds(150, 80, 250, 25);

//code for 3 row (phone)
     JLabel phone=new JLabel("Phone");
     add(phone);
     phone.setBounds(50, 110, 70, 10);
     t3=new JTextField("");
     add(t3);
     t3.setBounds(150, 110, 250, 25);

//code for 4 row (email)
     Label email=new Label("E_mail");
     add(email);
     email.setBounds(50, 140, 70, 30);
     t4=new JTextField("");
     add(t4);
     t4.setBounds(150, 140, 250, 25);

//code for save JButton        
        JButton save=new JButton("Save");
        add(save);
        save.setBackground(Color.LIGHT_GRAY);
        save.setForeground(Color.BLACK);
        save.setBounds(30, 200, 80, 30);
        save.addActionListener(this);

//code for del JButton        
        JButton del=new JButton("Delete");
        add(del);
        del.setBackground(Color.LIGHT_GRAY);
        del.setForeground(Color.BLACK);
        del.setBounds(130, 200, 80, 30);
         del.addActionListener(this);

//code for clear JButton
        JButton clr=new JButton("Clear");
        add(clr);
        clr.setBackground(Color.LIGHT_GRAY);
        clr.setForeground(Color.BLACK);
        clr.setBounds(230, 200, 80, 30);
        clr.addActionListener(this);

//code for update JButton
        JButton updat=new JButton("Update");
        add(updat);
        updat.setBackground(Color.LIGHT_GRAY);
        updat.setForeground(Color.BLACK);
        updat.setBounds(330, 200, 80, 30);
        updat.addActionListener(this); 

//code for seaarch JButton        
        JButton sr=new JButton("Search");
        add(sr);
        sr.setBackground(Color.LIGHT_GRAY);
        sr.setForeground(Color.BLACK);
        sr.setBounds(30, 250, 80, 30);
        sr.addActionListener(this);

//code for forward JButton        
        JButton fr=new JButton(">>");
        add(fr);
        fr.setBackground(Color.LIGHT_GRAY);
        fr.setForeground(Color.BLACK);
        fr.setBounds(130, 250, 80, 30);
         fr.addActionListener(this);

//code for back JButton       
        JButton bk=new JButton("<<");
        add(bk);
        bk.setBackground(Color.LIGHT_GRAY);
        bk.setForeground(Color.BLACK);
        bk.setBounds(230, 250, 80, 30);
         bk.addActionListener(this);

//code for Exit JButton        
         JButton exit=new JButton("Exit");
         add(exit);
         exit.setBackground(Color.LIGHT_GRAY);
         exit.setForeground(Color.BLACK);
         exit.setBounds(330, 250, 80, 30);
         exit.addActionListener(this);        
    }


public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if(str.equals("save")) {
try {

Class.forName("com.mysql.jdbc.Driver"); //Step 1.
link = DriverManager.getConnection("jdbc:mysql://localhost:3306/address_book","root","sunny786"); //Step 2.
s = link.createStatement(); 
String sql_query = "INSERT INTO addbook VALUES ('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+t4.getText()+"')";
//this.actionPerformed(save);
s.executeUpdate(sql_query);
//if (sql_query == 0)
//System.out.println("\nUnable to insert record!");
link.close();
 } 
catch(Exception e){
e.printStackTrace();
}
}
else if (str.equals("del")){
//code for delete here
}
else if(str.equals("clr")) { 
// code for clear
}
else if(str.equals("updat")) { 
// code for update
}
else if(str.equals("sr")) { 
// code for search
}
else if(str.equals("bk")) { 
// code for back
}
else if(str.equals("fr")) { 
// code for fwd
}
else if(str.equals("exit")) {
System.exit(0);
}
} 
    }


public class ProjectActivity1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
     test t1 = new test();
    }
}

Any kind of help is greatly appreciated.
Thank you! :)

Could it be that the action command is "Save" (defaults to the same as the button text), but you are testing for equals("save") ?

commented: equalsIgnoreCase to the rescue :) +13

YES!! thanks i tried that but now it's generating some exception. (progress i guess)
Here is the exception.

java.lang.NullPointerException
`   at projectactivity1.test.actionPerformed(ProjectActivity1.java:140)`
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

i haven't set any of my DB columns to accept null. they all need to have values. Thanks for your help!

That's a classic error!
On line 24 you declare a text field t1
In your test method you have
JTextField t1=new JTextField("");
That creates a new variable t1, "masking" the original one, and initialises it.
When test finishes executing, that t1 goes out of scope, and the associated text field can be garbage collected.
The rest of the code then tries to use the original t1, but that has not been initalised, so it's still null.

oh wow that worked like a charm. i forgot to remove the 1 bit from there. thanks alot for your help and time :) its inserting data into database now!

Glad to help.
Please mark your threads "solved" for our knowledge base when you have the answer to your question. You can always start a new thread with an appropriate title if you have a new problem.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.