1

I dont know why a am geting NullPointerException. I think problem creates on ArrayAdapter. Need help. Thanks in advance.

LogCat:

android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)

Activity:

package com.map.map;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ScrollView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    Map map;
    String Source = "Blank";
    String Destination = "Blank";
    ListView list1;
    ListView list2;
    String [] listArray;
    BufferedReader a;
    BufferedReader b;

    public static Integer Number_Of_Airport = 1168; //Total number of Airport in the World
    public HashMap<Integer,Time> Clock;   //The Clock of each Airport
    public HashMap<String,Integer> Airport;  //airport mapped to representative integer
    public HashMap<String,Coordinate> Coordinate; // airport mapped to their co-ordinates
    public HashMap<String,ArrayList<IntegerFour>> AirportInfo;  // airport mapped to their information
    public ArrayList<String> shortestPath; // the list of coordinates indicates the way to shortest path 
    public HashMap<Integer,String> AirportName; //index mapped to the Airport
    String [] listAirport; // holds the Airports name 
    int [] Parent;  // For getting the ShortestPath way Parent keep track the Child



    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            a = new BufferedReader(
                                                 new InputStreamReader(getAssets().open("a.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            b = new BufferedReader(
                                                 new InputStreamReader(getAssets().open("b.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        listArray = new String[1168];
        AirportName = new HashMap<Integer,String>();
        Airport = new HashMap<String,Integer>();
        Coordinate = new HashMap<String,Coordinate>();
        AirportInfo = new HashMap<String,ArrayList<IntegerFour>>();
        shortestPath = new ArrayList<String>();
        Clock = new HashMap<Integer,Time>();
        listAirport = new String[Number_Of_Airport];
        Parent = new int[Number_Of_Airport];



        String line1;
        int c = 0;// location detector
        try{
            while((line1 = AirportLocation.readLine())!=null){
                StringTokenizer s = new StringTokenizer(line1);
                String airport = s.nextToken();
                //AirportNameByString.put(c, airport);
                listAirport[c] = airport;
                Airport.put(airport,c);
                XY a = new XY(s.nextToken()); // setting the coordinates in exact form
                int x = a.x;int y = a.y;
                AirportName.put(c,airport); 
                Coordinate.put(airport, new Coordinate(x,y));
                c++;// next Airport
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }



        String line2;
        try{
            while((line2 = AirportInformation.readLine())!=null){
                String airportName = line2;
                ArrayList<IntegerFour> list = new ArrayList<IntegerFour>();
                for(int t = 0;t<3;t++){
                    StringTokenizer info = new StringTokenizer(AirportInformation.readLine());
                    String destinationAirport = info.nextToken();
                    Time slotTime = new Time(info.nextToken());
                    Time duration = new Time(info.nextToken());
                    int expense = Integer.valueOf(info.nextToken().replace("$",""));
                    list.add(new IntegerFour(destinationAirport,slotTime,duration,expense));
                }
                AirportInfo.put(airportName,list);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }




        list1 = (ListView) findViewById(R.id.list1);
        list2 = (ListView) findViewById(R.id.list2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        list1.setAdapter(adapter1);
        list2.setAdapter(adapter2);
        list1.setOnItemClickListener(new SourceSelection());
        list2.setOnItemClickListener(new DestinationSelection());

    }

    public void shortestPath(){



        int [] shortestpath = new int[Number_Of_Airport];
        Arrays.fill(shortestpath,1000000000);
        PriorityQueue<Pair> q = new PriorityQueue<Pair>();
        q.add(new Pair(Source,0));
        shortestpath[Airport.get(Source)] = 0;

        Time starting = new Time("12Hour13Minute19Second");
        Clock.put(Airport.get(Source),starting);
        while(!q.isEmpty()){
            String u = q.poll().airport;
            if(!u.equals(Destination)){
                ArrayList<IntegerFour> listOfInfo = AirportInfo.get(u);
                for(int i = 0;i<listOfInfo.size();i++){
                    String airport = listOfInfo.get(i).destinationAirport;
                    int expense = listOfInfo.get(i).expense;
                    Time slotTime = listOfInfo.get(i).slotTime;
                    Time duration = listOfInfo.get(i).duration;
                    if(shortestpath[Airport.get(airport)]>shortestpath[Airport.get(u)]+expense){
                        Parent[Airport.get(airport)] = Airport.get(u);
                        Clock.put(Airport.get(airport),slotTime);
                        shortestpath[Airport.get(airport)] = shortestpath[Airport.get(u)]+expense;
                        q.add(new Pair(airport,shortestpath[Airport.get(airport)]));
                    }
                }
            }
            else{
                break;
            }
        }       
    }

    public void getTheShortestPath(){
        int start = Airport.get(Destination);
        while(start!=Airport.get(Source)){
            shortestPath.add(AirportName.get(start));
            start = Parent[start];
       }
        shortestPath.add(AirportName.get(start));
    }




    public void Draw(){
        map = new Map(this);
        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(map);
        setContentView(scrollView); 
        //setContentView(map);
    } 

    class SourceSelection implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView temp = (TextView) arg1;
            Source = temp.getText()+"";
            if(!Source.equals("Blank") && !Destination.equals("Blank")){
                //shortestPath();
                Draw();
            }
        }
    }

    class DestinationSelection implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            TextView temp = (TextView) arg1;
            Destination = temp.getText()+"";
            if(!Source.equals("Blank") && !Destination.equals("Blank")){
                //shortestPath();
                Draw();
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public class Map extends View {
        Paint paint = new Paint();
        public Map(Context context) {
            super(context);
        }
        @Override
        public void onDraw(Canvas canvas) {
            paint.setColor(Color.RED);
            canvas.drawCircle(100, 100, 20, paint);
            canvas.drawLine(2000, 2000, 5000, 5000, paint);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // Compute the height required to render the view
            // Assume Width will always be MATCH_PARENT.
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
            setMeasuredDimension(width, height);
        }
    }
}


class Coordinate{
    int x,y;
    public Coordinate(int x,int y){
        this.x = x;
        this.y = y;
    }
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Coordinate other = (Coordinate) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}


class Time{
    int hour,minute,second;
    int totalSecond;
    public Time(String s){
        s = s.replace("Hour"," ");s = s.replace("Minute"," ");s = s.replace("Second"," ");
        StringTokenizer ss = new StringTokenizer(s);
        this.hour = Integer.valueOf(ss.nextToken());
        this.minute = Integer.valueOf(ss.nextToken());
        this.second = Integer.valueOf(ss.nextToken());
        this.totalSecond = (this.second) + ((this.minute)*60) + ((this.hour)*3600); 
    }
}


class IntegerFour{
    String destinationAirport;
    Time slotTime;// in hour,minute,second 3 of them
    Time duration;
    int expense;
    public IntegerFour(String destinationAirport,Time slotTime,Time duration,int expense){
        this.destinationAirport = destinationAirport;
        this.duration = duration;
        this.slotTime = slotTime;
        this.expense = expense;
    }
}



class Pair implements Comparable{
    String airport;
    int dollar;
    public Pair(String airport,int dollar){
        this.airport =  airport;
        this.dollar = dollar;
    }

    public int compareTo(Object o) {
        return (this.dollar - ((Pair)o).dollar);
    }

}



class XY{
    int x,y;
    public XY(String s){
        s = s.replace("(","");s = s.replace(")","");s = s.replace(","," ");
        StringTokenizer ss  =  new StringTokenizer(s);
        x = Integer.valueOf(ss.nextToken()); y = Integer.valueOf(ss.nextToken());
    }
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:paddingLeft="16dp"
   android:paddingRight="16dp" >

   <ListView
       android:id="@+id/list1"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true" />

   <ListView
       android:id="@+id/list2"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true" />

   <View
        android:id="@+id/mapview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>  

</RelativeLayout>
1
  • Your question title should be descriptive itself. Commented Feb 28, 2014 at 9:47

1 Answer 1

4

You're getting this error because the elements of listArray are null. In Java, when you initialize an array, it's just something that can store a number of objects. But its elements are null! You have to set up the array elements one by one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.