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
Regular Contributor
Zenkology
Posts: 92
Registered: ‎01-31-2012
My Carrier: vodafone
Accepted Solution

SAX parsing issue

hey guys I hope you are fine,um parsing an xml using sax parsing and I have a problem ... parsing done successfully ,but when I started to display it on screen it shows NullPointerException for the array saving the categories 

 

that's the code for handler 

 

package startAPp;

import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import Utils.NCRDecoder;

public class XMLHandler extends DefaultHandler {
	StringBuffer sb;
	private CategoryBean category;
	private Vector categories = new Vector();

	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
	}

	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		categories.trimToSize();
		CategoryBean[] beans = new CategoryBean[categories.size()];
		categories.copyInto(beans);
		AppManger manger = new AppManger();
		manger.setSize(categories.size());
		manger.setCategories(beans);
		System.out.println("end Document");
		
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		sb = new StringBuffer("");
		if (localName.equals("category")) {
			category = new CategoryBean();

		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if (localName.equalsIgnoreCase("title")) {
			category.setTitle(sb.toString());
			System.out.println(" Name....." + sb.toString());
		} else if (localName.equalsIgnoreCase("link")) {
			category.setLink(NCRDecoder.decode(sb.toString()));
			System.out.println(sb.toString());
		}
		System.out.println("Local Name....." + localName);
		categories.addElement(category);
		sb = new StringBuffer("");
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String theString = new String(ch, start, length);
		sb.append(theString);
	}

}

 and that's the manger which save the array .

 

package startAPp;

public class AppManger {
	CategoryBean[] beans;
	int size;

	public AppManger() {

	}

	public void setCategories(CategoryBean[] beans) {
		this.beans = beans;

	}

	public CategoryBean[] getCategories() {
		return beans;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;

	}

}

 that's a sample of the screen to detect if the array contains null or not 

 

	AppManger app;
	CategoryBean[] beans;
	Connection _conn;

	Vector v;

	public MyScreen() {
		setTitle("MyTitle");
		_conn = new Connection();
		_conn.start();
	

	}

	public void addElements() {
		app = new AppManger();
		beans=new CategoryBean[app.getSize()];
		
		v = new Vector();
		add(new LabelField("start"));
		for (int i = 0; i < beans.length; i++) {
			add(new LabelField(i+""));
		}
}

 

guys please help me :smileyhappy:

Please use plain text.
Developer
peter_strange
Posts: 19,508
Registered: ‎07-14-2008

Re: SAX parsing issue

There appears to be lots of ln=inkage code missing in this example,  In you startup, you start a Connection.  But we have no idea how this relates to your XML Handler or how your XMLHandler, which seems to have its own local variables, relates to the the AppManger in your MyScreen.  There are timing issues here too - idoes the Thread actulaly complete before you display MyScreen.

 

Just wondered - how new to Java are you?

 

Anyway, the most obvious error is shown by these two lines:

a) In XMLHandler

AppManger manger = new AppManger();

b) In MyScreen

p = new AppManger();

 

You see to think that these are the same Object.  They are the same class, but that does not make them the same Object.  Specifically they are two different instances of the same class.  If you want the processing to update the same Object, then they have to share it.

 

You can do this by

1) sharing a reference t the same Object (i.e. only create one AppManager Object and pass it to both MyScreen and XMLHandler) or

2) create a Singleton Instance that bot can get access too. 

 

In this case (2) is easier, and the easiest way to create a Singleton is to make it static (whihc as you will find out later does not actually make it a Singeton in all cases, but does in this case).

So change the start of AppManager to the following:

 

public class AppManger {
CategoryBean[] beans;
int size;

 

static AppManger instance;

private AppManger() {

}

public static AppManger getInstance() {

if ( instance == null ) {
instance = new AppManger();

}

return instance;

}
.....

 

and then instead of using

... = new AppManager()

use

... = AppManager.getInstance();

 

With this resolved, you only have the timing issue to worry about, in other words, making sure you don't try to display the data on MyScreen until you have downloaded it.  This is another problem. 

Please use plain text.
Regular Contributor
Zenkology
Posts: 92
Registered: ‎01-31-2012
My Carrier: vodafone

Re: SAX parsing issue

[ Edited ]

I did what you say but actually the problem still exist :smileyfrustrated: , actually it goes to the getCategories() , from the constructor of the MyScreen class before it setCategories() in XMLHandler class and I don't know how to solve that

Please use plain text.
Developer
peter_strange
Posts: 19,508
Registered: ‎07-14-2008

Re: SAX parsing issue

So you are having the timing issue I mentioned at the end of my last post?

 

If so, have a look at this:

http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/t...

Please use plain text.
Regular Contributor
Zenkology
Posts: 92
Registered: ‎01-31-2012
My Carrier: vodafone

Re: SAX parsing issue

great thnx peter :smileyhappy:
Please use plain text.