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
Contributor
Posts: 13
Registered: ‎03-12-2011
My Device: Bold 9700
My Carrier: T-Mobile

Null Pointer Exception thrown when trying to use an ObjectChoiceField within a try catch block

Hi all,

 

I'm trying to populate an ObjectChoiceField with data obtained from a for loop within a try catch block. Everytime I run it I get the null pointer exception, please have a look at my code and make some suggestions:

 

 

Hashtable loc = new Hashtable();
Hashtable desc = new Hashtable();
Hashtable latitude = new Hashtable();
Hashtable longitude = new Hashtable();


private void parseJsonFile()
{
String choices[] = new String[26];

//Parse JSON File containing Bus Stop Details and store in hash table
try
{
InputStream instream = this.getClass().getResourceAsStream("jsonstops.json");

byte bytes[] = IOUtilities.streamToBytes(instream);
String fileString = new String(bytes);



try
{
JSONObject jso = new JSONObject(fileString);
JSONArray busStops = jso.getJSONArray("busstops");

for(int i = 0; i < busStops.length(); i++)
{
JSONObject stop = busStops.getJSONObject(i);
String location = stop.getString("location");
String description = stop.getString("description");
String lat = stop.getString("latitude");
String lon = stop.getString("longitude");


loc.put("loc"+i, location);
desc.put("desc"+i, description);
latitude.put("lat"+i, lat);
longitude.put("lon"+i, lon);

choices[i] = description;



}
//*****Here is the Problem********
add(new ObjectChoiceField("", choices, 0, FIELD_HCENTER));



}
catch(JSONException e)
{
e.printStackTrace();
}


}
catch(IOException e1)
{
Status.show("error while file reading");
e1.printStackTrace();
}


}

 Any Suggestions?

 

 

 

       
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Null Pointer Exception thrown when trying to use an ObjectChoiceField within a try catch block

Two possibilities:

 

a) You have

String choices[] = new String[26];

Do you actually get this number of choices.  You should supply an array that contains only defined data to your ObjectChoiceField.

 

2) Make sure that you do not have a null description.

 

So you have:

String description = stop.getString("description");

you need to check this:

if ( description == null || description.length() < 1 ) {

description = "<no description supplied>";

}

Contributor
Posts: 13
Registered: ‎03-12-2011
My Device: Bold 9700
My Carrier: T-Mobile

Re: Null Pointer Exception thrown when trying to use an ObjectChoiceField within a try catch block

[ Edited ]

My problem is the first one you raised as I know all descriptions are not null.

 

How do I declare the length of the array as the number of iterations of the loop (i) and then use that to populate the objectchoice field outside the for loop?

 

I've tried to do it like the following but still am getting the same null pointer exception:

 

 

for(int i = 0; i < busStops.length(); i++)
{
					
  JSONObject stop = busStops.getJSONObject(i);
  String location = stop.getString("location");
  String description = stop.getString("description");
  String lat = stop.getString("latitude");
  String lon = stop.getString("longitude");
					
					
  loc.put("loc"+i, location);
  desc.put("desc"+i, description);
  latitude.put("lat"+i, lat);
  longitude.put("lon"+i, lon);
					
//******Populate choices array with descriptions**********
					
   choices[i] = description;
					
					
}
				
//****Create new array and set length as choices .length
				
String real[] = new String[choices.length];

//*****Then populate objectchoicefield 				
add(new ObjectChoiceField("", real, 0, FIELD_HCENTER));
				
				 
}

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Null Pointer Exception thrown when trying to use an ObjectChoiceField within a try catch block

I would do it like this:

 

private void parseJsonFile()
{
String choices[] = null];

//Parse JSON File containing Bus Stop Details and store in hash table
try
{
InputStream instream = this.getClass().getResourceAsStream("jsonstops.json");

byte bytes[] = IOUtilities.streamToBytes(instream);
String fileString = new String(bytes);

try
{
JSONObject jso = new JSONObject(fileString);
JSONArray busStops = jso.getJSONArray("busstops");

choices = new String[busStops.length()];

for(int i = 0; i < busStops.length(); i++)

...