Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Why I'm getting NULL

    Hi,
    I have coded an android application and I'm trying to access a member method implemented in an interface...but the result is always NULL which indicates there is something wrong
    Please guide me

    the method that i\m trying to access is called getprint();
    Java Code:
    package com.androidbook.CarPosition;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.location.GpsSatellite;
    import android.location.GpsStatus;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class CarPositionActivity extends Activity {
        /** Called when the activity is first created. */
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            LocationListener locationListener = new MyLocationListener();
            
            if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            	AlertDialog.Builder noGps = new AlertDialog.Builder(this);
            	noGps.setMessage("GPS is off. Please turn on GPS an restart App.");
            	noGps.show();
            }
            
            try
            {
            	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 2, locationListener);
            }
            catch(IllegalArgumentException e)
            {
            	TextView tv1 = new TextView(this);
                tv1.setText("Provider or Lisetner is null");
                setContentView(tv1);	
            }
            catch(RuntimeException e)
            {
            	TextView tv2 = new TextView(this);
                tv2.setText("No suitable permission or the calling thread has no looper");
                setContentView(tv2);
            }
            //String txt = ((MyLocationListener) locationListener).getprint();
            TextView tv2 = new TextView(this);
            tv2.setText(""+((MyLocationListener) locationListener).getprint());
            setContentView(tv2);
            /*String latitude = ((MyLocationListener) locationListener).getRegisteredLocationLat();
            TextView tv3 = new TextView(this);
            tv3.setText(" hi "+latitude);
            setContentView(tv3);*/   
            
            
            
        }
    
        
        }
    Java Code:
    package com.androidbook.CarPosition;
    
    import com.androidbook.CarPosition.R;
    
    import android.content.Context;
    import android.content.ContextWrapper;
    import android.location.Location;
    import android.location.LocationListener;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MyLocationListener implements LocationListener {
    
    	String tt;
    	String longitude;
    	String latitude;
    	
    	/*TextView lon;
    	TextView lat;
    	lon = (TextView) void findViewById(R.id.lon);
        lat = (TextView) findViewById(R.id.lat);
        lon.setText("LON");
    	lat.setText("LAT");*/
    	
    	@Override
    	public void onLocationChanged(Location loc) {
    		// TODO Auto-generated method stub
    		
    	    
                   
            String text = "current location is: " + "latitude = " +Location.convert(loc.getAltitude(), Location.FORMAT_DEGREES) +
            		                                "longitude = " +Location.convert(loc.getLongitude(), Location.FORMAT_DEGREES);
            /*lon.setText(String.valueOf(text);
    		lat.setText(String.valueOf(loc.getLatitude()));*/ 
            //setprint(text);
            
            //setRegisteredLocationLat(loc);
            //setRegisteredLocationLong(loc);
            
            //Toast.makeText(context.getApplicationContext(), "location changed", Toast.LENGTH_SHORT).show();
    	}
    	
    	public void setprint(String t)
    	{
    	    tt=t;
    	}
    	
    	public String getprint()
    	{
    		return tt;
    	}
    
    	/*public void setRegisteredLocationLat(Location loc) {
    		// TODO Auto-generated method stub
    		 latitude=Location.convert(loc.getLatitude(), Location.FORMAT_DEGREES);
    	}*/
    	/*public void setRegisteredLocationLong(Location loc) {
    		// TODO Auto-generated method stub
    		 longitude=Location.convert(loc.getLongitude(), Location.FORMAT_DEGREES);
    	}*/
    	
    	/*public String getRegisteredLocationLong() {
    		// TODO Auto-generated method stub
    		return longitude;
    	}*/
    
    	/*public String getRegisteredLocationLat() {
    		// TODO Auto-generated method stub
    		return latitude;
    	}*/
    
    
    
    	@Override
    	public void onProviderDisabled(String arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onProviderEnabled(String arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onStatusChanged(String provider, int status, Bundle extras) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    Last edited by amrmb09; 11-05-2011 at 05:09 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    What variable is null? Where do you give that variable a non null value?

  3. #3
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    variable tt and t

    they should were assigned values when setprint() and getprint are called

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    Is setprint() called with a non null value before getprint() is called?

  5. #5
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    yes setprint(text);
    text should be a text that i want to display it to the user
    please see line 22 and 24 in the 2nd java code

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    Is setprint() called with a non null value before getprint() is called?

    Is there more than one instance of the class? One has the value and the other does not.
    Add a debugging println to the constructor to see if it is called more than once.

  7. #7
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    yes setprint(text);
    text is a varible of type string the contains a text that i want to display it
    please see line 22 and 24 in the 2nd java file

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    Is there more than one instance of the class? One has the value and the other does not.
    Add a debugging println to the constructor to see if it is called more than once.

  9. #9
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    please see lines 23 and 43 in the 1st java file
    sorry i dont know what is that "Add a debugging println to the constructor to see if it is called more than once."

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    You need to determine why the value of tt in one instance of the object is not seen by the getprint method.
    What if there are more than one instance of the object? One object has a good tt value and the other one has a null value.
    What if you call the getprint method on the one with the null value, not on the one with the good value?

    In java SE you could detect if there are more than one instances of the object by adding this line to the constructor:
    System.out.println("hi from the constructor");
    If you saw this printed out more than one time, you would know there are more than one instance of the object.

  11. #11
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    now i have modified the code so that i have one instance only of the class and still i get null

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    Then either setprint is NOT being called or setprint is called with a null value.
    Add some debug statements in setprint to show that it is being called and to show what value it receives.

  13. #13
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    ok, i will try it but sorry i have to go now
    regards

  14. #14
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    I solved my problem by merginging both of the class the extends activity and the class that implements myinterface in one file..actually, i dont know why when i splitted both of them into 2 different files i receive errors. Anyway, i have another question or problem which is:
    can i use 2 listeners in the same class, in other words, in my application i have the following:
    try
    {
    locationManager.requestLocationUpdates(LocationMan ager.GPS_PROVIDER, 1, 2, this); // this=>refers to the listener
    }
    and i want to use another different listener listener, how can i do that..and i think i cant write the same sentence mentioned above with "this" keyword because by doins so, i will refere to the same listener interface.
    please see below my code:
    Java Code:
    package com.androidbookCarCurrentPosition;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class CarCurrentPositionActivity extends Activity implements LocationListener{
        /** Called when the activity is first created. */
    	
        TextView lon;
        TextView lat;
        TextView latMin;
        TextView lonMin;
       
        
    	@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            lon    = (TextView) findViewById(R.id.lon);
            lat    = (TextView) findViewById(R.id.lat);
            latMin = (TextView) findViewById(R.id.latMin);
            lonMin = (TextView) findViewById(R.id.lonMin);
                   
            lon.setText("LON: ");
    		lat.setText("LAT: ");
            latMin.setText("LAT in Minutes: ");
            lonMin.setText("LON in Minutes: ");
            
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
                    
            if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            	AlertDialog.Builder noGps = new AlertDialog.Builder(this);
            	noGps.setMessage("GPS is off. Please turn on GPS and restart App.");
            	noGps.show();
            }
            
            try
            {
            	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 2, this);
            }
            catch(IllegalArgumentException e)
            {
            	TextView tv1 = new TextView(this);
                tv1.setText("Provider or Lisetner is null");
                setContentView(tv1);	
            }
            catch(RuntimeException e)
            {
            	TextView tv2 = new TextView(this);
                tv2.setText("No suitable permission or the calling thread has no looper");
                setContentView(tv2);
            }
                   
        }
        
        
        public void onLocationChanged(Location loc) {
    		// TODO Auto-generated method stub
    	      
            /*String text = "current location is: " + "latitude = " +Location.convert(loc.getAltitude(), Location.FORMAT_DEGREES) +
            		                                "longitude = " +Location.convert(loc.getLongitude(), Location.FORMAT_DEGREES);*/
        	
    		lon.setText(String.valueOf(loc.getLongitude()));
     		lat.setText(String.valueOf(loc.getLatitude()));
     		lonMin.setText(String.valueOf(Location.convert(loc.getLongitude(), Location.FORMAT_MINUTES)));
     		latMin.setText(String.valueOf(Location.convert(loc.getLatitude(), Location.FORMAT_MINUTES)));
    	}
        
    	@Override
    	public void onProviderDisabled(String arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onProviderEnabled(String arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onStatusChanged(String provider, int status, Bundle extras) {
    		// TODO Auto-generated method stub
    
    	}
    }
    Last edited by Norm; 11-08-2011 at 03:33 PM. Reason: changed tags to code

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    can i use 2 listeners in the same class
    Yes, your class can implement more than one interface. One for each listener.
    Java Code:
    class TheClass implements Interface1, Interface2, Interface3, .... {
    I want to use another different listener
    Create an instance of the class that implements the listener and use its reference as an arg to the method you are calling.
    Last edited by Norm; 11-08-2011 at 03:38 PM.

  16. #16
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    [QUOTE=Norm;244088]Create an instance of the class that implements the listener and use its reference as an arg to the method you are calling.
    would u please give me an example to understand what do u mean better
    Regards

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    Java Code:
    ListenerClass lc = new ListenerClass();  // create an instance of a listener class
    someObjRef.methodThatWantsListener(lc);   // pass the reference to the listener as an arg to a method

  18. #18
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    Quote Originally Posted by Norm View Post
    Java Code:
    ListenerClass lc = new ListenerClass();  // create an instance of a listener class
    someObjRef.methodThatWantsListener(lc);   // pass the reference to the listener as an arg to a method
    is it could be like this also:
    ListenerClass lc = new ListenerClass();
    lc.methodThatWantsListener(lc);
    or

    ListenerClass lc = new ListenerClass();
    lc.methodThatWantsListener(this);
    please tell me wheather they are right or wrong

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Why I'm getting NULL

    Neither looks right.
    The listener would not be passed to itself via one of its own methods.
    And it would not be passed a reference to the calling class.

  20. #20
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting NULL

    thanks alot...i will try it and
    now i have to go

    Regards

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  2. Null :(
    By PhQ in forum Advanced Java
    Replies: 4
    Last Post: 04-12-2010, 01:05 AM
  3. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  4. Null
    By leapinlizard in forum New To Java
    Replies: 4
    Last Post: 04-29-2009, 11:29 PM
  5. What is NULL
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •