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
New Developer
Posts: 27
Registered: ‎12-17-2011
My Device: Curve
My Carrier: Celcom

Wierd Problem on my java code

Dear expert, I experience a weird problem while performing the code below, the code work fine in below condition and unfortunately not working at all when i try to move the code inside setChangeListener.. Please help me to resolve this issue. public class SpeedMainScreen extends MainScreen { /** * Creates a new SpeedMainScreen object */ public SpeedMainScreen() { // Set the displayed title of the screen setTitle("Speedometer"); Criteria criteria=new Criteria(); criteria.setHorizontalAccuracy(500); criteria.setVerticalAccuracy(500); LocationProvider locationProvider = null; try { locationProvider = LocationProvider.getInstance(criteria); } catch (LocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Location location = null; try { location = locationProvider.getLocation(60); } catch (LocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } QualifiedCoordinates coordinates=location.getQualifiedCoordinates(); System.out.println("Latitude : "+coordinates.getLatitude()+" Longitude : "+coordinates.getLongitude()); LabelField AboutLabel = new LabelField("Latitude : "+coordinates.getLatitude()+ " Longitude : "+coordinates.getLongitude()); add (AboutLabel); float test = 0; test = location.getSpeed(); String strSpeed = ""; strSpeed = String.valueOf(test); final LabelField SpeedLabel = new LabelField(strSpeed); //final LabelField SpeedLabel = new LabelField("test"); add (SpeedLabel); ButtonField getSpeedBtn = new ButtonField( "Get Current Speed", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER ); getSpeedBtn.setChangeListener( new FieldChangeListener() { public void fieldChanged( Field arg0, int arg1 ) { //i try to insert the same code here but failed //SpeedLabel.setText(""); } } ); add (getSpeedBtn); } }
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Wierd Problem on my java code

Hi @johagood,

 

First, please use the the 'Insert Code' tool when posting code so we can understand it (it's very hard to read).

 

Second, please post the results of your tests and the errors you got. We would like to help but it's very hard to help when we can't understand what went wrong.

 

E.

 

 

New Developer
Posts: 27
Registered: ‎12-17-2011
My Device: Curve
My Carrier: Celcom

Re: Wierd Problem on my java code

sorry, i never knew about insert code tool until you told me to do so..

 

Below is my code

public class SpeedMainScreen extends MainScreen
{
    /**
     * Creates a new SpeedMainScreen object
     */
    public SpeedMainScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Speedometer");
        
        Criteria criteria=new Criteria();
		criteria.setHorizontalAccuracy(500);
		criteria.setVerticalAccuracy(500);			
		LocationProvider locationProvider = null;
		try {
			locationProvider = LocationProvider.getInstance(criteria);
		} catch (LocationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Location location = null;
		try {
			location = locationProvider.getLocation(60);
		} catch (LocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		QualifiedCoordinates coordinates=location.getQualifiedCoordinates();
		System.out.println("Latitude : "+coordinates.getLatitude()+" Longitude : "+coordinates.getLongitude());
        LabelField AboutLabel = new LabelField("Latitude : "+coordinates.getLatitude()+
        		" Longitude : "+coordinates.getLongitude());
        add (AboutLabel);
		float test = 0;
		test = location.getSpeed();
		String strSpeed = "";
		strSpeed = String.valueOf(test);
		
		final LabelField SpeedLabel = new LabelField(strSpeed);
        
        //final LabelField SpeedLabel = new LabelField("test");
        add (SpeedLabel);
        
        ButtonField getSpeedBtn = new ButtonField( "Get Current Speed", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER );
        getSpeedBtn.setChangeListener( new FieldChangeListener() {

        	
			public void fieldChanged( Field arg0, int arg1 ) {
				
				//i try to insert the same code here but failed
				
				//SpeedLabel.setText("");
                
            }
        } );
        add (getSpeedBtn);
    }
}

 It work perfecty fine until i move the code into new location. Please refer the code above which i put note "//try to insert the same code here but failed"

 

The error i receive is exception null. When i debug, i found out that variable location return Null value

Developer
Posts: 16,647
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Wierd Problem on my java code

getLocation is a blocking call and should be executed on a separate thread.
alternatively you can use a locationlistener
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter