Android - 不确定为什么我的数组为null

时间:2017-08-13 20:47:41

标签: java android arrays

我正试图围绕Asynctasks和线程等旋转我的想法,我认为我有一个很好的解决方案,我在Asynctask类上进行所有计算,使用onCallComplete将它们发送回我的活动,添加所有将变量转换为单独的数组,然后将该数组发送到List View适配器。但是,由于某种原因我似乎无法掌握,Android正在向我投掷java.lang.NullPointerException: Attempt to read from null array。我确定这很明显,但我不知道如何解决它,所以任何建议都会非常感谢,谢谢!

活动的onCallComplete(发生错误)

 public void onCallComplete(int listSize, String[] titles, String[] prices, Bitmap[] images, String[] productURLs ){
      ItemList[]itemArray= new ItemList[listSize];
      int i =0;
      while(i<listSize){
            itemArray[i] = new ItemList();
            itemArray[i].setTitle(titles[i]); //error occurs here
            itemArray[i].setPrice(prices[i]);
            itemArray[i].setImage(images[i]);
            itemArray[i].setProductURL(productURLs[i]);
            i++;
      }
      ListAdapter adapter = new CustomAdapter(this, itemArray);
      ListView productListView = (ListView)findViewById(R.id.productListView);
      productListView.setAdapter(adapter);
 }

ItemList类:

public class ItemList {
    private String title;
    private String price;
    private Bitmap image;
    private String productURL;


    public ItemList(){
        this.title=null;
        this.price=null;
        this.image=null;
        this.productURL=null;
    }

    void setTitle(String title){
        this.title = title;
    }

    void setImage(Bitmap image){
        this.image = image;
    }

    void setPrice(String price){
        this.price = price;
    }

    void setProductURL(String productURL){
        this.productURL = productURL;
    }

    String getTitle(){
        return title;
    }

    String getPrice(){
        return price;
    }

    Bitmap getImage(){
        return image;
    }

    String getProductURL(){
        return productURL;
    }
}

编辑:因此经过一些实验和来自Rajendran的指针我得出的结论是我的AsyncTask计算返回null(如图所示,Asynctask对我来说总是一个问题)。这是AsyncTask的代码。

public class GetProductAttributes extends AsyncTask<Object, Object, Void> {
    OnCallCompleteCallBack callback;
    String url;
    int listSize;
    int index;
    String[] productURLs;
    String[] prices;
    String[] titles;
    String imageSRC;
    Bitmap[] images;
    ImageView productView;
    int listSize;
    int result;

    public GetProductAttributes(String url, int index, OnCallCompleteCallBack callback) {
        this.url = url;
        this.index = index;
        this.callback = callback;
    }
protected Void doInBackground(Object... voids) {
        //Create JSoup connection
        try { 
        Document doc = Jsoup.connect(url).get();
        String link = doc.select("h2#s-result-count").first().text();
        System.out.println(link);
        System.out.println(link.substring(1));
        if (link.substring(1, 2).equals("-")) {
            System.out.println("run1");
            listSize = Integer.parseInt(link.substring(2, 3));
            System.out.println(listSize);
            try {
                listSize = Integer.parseInt(link.substring(2, 4));
                System.out.println(listSize);
            } catch (Exception e) {
            }
        } else {
            System.out.println("run2");
            listSize = Integer.parseInt(link.substring(0, 1));
            System.out.println(listSize);
            try {
                listSize = Integer.parseInt(link.substring(0, 2));
                System.out.println(listSize);
            } catch (Exception e) {
            }
        }

            titles = new String[listSize];
            prices = new String[listSize];
            productURLs = new String[listSize];
            images = new Bitmap[listSize];
            int i = 0;
            while (i < listSize) {
                Elements basicLink = doc.select("div.showRightCol")
                        .select("div.leftCol")
                        .select("div.a-row.s-result-list-parent-container")
                        .select("ul.s-result-list.s-col-1.s-col-ws-1.s-result-list-hgrid.s-height-equalized.s-list-view.s-text-condensed")
                        .select("li[id=result_" + i + "]")
                        .select("div.s-item-container")
                        .select("div.a-fixed-left-grid")
                        .select("div.a-fixed-left-grid-inner");//start here to get to everything

                Element element = basicLink.select("a.a-link-normal.s-access-detail-page.s-color-twister-title-link.a-text-normal").first();
                String title = element.attr("title");
                //System.out.println("Title is: " + title);
                titles[i] = title;

//          Gets product URL and image source
                Elements longLink = basicLink.select("div.a-fixed-left-grid-col.a-col-left")
                        .select("div.a-row")
                        .select("div.a-column.a-span12.a-text-center")
                        .select("a.a-link-normal.a-text-normal");
                String productURL = longLink.attr("href");
                //System.out.println(productURL);
                productURLs[i] = productURL;
                imageSRC = longLink.select("img.s-access-image.cfMarker").attr("src");
                //System.out.println(imageSRC);
                images[i] = getImage(imageSRC);

                //Gets price
                String price = basicLink.select("div.a-row")
                        .select("div.a-column.a-span7")
                        .select("div.a-row.a-spacing-mini")
                        .select("div.a-row.a-spacing-none")
                        .select("a.a-size-small.a-link-normal.a-text-normal")
                        .select("span.a-size-base.a-color-base").first().text();
                System.out.println(price);
                prices[i]=price;
                i++;
            }
            //Gets title

        } catch (Exception e) {
        }
        return null;
    }

1 个答案:

答案 0 :(得分:-1)

以下是您的答案“尝试从空数组中读取”。这意味着这个“titles [i]”返回null。