我的应用中存在错误

时间:2016-05-05 09:29:45

标签: java android csv

我正在创建一个应用程序,将在谷歌地图上显示多个位置,我已通过csv文件阅读它。我在OnCreate和displarArrayList方法中有错误。

这是错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.w2s.where2shop.ShowAllMap$CSVFile.displayArrayList(ShowAllMap.java:267)
at com.w2s.where2shop.ShowAllMap.onCreate(ShowAllMap.java:65)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:147) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5264) 
at java.lang.reflect.Method.invoke(Native Method) 

这是onCreate

 private InputStream inputStream;
private CSVFile csvFile;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_all_map);
    setUpMapIfNeeded();
    inputStream = getResources().openRawResource(R.raw.allmap);
    csvFile = new CSVFile(inputStream);
    csvFile.read();
    try {
        csvFile.displayArrayList();
    } catch (IOException e) {
        e.printStackTrace();
    }

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_LOW_POWER)
            .setNumUpdates(1);


}

这是我读取我的csv文件的地方,在CsvFile类中我得到了displayarrayItem方法。

public class CSVFile {
    InputStream inputStream;
    ArrayList<String[]> addressList;


    public CSVFile(InputStream is) {
        this.inputStream = is;
    }

    public ArrayList<String[]> read() {
        ArrayList<String[]> addressList = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] row = line.split(",");
                addressList.add(row);
            }
        } catch (IOException e) {
            Log.e("Main", e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                Log.e("Main", e.getMessage());
            }
        }
        return addressList;
    }

    public ArrayList<String[]> displayArrayList() throws IOException {
        for (int x = 0; x > this.addressList.size(); x++) {

            Geocoder gc = new Geocoder(ShowAllMap.this);
            List<Address> list = gc.getFromLocationName(addressList.get(x).toString(), 1);

            if (list.size() > 0) {
                Address add = list.get(0);
                double lat = add.getLatitude();
                double lng = add.getLongitude();
                LatLng latLng = new LatLng(lat, lng);

                MarkerOptions options = new MarkerOptions()
                        .position(latLng);
                mMap.animateCamera(CameraUpdateFactory.zoomIn());
                mMap.addMarker(options);


            }
        }
        return addressList;
    }

}

3 个答案:

答案 0 :(得分:1)

您已声明了全局arrayList,但您还在read()方法中创建了相同的数组列表,因此请删除该声明,并确保displayList中的arrayList不为null。

 public class CSVFile {
        InputStream inputStream;
        ArrayList<String[]> addressList;


        public CSVFile(InputStream is) {
            this.inputStream = is;
        }

        public ArrayList<String[]> read() {
             addressList = new ArrayList<>();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] row = line.split(",");
                    addressList.add(row);
                }
            } catch (IOException e) {
                Log.e("Main", e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.e("Main", e.getMessage());
                }
            }
            return addressList;
        }

        public ArrayList<String[]> displayArrayList() throws IOException {
        if(addressList != null){
            for (int x = 0; x > this.addressList.size(); x++) {

                Geocoder gc = new Geocoder(ShowAllMap.this);
                List<Address> list = gc.getFromLocationName(addressList.get(x).toString(), 1);

                if (list.size() > 0) {
                    Address add = list.get(0);
                    double lat = add.getLatitude();
                    double lng = add.getLongitude();
                    LatLng latLng = new LatLng(lat, lng);

                    MarkerOptions options = new MarkerOptions()
                            .position(latLng);
                    mMap.animateCamera(CameraUpdateFactory.zoomIn());
                    mMap.addMarker(options);


                }
            }
            }
            return addressList;
        }

    }

答案 1 :(得分:1)

在displayArrayList()

中将for (int x = 0; x > this.addressList.size(); x++)更改为for (int x = 0; x < this.addressList.size(); x++)

答案 2 :(得分:0)

ArrayList ArrayList<String[]> addressList = new ArrayList<>();的范围是本地的。在read方法之外,您没有定义ArrayList,这就是您收到此错误的原因。

溶胶。在调用read()和displayArraylist()方法之前,在onCreate中声明您的ArrayList。

这可能会对你有帮助。