【问题标题】:There is an error in my app我的应用程序出现错误
【发布时间】:2016-05-05 09:29:44
【问题描述】:

我正在制作一个将在谷歌地图上显示多个位置的应用程序,并且我已经通过 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;
    }

}

【问题讨论】:

  • ArrayList 地址列表;你已经声明了 arraylist 但还没有实例化。
  • CSVFile.read() 方法中将ArrayList&lt;String[]&gt; addressList = new ArrayList&lt;&gt;(); 替换为addressList = new ArrayList&lt;&gt;(); 您的本地变量会隐藏您的字段,因为具有相同的名称并且您的字段始终为空
  • 感谢 hlp,现在它没有错误了。但是当我运行应用程序时。没有标记。
  • 请在您的标题上加倍努力:There is an error in my app 在读者在搜索结果或首页上看到它时并不是很有用。也不鼓励使用hlp plz 的 Txtspk - Stack Overflow 不是聊天网站。

标签: java android csv


【解决方案1】:

你已经声明了全局arrayList,但你也在read()方法中创建了相同的数组列表,所以删除声明并确保arrayList在displayArrayList()方法中不为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;
        }

    }

【讨论】:

  • 谢谢,它没有错误,但标记不显示
【解决方案2】:

在 displayArrayList() 中将 for (int x = 0; x &gt; this.addressList.size(); x++) 更改为 for (int x = 0; x &lt; this.addressList.size(); x++)

【讨论】:

    【解决方案3】:

    您的 ArrayList ArrayList&lt;String[]&gt; addressList = new ArrayList&lt;&gt;(); 的范围是本地的。在读取方法之外,您的 ArrayList 未定义,这就是您收到此错误的原因。

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

    这可能会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2022-01-04
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      相关资源
      最近更新 更多