tags:

views:

174

answers:

0

i am implementing ListField to show users data in next screen. but its throwing an exception can u plz guide me whats wrong with this code.

import java.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import java.util.*;

//import Display.StoreInfo;
import net.rim.device.api.i18n.*;
import net.rim.device.api.synchronization.*;

/*An application in which user enters the data. this data is displayed when user press the save button*/
public class MusicStores extends UiApplication 
{
/*declaring Strings to store the data of the user*/
 String getFirstName;
 String getLastName;
 String getEmail;
 String getGender;
 String getStatus;

 private static final int FIELDTAG_NAME = 1;
 private static final int FIELDTAG_PHONE = 2;
 private static final int FIELDTAG_ADDRESS = 3;
 private static final int FIELDTAG_SPECIALTY = 4;

 /*declaring text fields for user input*/
 private AutoTextEditField firstName;

 private AutoTextEditField lastName;

 private EmailAddressEditField email;
/*declaring choice field for user input*/
 private ObjectChoiceField gender;
 /*declaring check box field for user input*/
 private CheckboxField status;
 ButtonField btnShow;
 /*declaring vector*/
 private static Vector _data;
 /*declaring persistent object*/
 private static PersistentObject store;

 private static MusicStores _instance;



 //adding functionality to menu item "saveItem" 
 private MenuItem saveItem = new MenuItem("Save", 110, 10) 
 {
   public void run() 
   {
    //creating an object of inner class StoreInfo
    StoreInfo info = new StoreInfo();
    //getting the test entered in the input fields
    info.setElement(StoreInfo.NAME, firstName.getText());
    info.setElement(StoreInfo.LastNAME,lastName.getText());
    info.setElement(StoreInfo.EMail, email.getText());
    info.setElement(StoreInfo.GenDer,gender.toString());
    if(status.getChecked())
     info.setElement(StoreInfo.setStatus, "Active");
    else
     info.setElement(StoreInfo.setStatus, "In Active");
    //adding the object to the end of the vector
    _data.addElement(info);
    //synchronizing the thread
    synchronized (store) 
    {

     store.setContents(_data);
     store.commit();
    }
    //resetting the input fields

    Dialog.inform("Success!");
    firstName.setText(null);
    lastName.setText(null);
    email.setText("");
    gender.setSelectedIndex("Male");
    status.setChecked(true);
   }
 };
 //adding functionality to menu item "saveItem" 
 private MenuItem getItem = new MenuItem("Get", 110, 11) 
 {
  public void run() 
  {
   synchronized (store) 
   {
    _data = (Vector) store.getContents();
    if (!_data.isEmpty()) 
    {
     StoreInfo info = (StoreInfo)_data.lastElement();
     getFirstName = (info.getElement(StoreInfo.NAME));
     getLastName  = (info.getElement(StoreInfo.LastNAME));
     getEmail   = (info.getElement(StoreInfo.EMail));
     getGender   =  (info.getElement(StoreInfo.GenDer));
     getStatus  = (info.getElement(StoreInfo.setStatus));
     show();
    }
   }
  }
 };
 /*creating an entry point*/
public static void main(String[] args) 
{
 /*creating instance of the class */
 MusicStores app = new MusicStores();
 app.enterEventDispatcher();
}

/*creating default constructor*/
 public MusicStores() 
 {
  /*Creating an object of the main screen class to use its functionalities*/
  MainScreen mainScreen = new MainScreen();

  //setting title of the main screen
  mainScreen.setTitle(new LabelField("Enter Your Data"));

  //creating text fields for user input
  firstName = new AutoTextEditField("First Name: ", "");
  lastName= new AutoTextEditField("Last Name: ", "");
  email= new EmailAddressEditField("Email:: ", "");

  //creating choice field for user input
  String [] items = {"Male","Female"};
  gender= new ObjectChoiceField("Gender",items);

  status = new CheckboxField("Active",true);
  btnShow = new ButtonField("Show",ButtonField.CONSUME_CLICK);
  btnShow.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    pushScreen(new ShowScreen());
   }
  });
  mainScreen.add(btnShow);
  //adding the input fields to the main screen
  mainScreen.add(firstName);
  mainScreen.add(lastName);
  mainScreen.add(email);
  mainScreen.add(gender);
  mainScreen.add(status);

  //adding menu items

  mainScreen.addMenuItem(saveItem);
  mainScreen.addMenuItem(getItem);
  //pushing the main screen
  pushScreen(mainScreen);
 }

static {
store =
PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
_data = new Vector();
_data = (Vector) store.getContents();

}
private static final class StoreInfo implements Persistable 
{
 private Vector _elements;
 public static final int NAME = 0;
 public static final int LastNAME = 1;
 public static final int EMail= 2;
 public static final int GenDer = 3;
 public static final int setStatus = 4;

 public StoreInfo() 
 {
  _elements = new Vector(5);
  for (int i = 0; i < _elements.capacity(); ++i) 
  {
   _elements.addElement(new String(""));
  }
 }

 public String getElement(int id) 
 {
  return (String) _elements.elementAt(id);
 }
 public void setElement(int id, String value) 
 {
  _elements.setElementAt(value, id);
 }
}
public void show()
{
 Dialog.alert("Name is "+getFirstName+" "+getLastName+"\nGender is "+getGender+"\nE-mail: "+getEmail+"\nStatus is "+getStatus);
}
class ShowScreen extends MainScreen implements ListFieldCallback
{
 String showFirstName;
 String showLastName;
 String showEmail;
 String showGender;
 String showStatus;
 ListField list;
 private Vector listElements = new Vector();

 public ShowScreen()
 {
  setTitle(new LabelField("Showing Data"));
  show();
 }
 public void show()
 {
  _data = (Vector) store.getContents();
  try
  {
   for (int i = _data.size()-1; i >-1; i--) 
    {

     StoreInfo info = (StoreInfo)_data.elementAt(i);
     //checking for empty object
     if (!_data.isEmpty()) 
      {
       //if not empty
       //create a new object of Store Info class






       //storing information retrieved in strings
       //StoreInfo info = (StoreInfo)_data.lastElement();
       showFirstName = (info.getElement(StoreInfo.NAME));
       showLastName  = (info.getElement(StoreInfo.LastNAME));
       showEmail   = (info.getElement(StoreInfo.EMail));
       showGender   =  (info.getElement(StoreInfo.GenDer));
       showStatus  = (info.getElement(StoreInfo.setStatus));
       HorizontalFieldManager hr = new HorizontalFieldManager();


   //hr.add(new RichTextField(showFirstName + " "+ showLastName +"  "+ showEmail+ "  " + showGender  + " " +showStatus ,RichTextField.FOCUSABLE));
    //LabelField lblData= new LabelField(showFirstName + " "+ showLastName +"  "+ showEmail+ "  " + showGender  + " " +showStatus ,LabelField.FOCUSABLE);    
   list = new ListField();
   ShowScreen myCallback = new ShowScreen();
   list.setCallback(myCallback);
   String data = showFirstName + " "+ showLastName +"  "+ showEmail+ "  " + showGender  + " " +showStatus ;

   list.insert(0);
   myCallback.insert(data, 0);
   add(list);
   // hr.add(lblData);
   SeparatorField sp = new SeparatorField();
   add(hr);
   add(sp);

   }
    }
  }
   catch(Exception e){}



 }
 public void drawListRow(ListField list, Graphics g, int index, int y,int w) {
  String text = (String)listElements.elementAt(index);
  g.drawText(text, 0, y, 0, w);


 }
 public Object get(ListField listField, int index) {
  return listElements.elementAt(index);


 }
 public int getPreferredWidth(ListField listField) {
  return Graphics.getScreenWidth();
 }
 public int indexOfList(ListField list, String p, int s) {
  return listElements.indexOf(p, s);

 }
 public void insert(String toInsert, int index) {
  listElements.addElement(toInsert);
  }
  public void erase() {
  listElements.removeAllElements();
  }


}
}