Results 1 to 20 of 24
Thread: Why I'm getting NULL
- 11-05-2011, 01:09 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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.
- 11-05-2011, 03:26 PM #2
Re: Why I'm getting NULL
What variable is null? Where do you give that variable a non null value?
- 11-05-2011, 04:25 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
Re: Why I'm getting NULL
variable tt and t
they should were assigned values when setprint() and getprint are called
- 11-05-2011, 04:31 PM #4
Re: Why I'm getting NULL
Is setprint() called with a non null value before getprint() is called?
- 11-05-2011, 04:38 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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
- 11-05-2011, 04:39 PM #6
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.
- 11-05-2011, 04:41 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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
- 11-05-2011, 04:42 PM #8
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.
- 11-05-2011, 04:45 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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."
- 11-05-2011, 04:49 PM #10
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-05-2011, 05:06 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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
- 11-05-2011, 05:09 PM #12
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.
- 11-05-2011, 05:13 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
Re: Why I'm getting NULL
ok, i will try it but sorry i have to go now
regards
- 11-08-2011, 02:41 PM #14
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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:
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.try
{
locationManager.requestLocationUpdates(LocationMan ager.GPS_PROVIDER, 1, 2, this); // this=>refers to the listener
}
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
- 11-08-2011, 03:35 PM #15
Re: Why I'm getting NULL
Yes, your class can implement more than one interface. One for each listener.can i use 2 listeners in the same classJava Code:class TheClass implements Interface1, Interface2, Interface3, .... {Create an instance of the class that implements the listener and use its reference as an arg to the method you are calling.I want to use another different listenerLast edited by Norm; 11-08-2011 at 03:38 PM.
- 11-08-2011, 03:43 PM #16
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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
- 11-08-2011, 03:50 PM #17
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
- 11-08-2011, 05:01 PM #18
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
- 11-08-2011, 05:06 PM #19
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.
- 11-08-2011, 05:27 PM #20
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
Similar Threads
-
Is it better to set array elements to null before setting the arrayreference to null?
By kreyszig in forum Advanced JavaReplies: 6Last Post: 10-18-2010, 10:40 AM -
Null :(
By PhQ in forum Advanced JavaReplies: 4Last Post: 04-12-2010, 01:05 AM -
JOptionPane.showMessageDialog(null,"Etc Etc"); - What does null actually do?
By markious in forum New To JavaReplies: 2Last Post: 03-19-2010, 05:30 PM -
Null
By leapinlizard in forum New To JavaReplies: 4Last Post: 04-29-2009, 11:29 PM -
What is NULL
By bugger in forum New To JavaReplies: 1Last Post: 01-09-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks