Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all, I want to start a service from an activity. So I gave this in my activity onCreate method

Java
try
        {
        Intent gforce=new Intent(GPS2Activity.this,GforceService.class);
        gforce.putExtra("NUMBER", number);
        ct.startService(gforce);
        }

        catch (Exception e) {
           Toast.makeText(this, e.toString(), 2000).show();
       }


in my service class onStart() method I put

Java
@Override
	public void onStart(Intent intent, int startId) {
		
		phn=intent.getStringExtra("NUMBER");
		
		super.onStart(intent, startId);
	}


Then it gives Java.lang.NullPointerEsception. What is the reason for that and what can I do to solve this problem.

Thanks,
Posted

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Details here:
Class NullPointerException[^]
Java error java.lang.nullpointerexception[^]

DEBUG and look for the potential place and fix/handle it.
 
Share this answer
 
Comments
Espen Harlinn 12-Jun-12 8:17am    
5'ed!
Sandeep Mewara 12-Jun-12 12:55pm    
Thanks Espen.
You should always check the intent parameter for null. In the docs:

The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.

Furthermore, onStart() is deprecated. You should use onStartCommand() in your service.
 
Share this answer
 
Comments
Espen Harlinn 12-Jun-12 8:17am    
5'ed!
Niklas L 12-Jun-12 16:00pm    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900