Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

My Persistent Storage won't work

I am new at Persistent Storage and the following program doesn't seem to work properly, meaning that it gives me a ClassCast Exception. I'm not quite sure where my logic is wrong...Any suggestions please?

 

What the program does right now is that allows for EditFields to be added to screen and their contents remembered.

 

Here is the code

public final class MyScreen extends MainScreen
{
	EditField edit= new EditField("Enter: ","");
	ButtonField addN=new ButtonField("Add Field");
	ButtonField remov=new ButtonField("Remove Field");	
	int total=1;
	int tempT;	
	String str;
	
	static PersistentObject store=PersistentStore.getPersistentObject(0x22385e7657ec55aL);
	
	Vector temp=new Vector();
	
	Saver a=new Saver();
	
	//mypersistenttest 0x22385e7657ec55aL
	VerticalFieldManager up=new VerticalFieldManager();
	HorizontalFieldManager ac=new HorizontalFieldManager();

    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Persistent Storage");
        
        a=getInfo();
       
        tempT=a.getTotal();
        
        add(up);
        if(tempT>0)
        {
        	total=tempT;
        	temp=a.retrieve();
        	
        	for(int i=0;i<tempT;i++)
        	{
        		String s=(String) temp.elementAt(i);
        		up.add(new EditField("Was: ",s));
        	}       	
        }
        else
        {
        	up.add(edit);
        }     
        
        add(ac);    
        ac.add(addN);
        ac.add(remov);
        
        addN.setChangeListener(inc);
        //remov.setChangeListener(dec);
            
    }
    FieldChangeListener inc=new FieldChangeListener()
    {
    	public void fieldChanged(Field f, int i)
    	{
    		up.add(new EditField("Enter: ",""));
    		total=total+1;
    	}
    };
     //Save persistent Info
    public void saveInfo()
    {
    	int t;
    	Saver s=new Saver();
    	String inf;
    	Vector vec= new Vector();
    	
    	t=up.getFieldCount();
    	
    	
    	for(int z=0;z<t;z++)
    	{
    		Field f=up.getField(z);
    		if(f instanceof EditField)
    		{
    			EditField ef = (EditField)f;
    			inf=ef.getText();
        		vec.addElement(inf);
    		}

    	}
    	synchronized(store)
    	{
    		s.store(vec);
    		s.saveTotal(t);
    		store.setContents(s);
    		store.commit();
    	}
    }
    //Get persistent Info
    public Saver getInfo()
    {
    	Saver s=new Saver();
    	
    	synchronized(store)
    	{
    		s=(Saver)store.getContents();
    		if(s==null)
    		{
    			s= new Saver();
    		}
    	}
    	return s;
    }
    
    public boolean onClose()
    {
    	saveInfo();
    	System.exit(0);
        return true;
    }
}

 And this is my Persistable Class

 

public class Saver implements Persistable
{
	Vector storage=new Vector();
	int total;
	int edited;
	String msg;
	
	public void saveTotal(int a)
	{
		total=a;
	}
	public int getTotal()
	{
		
		return total;
	}
	
	public void store(Vector s)
	{
		storage=s;
		edited=1;
	}
	public Vector retrieve()
	{
		return storage;
	}
}

 I am clueless as to what is wrong..

_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: My Persistent Storage won't work

Hi @

 

 

Here are some points to think about:

 

1. You said you get ClassCastException but you didn't say on which line. It's very hard (if not useless) to debug this code using eyes only...

 

2. When you add fields (managers) to a screen, make sure they are all set BEOFRE adding them. Try and reduce the changes you do to a field after you adding it to a manager or screen as some changes call invalidate and repaint your screen.

 

3. Try to give your variables better names, and by better I mean, more understandable names. Try avoiding using 'a' as a variable name. It would be easier to debug and understand your code.

 

4. No need to create new Saver for each commit operation. Create one instance and update its contents. Once you are ready to save, just call commit on your instance. This will reduce resources usage and prevent deletion of old data.

 

Again, post the exact error and location so we can help with your original question.

 

Hope that helps,

 

E.

 

Highlighted
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: My Persistent Storage won't work

Sorry.

1. I am using Eclipse with Blackberry Plugin and I am truly unaware of how to find out where error's are located. I simply run it on the simulator and the simulator tells me this...I have no knowledge as to where the error is generated from when I get that info from the simulator.
2. Will Do
3. Yeah the variable names are horrible, partly because I was just doing a prototype test and had no intention of sharing the code.. But I'll heed your example..it is good practice.
4. Okay

I'll give you feedback soon. Thanks
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: My Persistent Storage won't work

The simplest way is to open the device (simulator) log and search for the error (usually in bold).

 

To open the log, click on: alt + L + G +L +G.

 

If that does not open the log for you, google for the correct key combination with your device model.

 

E.

Developer
Posts: 202
Registered: ‎04-19-2011
My Device: Blackberry Bold 9780
My Carrier: TAM

Re: My Persistent Storage won't work

As far as i know,

Class Cast exception is exception that you got if u make wrong instance..

excample :

String a = "helloWorld";

Vector vector = new Vector();

vector.addElement(a);

int b = (int) vector.elementAt(0);

 

correct me if i'm wrong