It's all in the logcat:

E/AndroidRuntime(11235): java.lang.NullPointerException
E/AndroidRuntime(11235):        at
com.example.MyService.print_result(MyService.
java:62)

The stack trace is quite informative - learn to use it to find references
to your code.

23 ноября 2011 г. 17:46 пользователь Graham Bright <gbwienmobil...@gmail.com
> написал:

> Hi I am getting FATAL EXCEPTION when the ArrayList items is
> populated;
>
> can you help ?
>
> Thanks
> Graham
>
>
> W/dalvikvm(11235): threadid=9: thread exiting with uncaught exception
> (group=0x4
> 001e578)
> E/AndroidRuntime(11235): FATAL EXCEPTION: Thread-10
> E/AndroidRuntime(11235): java.lang.NullPointerException
> E/AndroidRuntime(11235):        at
> com.example.MyService.print_result(MyService.
> java:62)
> E/AndroidRuntime(11235):        at com.example.MyService
> $1.run(MyService.java:54
> )
> E/AndroidRuntime(11235):        at java.lang.Thread.run(Thread.java:
> 1019)
> W/ActivityManager( 2696):   Force finishing activity
> com.example/.ServicesDemo
> I/OrientationDebug( 2696): [pwm] in updateOrientationListenerLp()
> V/OrientationDebug( 2696): in updateOrientationListenerLp(), Screen
> status=true,
>  current orientation=1, SensorEnabled=true
> I/OrientationDebug( 2696): [pwm] needSensorRunningLp(), return true #4
> I/Launcher( 2852): onResume(). mIsNewIntent : false screenOff: false
> E/        ( 2696): Dumpstate > /data/log/dumpstate_app_error
> I/dumpstate(11275): begin
> D/KeyguardViewMediator( 2696): handleTimeout
> W/PowerManagerService( 2696): Timer 0x3->0x3|0x3
>
>
> package com.example;
>
>
> import java.util.ArrayList;
>
> import android.app.Service;
> import android.content.Intent;
> import android.os.Bundle;
> import android.os.IBinder;
> import android.util.Log;
> import android.widget.Toast;
>
> public class MyService extends Service {
>
> ArrayList<String> items = null;
>
> public String ORIG = "";
> private static final String TAG = "MyService";
> public Bundle data = new Bundle();
>
>
> @Override
> public IBinder onBind(Intent intent) {
> return null;
> }
>
>
> public static String getTag() {
> return TAG;
> }
>
>
> @Override
> public void onCreate() {
> Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
>
> }
>
> @Override
> public void onDestroy() {
> Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
> Log.d(TAG, "onDestroy");
>
> }
>
> @Override
> public void onStart(Intent intent, int startid) {
> Log.d(TAG, "onStart");
> data = intent.getExtras();
> ORIG = data.getString("originator");
> Log.d(TAG, "onCreate");
> Thread initBkgdThread = new Thread(new Runnable() {
> public void run() {
> print_result(ORIG);
> }
> });
> initBkgdThread.start();
> }
>
> public void print_result(String orig){
> Log.d(TAG, "HELLO WORLD:" + orig);
> items.add(orig);
> if (items != null) {
> items.add(orig);
> String toadd = orig.toString();
>
> if(items.contains(toadd)){
> Log.d(TAG, "Element already exists exiting ");
> } else {
> Log.d(TAG, "Adding Element");
>     items.add(toadd);
>
> }
>
> }
> else {
> Log.d(TAG, "IS NULL");
> }
> }
>
> }
>
> On 23 Nov., 12:53, Kostya Vasilyev <kmans...@gmail.com> wrote:
> > Look below "caused by" in the logcat to identify where the
> > NullPointerException occurrs. Then fix it.
> >
> > 23 ноября 2011 г. 15:36 пользователь Graham Bright <
> gbwienmobil...@gmail.com
> >
> >
> >
> > > написал:
> > > Can anyone help?
> >
> > > Thanks
> > > Graham
> > > On Nov 23, 2011 11:51 a.m., "Graham Bright" <gbwienmobil...@gmail.com>
> > > wrote:
> >
> > >> Hi,
> >
> > >> I am creating service which will run in the background and contain a
> > >> blocking list  (implemented using ArrayList) which may be used later
> > >> by other activites.
> >
> > >> My Problem:-
> >
> > >> From my activity Services Demo I pass data to my service using
> >
> > >> dataIntent.putExtra("originator", x);
> >
> > >> this is fine but when I try to store this data in my Service in an
> > >> ArrayList called Items I get an exception and I cannot figure out
> > >> why.
> >
> > >> The problem is in my Service code MyService :
> >
> > >>                if(items.contains(ORIG)){
> > >>                        Log.d(TAG, "Element already exists exiting ");
> > >>                } else {
> > >>                        Log.d(TAG, "Adding Element");
> > >>                    items.add(ORIG);
> >
> > >>                }
> > >> Please help.
> >
> > >> EXCEPTION
> > >> java.lang.RuntimeException: Unable to start service ...... with
> > >> intent
> >
> > >> caused by: java.lang.NullPointerException
> >
> > >> Best Regards,
> >
> > >> Graham
> >
> > >> CODE Eclipse
> >
> > >> Activity -> ServicesDemo
> > >> Service -> MyService
> >
> > >> ACTIVITY ServicesDemo
> >
> > >> package com.example;
> >
> > >> import android.app.Activity;
> > >> import android.content.Intent;
> > >> import android.os.Bundle;
> > >> import android.util.Log;
> > >> import android.view.View;
> > >> import android.view.View.OnClickListener;
> > >> import android.widget.Button;
> > >> import android.widget.EditText;
> >
> > >> public class ServicesDemo extends Activity implements OnClickListener
> > >> {
> > >>  private static final String TAG = "ServicesDemo";
> > >>  Button buttonStart, buttonStop;
> > >>  EditText Input;
> > >>  String x = "";  //test pass to my service
> >
> > >>  @Override
> > >>  public void onCreate(Bundle savedInstanceState) {
> > >>    super.onCreate(savedInstanceState);
> > >>    setContentView(R.layout.main);
> >
> > >>    buttonStart = (Button) findViewById(R.id.buttonStart);
> > >>    buttonStop = (Button) findViewById(R.id.buttonStop);
> > >>    Input = (EditText) findViewById(R.id.INPUT);
> > >>    buttonStart.setOnClickListener(this);
> > >>    buttonStop.setOnClickListener(this);
> >
> > >>  }
> >
> > >>  public void onClick(View src) {
> > >>    switch (src.getId()) {
> > >>    case R.id.buttonStart:
> > >>      Log.d(TAG, "onClick: starting srvice");
> > >>      Intent dataIntent = new Intent(ServicesDemo.this,
> > >> MyService.class);
> > >>      x = Input.getText().toString();
> > >>      dataIntent.putExtra("originator", x);
> > >>      startService(dataIntent);
> >
> > >>      break;
> >
> > >>    case R.id.buttonStop:
> >
> > >>      Log.d(TAG, "onClick: stopping srvice");
> > >>      //implicit starting
> > >>      stopService(new Intent(this, MyService.class));
> > >>      break;
> > >>    }
> > >>  }
> >
> > >> public static String getTag() {
> > >>        return TAG;
> > >> }
> > >> }
> > >> -----------------------------------<end ACTIVITY>
> >
> > >> --Service MyService ---------
> > >> package com.example;
> >
> > >> import java.util.ArrayList;
> >
> > >> import android.app.Service;
> > >> import android.content.Intent;
> > >> import android.os.Bundle;
> > >> import android.os.IBinder;
> > >> import android.util.Log;
> > >> import android.widget.Toast;
> >
> > >> public class MyService extends Service {
> >
> > >>        ArrayList<String> items;
> >
> > >>        public String ORIG = "";
> > >>        private static final String TAG = "MyService";
> > >>        public Bundle data = new Bundle();
> >
> > >>        @Override
> > >>        public IBinder onBind(Intent intent) {
> > >>                return null;
> > >>        }
> >
> > >>        public static String getTag() {
> > >>                return TAG;
> > >>        }
> >
> > >>        @Override
> > >>        public void onCreate() {
> > >>                Toast.makeText(this, "My Service Created",
> > >> Toast.LENGTH_LONG).show();
> > >>                Log.d(TAG, "onCreate");
> >
> > >>        }
> >
> > >>        @Override
> > >>        public void onDestroy() {
> > >>                Toast.makeText(this, "My Service Stopped",
> > >> Toast.LENGTH_LONG).show();
> > >>                Log.d(TAG, "onDestroy");
> >
> > >>        }
> >
> > >>        @Override
> > >>        public void onStart(Intent intent, int startid) {
> > >>                Log.d(TAG, "onStart");
> > >>                data = intent.getExtras();
> > >>                ORIG = data.getString("originator");
> > >>                Toast.makeText(this, "My Service Started with passed in
> > >> value " +
> > >> ORIG, Toast.LENGTH_LONG).show();
> > >>                if(items.contains(ORIG)){
> > >>                        Log.d(TAG, "Element already exists exiting ");
> > >>                } else {
> > >>                        Log.d(TAG, "Adding Element");
> > >>                    items.add(ORIG);
> >
> > >>                }
> >
> > >>                Thread initBkgdThread = new Thread(new Runnable() {
> > >>                        public void run() {
> > >>                                //print_result();
> > >>                        }
> > >>                        });
> > >>                        initBkgdThread.start();
> >
> > >>        }
> >
> > >>        public void print_result(String orig){
> > >>                Log.d(TAG, "HELLO WORLD:" + orig);
> >
> > >>        }
> >
> > >> }
> >
> > >> --------------end of service-----------
> >
> > >  --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to
> android-developers@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-developers+unsubscr...@googlegroups.com
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en- Zitierten
> Text ausblenden -
> >
> > - Zitierten Text anzeigen -
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to