无法读取json文件

时间:2017-02-25 11:08:45

标签: java android json nullpointerexception

这是我读取json文件的方法。

private void LoadTabaksFromJson() {

    InputStream raw = mContext.getResources().openRawResource(R.raw.tabaks);
    Reader reader = new BufferedReader(new InputStreamReader(raw));


    ListOfTabaks listOfTodos = new Gson().fromJson(reader, ListOfTabaks.class);
    List<Tabak> todoList = listOfTodos.getTodoArrayList();

    for (Tabak item: todoList){
        mDataBase.insert(TabakTable.NAME,null,getContentValues(item));
    }
}


public class ListOfTabaks {

    protected ArrayList<Tabak> tabakArrayList;

    public ArrayList<Tabak> getTodoArrayList(){
        return tabakArrayList;
    }
}

和执行

  

引起:java.lang.NullPointerException:尝试调用接口   方法&#39; java.util.Iterator java.util.List.iterator()&#39;在null对象上   参考                                                                                  在   com.hookah.roma.hookahmix.TabakLab.LoadTabaksFromJson(TabakLab.java:61)                                                                                  在com.hookah.roma.hookahmix.TabakLab。(TabakLab.java:32)                                                                                  在com.hookah.roma.hookahmix.TabakLab.get(TabakLab.java:37)                                                                                  在   com.hookah.roma.hookahmix.TabakListFragment.updateUI(TabakListFragment.java:38)                                                                                  在   com.hookah.roma.hookahmix.TabakListFragment.onCreateView(TabakListFragment.java:32)                                                                                  在   android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)                                                                                  在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)                                                                                  在   android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)                                                                                  在   android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)

和json文件:

  {
  "tabaksArrayList":[
    {
      "name":"Абрикос",
      "description":"Со вкусом Абрикоса",
      "rating":"4.1",
      "favourite":"1",
      "family":"Al fakher"
    },
    {
      "name":"Ананас",
      "description":"Со вкусом Ананаса",
      "rating":"4.1",
      "favourite":"1",
      "family":"Al fakher"
    },
    {
      "name":"Апельсин",
      "description":"Со вкусом Апельсина",
      "rating":"4.1",
      "favourite":"1",
      "family":"Al fakher"
    },
    {
      "name":"Апельсин с мятой",
      "description":"Со вкусом Апельсина с мятой",
      "rating":"4.1",
      "favourite":"1",
      "family":"Al fakher"
    },

4 个答案:

答案 0 :(得分:0)

请将您的json文件放在资源文件夹

使用AsyncTask来保护ANR像situtation

lnS1 ~ φ [4.50703, 0.25]

然后解析

mean price = 80
σ = 0.5
below price = 70

X ~ N(80, 0.5)
Z = (70 – 80)/0.5 = -20
P(X ≤ 70) = P(Z ≤ = -20)

ParseJsonFileAsync.java

的详细信息

答案 1 :(得分:0)

看起来您没有初始化您的ArrayList,请尝试更改:

protected ArrayList<Tabak> tabakArrayList;

有:

protected ArrayList<Tabak> tabakArrayList = new ArrayList<>();

答案 2 :(得分:0)

看起来你的json架构问题,我猜listOfTodos返回null。您可以参考this来生成架构。

但有时候这些工具会让我们感到困惑,所以我试图像这样手动创建你的架构:

TabakRoot.java

public class TabakRoot {

@SerializedName("tabaksArrayList")
private List<TabakItem> tabakItem = null;

public List<TabakItem> getTabakItem() {
    return tabakItem;
}}

TabakItem.java

public class TabakItem {

@SerializedName("family")
@Expose
private String tabakFamily;

public String getTabakFamily() {
    return tabakFamily;
}}

最后

TabakRoot listOfTodos = new Gson().fromJson(reader, TabakRoot.class);
List<TabakItem> todoList = listOfTodos.getTabakItem();

答案 3 :(得分:0)

您没有初始化tabakArrayList,请在ListOfTabaks中添加构造函数,如下所示

public ListOfTabaks{
    tabakArrayList = new ArrayList<>();
}

你应该没问题