MurphyApps
MurphyApps

Reputation: 13

Android Calling an array list causes the program to crash

I have written some code and in one class I create and populate an array list and I am able to call and print it out from another class, but when I try and call it from a third class is causes the mobile app to close. Here is the code for the array list:

public class MyPositionOverlay extends ItemizedOverlay<OverlayItem> {

    public ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
    Context context;


    public MyPositionOverlay(Drawable marker, Context c) {
        super(boundCenterBottom(marker));
        // TODO Auto-generated constructor stub
        populate();
        context = c;


    }
    private final int mRadius = 5;

    Location location;

    public Location getLocation() {
        return location;
    }
    public void setLocation(Location location) {;
    this.location = location;
    }

    //Method for user intputs - fill in
    @Override
    public boolean onTap(GeoPoint point, MapView mapView) {

        if(super.onTap(point, mapView)){
            return true;
        }
        String title = "Point:" + String.valueOf(overlayItemList.size()+ 1) + "\n" +
                "Lat:" + "\n" + String.valueOf(point.getLatitudeE6() + 
                        "Long:" + "\n" + String.valueOf(point.getLongitudeE6()));
        String snippet = "geo:\n"
                + String.valueOf(point.getLatitudeE6()) + "\n"
                + String.valueOf(point.getLongitudeE6());

        addItem(point, title, snippet);



        //return true;
        //if(hit test is true)
        //  perform task
        // return true;
        //if not handled

        return false;
    }

    @Override
    protected boolean onTap(int index) {
        // TODO Auto-generated method stub
        //return super.onTap(index);

        Toast.makeText(context,
                "Touch on marker: \n" + overlayItemList.get(index).getTitle(),
                Toast.LENGTH_LONG).show();

        return true;
    }

    public void addItem(GeoPoint point, String title, String snippet){
        OverlayItem newItem = new OverlayItem(point, title, snippet);
        overlayItemList.add(newItem);
        populate();
    }



    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return overlayItemList.get(i);
    }
    @Override
    public int size() {
        // TODO Auto-generated method stub
        return overlayItemList.size();
    }
}

and i simply try and call it from the class below:

public class GeneticAlgorithm2  {

    //public int populationSize;
    //private MyPositionOverlay popList;

    MyPositionOverlay popSize;
    MyPositionOverlay positionOverlay;
    HomeScreen homeScreenPop;
    //MyPositionOverlay overlaySize = new MyPositionOverlay(null, null);

    //int populationSize2 = popSize.size();

    int populationSize = 800;
    public int route[] = new int[populationSize +1];
    int pointLimit;
    int iterations;
    double mutationRate = 0.10;

    //Canvas C = null;
    //Dimension CD;
    //Label L;
    //Graphics GC;
    //int Initial_population = 800;


    int matingPopulation = populationSize/2;
    int favPopulation = matingPopulation/2;
    int numOfCities = 30;//populationSize2;
    //int numOfCities = popSize.overlayItemList.size();
    int cutLength = numOfCities/5;
    //double Mutation_probability = 0.10;

    double loops=1.5;
    int ePoch;
    double xLow = 0.0;
    double yLow = 0.0;
    double xHigh = 100.0;
    double yHigh = 100.0;
    double xRange, yRange;
    double minCost = 5.0;
    double timeStart,timeEnd;
    Thread T = null;
    double costPerfect = 0.;

    boolean started = false;

    City [] cities;
    Chromosome [] chromosomes;



/*
    public GeneticAlgorithm2() {

        // Calling My Position Overlay class
        //popList = new MyPositionOverlay(null, null);

        //Sets the population size to the number of markers
        //populationSize = popSize.overlayItemList.size();
        //start();
        //test();

        //int populationSize3 = popSize.size();

    }*/



    public void test(){
        System.out.println("test");
    }

    public void start() {

        // Calling My Position Overlay class
        //popSize = new MyPositionOverlay(null, null);

        //Sets the population size to the number of markers
        //populationSize = popSize.overlayItemList.size();

        //popSize.size();

        //int populationSize2 = popSize.size();
        //int populationSize3 = positionOverlay.size();



        System.out.println("Start ...");
        // generate a set of cities

        System.out.println(positionOverlay.size());
        //System.out.println( homeScreenPop.positionOverlay.size());

}
}

Here is the error messages I get:

06-06 17:14:05.210: W/dalvikvm(1301): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-06 17:14:05.220: E/AndroidRuntime(1301): FATAL EXCEPTION: main
06-06 17:14:05.220: E/AndroidRuntime(1301): java.lang.NullPointerException
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.Maps.GeneticAlgorithm2.start(GeneticAlgorithm2.java:97)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.Maps.HomeScreen$3.onClick(HomeScreen.java:152)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.view.View.performClick(View.java:2408)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.view.View$PerformClick.run(View.java:8816)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.os.Handler.handleCallback(Handler.java:587)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.os.Looper.loop(Looper.java:123)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at java.lang.reflect.Method.invoke(Method.java:521)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 17:14:05.220: E/AndroidRuntime(1301):     at dalvik.system.NativeStart.main(Native Method)

From what i can see it is saying the array list is null but like i said i can happily output it from another class.

Please Help.

Upvotes: 0

Views: 1395

Answers (1)

mprabhat
mprabhat

Reputation: 20323

In your start method positionOverlay is null, you have just declared it but you haven't initialized it before accessing this field, hence NPE.

Upvotes: 3

Related Questions