从MySQL创建数组时的NPE

时间:2017-11-01 20:44:08

标签: java android mysql arrays json

我想从MySQL创建多个数据数组,但是收到了NullPointerException。可在此处找到错误:JSONObject jsonResponse = new JSONObject ...

NewsFeed.java

public class NewsFeed extends Fragment {


public NewsFeed() {
    // Required empty public constructor
}

String T = "check";

TextView newsTime;
TextView newsTitle;
TextView newsSection;

//TextView txtT;
//String title2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rv = inflater.inflate(R.layout.newsfeed, container, false);

    LayoutInflater infnews = getLayoutInflater();

    LinearLayout linearLayout = 
(LinearLayout)rv.findViewById(R.id.linear_newsfeed);
    final View view1 = infnews.inflate(R.layout.newsfront, linearLayout, 
false);

    view1.getLayoutParams().width = 
RelativeLayout.LayoutParams.MATCH_PARENT;
    linearLayout.addView(view1);

    newsTime = (TextView)rv.findViewById(R.id.newsTime);
    newsTitle = (TextView)rv.findViewById(R.id.newsTitle);
    newsSection = (TextView)rv.findViewById(R.id.newsSection);
    Typeface GothamProFont = 
Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamPro.ttf");
        newsTime.setTypeface(GothamProFont);
        newsTitle.setTypeface(GothamProFont);
        newsSection.setTypeface(GothamProFont);

    newsFeedDisplay();

    view1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), NewsBody.class);
            startActivity(intent);
        }
    });

    return rv;
}


void newsFeedDisplay () {

    Response.Listener<String> responseListener = new 
Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new 
JSONObject(jsonResult.substring(jsonResult.indexOf("{"), 
jsonResult.lastIndexOf("}")+1));
                JSONArray jsonMainNode = jsonResponse.optJSONArray("news");

                final ArrayList<HashMap<String,String>> MyArrList = new 
ArrayList<HashMap<String, String>>();

                HashMap<String,String> map;

                for(int i=0; i<jsonMainNode.length(); i++){
                    JSONObject c = jsonMainNode.getJSONObject(i);
                    Log.i(T, c.toString());

                    map = new HashMap<String,String>();

                    map.put("title", c.getString("title"));
                    map.put("time", c.getString("time"));
                    map.put("section", c.getString("section"));

                    MyArrList.add(map);
                    Log.i(T, MyArrList.toString());

                    SimpleAdapter sAdap;
                    //sAdap = new SimpleAdapter(getActivity(), MyArrList, 
R.layout.activity_column, new String[]{"MemberID","Name","city"}, new int[]
{R.id.ColMemberID, R.id.ColName, R.id.Colcity});

                    //listView.setAdapter(sAdap);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
    NewsFeedRequest3 newsFeedRequest3 = new 
NewsFeedRequest3(responseListener);
    RequestQueue queue = Volley.newRequestQueue(getContext());
    queue.add(newsFeedRequest3);
}
}

NewsFeedRequest3

public class NewsFeedRequest3 extends StringRequest{

private static final String REGISTER_REQUEST_URL = 
"https://blazhko.000webhostapp.com/test.php";
private Map<String, String> params;

public NewsFeedRequest3(Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
      params = new HashMap<>();
      //params.put("section", section);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

错误:

  

java.lang.NullPointerException:尝试调用虚方法&#39; int java.lang.String.indexOf(java.lang.String)&#39;在null对象引用上                                                                       在blazhko.sportarena.main_screen.NewsFeed $ 3.onResponse(NewsFeed.java:145)                                                                       在blazhko.sportarena.main_screen.NewsFeed $ 3.onResponse(NewsFeed.java:141)

我做错了什么?请帮帮我。

1 个答案:

答案 0 :(得分:0)

根据你的问题

  

java.lang.NullPointerException:尝试调用虚方法&#39; int java.lang.String.indexOf(java.lang.String)&#39;在

的空对象引用上

你可以这样做。

1.您的代码中是否有正确的responsejsonResultresponse

2.使用NullPointerException

进行处理

3.确保您的代码包含{}

你可以这样做。如果您使用jsonResult作为结果,则可以在代码中更改为jsonResult

@Override
public void onResponse(String response) {
    // String response = "hello{\"key\":\"value\"}world"; if you response like this 
    try {
        if (response != null) {
            JSONObject jsonResponse = new JSONObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
            // do something here
            Log.e("TAG", response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}