I'm new to Java and have been given a project to create a form using swing and to save and call up the data inputed. I have currently created my interface and have created a database using SQLite which I have statements which work for inputting, viewing, updating and deleting records. But when I try and get text from a textfield I get a null pointer exception or I have gotten my statement for grabbing the data as a record. The first bit of code is my statement for saving the data and the second bit is where I create my text field, if you need any more code just let me know. Thanks
if (action.equals("Save")){
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:enquiries.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String FORENAME = tf1.getText();
String sql = "INSERT INTO ENQUIRIES (FORENAME,SURNAME,ADDRESSLINE1,ADDRESSLINE2,TOWN,COUNTY,OUTWARDCODE,INWARDCODE,TYPEOFPHONE,PHONENUMBER,JOBWANTEDDOING,HASTHECLIENTUSEDAABEFORE,WHEREDIDTHECLIENTHEARABOUTAA,HASTHECLIENTBOOKED,WHYDIDTHECLIENTNOTBOOK,DATEBOOKED,MANHOURSNEEDED,NOOFSTAFF) " +
"VALUES ('+ tf1.getText() +', '', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '' );";
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
JPanel namePanel = new JPanel();
namePanel.setLayout (new GridLayout(1,2));
JTextField tf1=new JTextField(20);
tf1.setText("Forename");
JTextField tf2=new JTextField(20);
tf2.setText("Surname");
namePanel.add(tf1);
namePanel.add(tf2);
Aucun commentaire:
Enregistrer un commentaire