hi james cherril
I am doing a voting system application of which I have to hand up today. I am getting a runtime error when I entered a record and try to save it.
as I entered one record and when the save button is pressed it give me a runtime error. can someone give me a clear feedback as to why I am getting the error and how it can be fixed. andy help would do. I have three class.
here is the code i used.
this the is voterinfo class where it is gathering all the information on the voter
all help would be appreciated.
/*
* 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 votersapp;
import java.io.Serializable;
import java.util.HashMap;
/**
*
* @author luanataylor
*/
public class VotersInfo implements Serializable{
//inistiating variable
private String FirstName;
private String LastName;
private String EmailID;
private String HomeAddress;
private String RegNo;
private String ContactNumber;
private String position;
private String FirstTime;
private String age;
public VotersInfo(HashMap <String, String >voter){
this.setVoter(voter);
}
public String getFirstName() {
return FirstName;
}
public String getLastName() {
return LastName;
}
public String getEmailID() {
return EmailID;
}
public String getHomeAddress() {
return HomeAddress;
}
public String getContactNumber() {
return ContactNumber;
}
public String getPosition() {
return position;
}
public String getFirstTime() {
return FirstTime;
}
public String getRegNo() {
return RegNo;
}
public String getAge() {
return age;
}
public void setVoter(HashMap <String, String >voter ){
this.FirstName = voter.get("FirstName");
this.LastName = voter.get("LastName");
this.EmailID = voter.get("EmailID");
this.HomeAddress = voter.get("HomeAddress");
this.ContactNumber = voter.get("ContactNumber");
this.position = voter.get("position");
this.FirstTime = voter.get("FirstTime");
this.age = voter.get("age");
this.RegNo = voter.get("RegNo");
}
here is my second class
package votersapp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author luanataylor
*/
public class VotersOps {
public ArrayList <VotersInfo> list = new ArrayList();
///public VotersInfo v;
public VotersOps(){
//this.v = new VotersInfo ();
}
public void setVotersInfo(HashMap<String, String> voter){
}
public void addvoter(int position, HashMap<String, String> voter){
VotersInfo v = new VotersInfo(voter);
if( list.isEmpty() || position == list.size() )
{
list.add(v);
}
else
{
list.set(position, v);
}
System.out.println(this.list.toString());
System.out.println(this.list.size());
}
public void saveVoterInfo()throws Exception{
File f = new File("voter.txt");
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
}
and here is the gui frame
package votersapp;
import java.util.HashMap;
/**
*
* @author luanataylor
*/
public class Voters_reg1 extends javax.swing.JFrame {
HashMap<String, String>voterInfo;
public VotersOps voter;
public int currentpos;
public Voters_reg1() {
initComponents();
this.voter = new VotersOps();
this.voterInfo = new HashMap();
int size = voter.list.size();
jtf1.setText(String.valueOf(size + 1));
jtf2.setText(String.valueOf(size + 1));
this.currentpos = 1;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
fnamelbl = new javax.swing.JLabel();
fntxtf = new javax.swing.JTextField();
lnamelbl = new javax.swing.JLabel();
lntxtf = new javax.swing.JTextField();
regnumlbl = new javax.swing.JLabel();
regnumtxtf = new javax.swing.JTextField();
agelbl = new javax.swing.JLabel();
agetxtf = new javax.swing.JTextField();
emaillbl = new javax.swing.JLabel();
emailtxtf = new javax.swing.JTextField();
contactnumlbl = new javax.swing.JLabel();
contacttxtf = new javax.swing.JTextField();
addresslbl = new javax.swing.JLabel();
addresstxtf = new javax.swing.JTextField();
postlbl = new javax.swing.JLabel();
postcbox = new javax.swing.JComboBox();
firsttimelbl = new javax.swing.JLabel();
yesbtn = new javax.swing.JRadioButton();
nobtn = new javax.swing.JRadioButton();
savebtn = new javax.swing.JButton();
newbtn = new javax.swing.JButton();
exit = new javax.swing.JButton();
prevbtn = new javax.swing.JButton();
jtf1 = new javax.swing.JTextField();
jtf2 = new javax.swing.JTextField();
nextbtn2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fnamelbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
fnamelbl.setText("First Name");
fntxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
lnamelbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
lnamelbl.setText("Last Name");
lntxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
regnumlbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
regnumlbl.setText("Reg. No.");
regnumtxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
agelbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
agelbl.setText("Age");
agetxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
emaillbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
emaillbl.setText("Email ID");
emailtxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
contactnumlbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
contactnumlbl.setText("Contact No.");
contacttxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
addresslbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
addresslbl.setText("Home Address");
addresstxtf.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
postlbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
postlbl.setText("Position");
postcbox.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
postcbox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cerk I", "Clerk Typist I", "Manager", "Administrator" }));
firsttimelbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
firsttimelbl.setText("First Time");
yesbtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
yesbtn.setText("Yes");
nobtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
nobtn.setText("No");
savebtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
savebtn.setText("Save");
savebtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
savebtnActionPerformed(evt);
}
});
newbtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
newbtn.setText("New");
newbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newbtnActionPerformed(evt);
}
});
exit.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
exit.setText("Exit");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
prevbtn.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
prevbtn.setText("Previous");
prevbtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
prevbtnActionPerformed(evt);
}
});
jtf1.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
jtf1.setEnabled(false);
jtf2.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
jtf2.setEnabled(false);
nextbtn2.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
nextbtn2.setText("Next");
nextbtn2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextbtn2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(209, 209, 209)
.addComponent(jtf2, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(nextbtn2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(138, 138, 138))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(agelbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(regnumlbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lnamelbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fnamelbl, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
.addComponent(emaillbl))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(fntxtf)
.addComponent(lntxtf)
.addComponent(regnumtxtf)
.addComponent(agetxtf)
.addComponent(emailtxtf, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
.addGap(51, 51, 51)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(contactnumlbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(addresslbl, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
.addComponent(postlbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(firsttimelbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(contacttxtf)
.addComponent(addresstxtf)
.addComponent(postcbox, 0, 122, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(yesbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nobtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(newbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33)
.addComponent(savebtn, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(prevbtn)
.addGap(18, 18, 18)
.addComponent(jtf1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(58, 58, 58)
.addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(17, 17, 17)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fnamelbl)
.addComponent(fntxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(contactnumlbl)
.addComponent(contacttxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lnamelbl)
.addComponent(lntxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addresslbl)
.addComponent(addresstxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(regnumlbl)
.addComponent(regnumtxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(postlbl))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(agelbl)
.addComponent(agetxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(firsttimelbl)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(emaillbl)
.addComponent(emailtxtf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(yesbtn)
.addComponent(nobtn)))))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(postcbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(26, 26, 26)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jtf1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtf2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(nextbtn2)
.addComponent(prevbtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(newbtn)
.addComponent(exit)
.addComponent(savebtn))
.addGap(35, 35, 35))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 10, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void newbtnActionPerformed(java.awt.event.ActionEvent evt) {
fntxtf.setText("");
lntxtf.setText("");
regnumtxtf.setText("");
agetxtf.setText("");
emailtxtf.setText("");
contacttxtf.setText("");
addresstxtf.setText("");
int size = voter.list.size();
if(size > 0){
this.currentpos ++ ;
}else{
this.currentpos =1;
}
jtf1.setText(String.valueOf(this.currentpos ));
jtf2.setText(String.valueOf(size + 1));
System.out.println("pos: " );
System.out.println(this.currentpos);
}
private void savebtnActionPerformed(java.awt.event.ActionEvent evt) {
this.voterInfo.put("FirstName",fntxtf.getText());
this.voterInfo.put("LastName", lntxtf.getText());
this.voterInfo.put("RegNo", regnumtxtf.getText());
this.voterInfo.put("Age", agetxtf.getText());
this.voterInfo.put("EmailID", emailtxtf.getText());
this.voterInfo.put("ContactNo", contacttxtf.getText());
this. voterInfo.put("HomeAddress", addresstxtf.getText());
int index = this.currentpos;
System.out.println(index);
voter.addvoter(index-1, this.voterInfo );
int size = voter.list.size();
jtf2.setText(String.valueOf(size));
try{
voter.saveVoterInfo();
}catch(Exception e){
}
}
private void prevbtnActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(this.currentpos);
if(this.currentpos > 1){
this.currentpos--;
}
fntxtf.setText(voter.list.get(this.currentpos-1).getFirstName());
System.out.println(voter.list.get(this.currentpos-1).getFirstName());
jtf1.setText(String.valueOf(this.currentpos));
System.out.println(this.currentpos);
}
private void nextbtn2ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(this.currentpos);
int size = voter.list.size();
if(this.currentpos < size ){
this.currentpos++;
}
fntxtf.setText(voter.list.get(this.currentpos-1).getFirstName());
System.out.println(voter.list.get(this.currentpos-1).getFirstName());
jtf1.setText(String.valueOf(this.currentpos));
System.out.println(this.currentpos);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Voters_reg1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Voters_reg1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Voters_reg1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Voters_reg1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Voters_reg1().setVisible(true);
}
});
}
VotersOps vote = new VotersOps();