有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓致命异常:主java。lang.NullPointerException

我正在做一个学校项目来结束我的课程,这是一个与php和mysql数据库交互的小程序,用于检索数据并将其导入安卓应用程序

我得到了一个致命的异常/空指针异常,我不知道为什么

如果你需要我发布更多的程序文件,请告诉我 提前谢谢

Receitas。Java

public class receitas extends ActionBarActivity {

String myJSON;

private static final String TAG_RESULTS="result";
private static final String TAG_ID_RECIPE="id_recipe";
private static final String TAG_TITLE="title";
private static final String TAG_INGREDIENTS="ingredients";
private static final String TAG_STEPS="steps";

JSONArray recipes = null;

ArrayList<HashMap<String, String>> recipelist;

ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receitas);
    list = (ListView) findViewById(R.id.ListView);
    recipelist = new ArrayList<HashMap<String, String>>();
    getData();
}

protected void showList(){
    try{
        JSONObject jsonObj = new JSONObject(myJSON);
        recipes = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<recipes.length();i++){
            JSONObject c = recipes.getJSONObject(i);
            String id_recipe = c.getString(TAG_ID_RECIPE);
            String title = c.getString(TAG_TITLE);
            String ingredients = c.getString(TAG_INGREDIENTS);
            String steps = c.getString(TAG_STEPS);

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

            recipes.put(TAG_ID_RECIPE,id_recipe);
            recipes.put(TAG_TITLE,title);
            recipes.put(TAG_INGREDIENTS,ingredients);
            recipes.put(TAG_STEPS,steps);

            recipelist.add(recipes);
        }

        ListAdapter adapter = new SimpleAdapter(
                receitas.this, recipelist, R.layout.list_item,
                new String[]{TAG_ID_RECIPE,TAG_TITLE},
                new int[]{R.id.id_recipe, R.id.title}
        );

        list.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://localhost:8080/安卓/get-recipe-all.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_receitas, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

这是我的日志 Logcat

07-16 14:25:39.493  20431-20431/com.example.kuro.cheffridge E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:154)
        at org.json.JSONObject.<init>(JSONObject.java:171)
        at com.example.kuro.cheffridge.receitas.showList(receitas.java:55)
        at com.example.kuro.cheffridge.receitas$1GetDataJSON.onPostExecute(receitas.java:127)
        at com.example.kuro.cheffridge.receitas$1GetDataJSON.onPostExecute(receitas.java:88)
        at 安卓.os.AsyncTask.finish(AsyncTask.java:631)
        at 安卓.os.AsyncTask.access$600(AsyncTask.java:177)
        at 安卓.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at 安卓.os.Handler.dispatchMessage(Handler.java:99)
        at 安卓.os.Looper.loop(Looper.java:137)
        at 安卓.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    已创建字符串myJSON对象,但它是空对象。您从未为其指定实际值。然后,当您尝试使用它来创建JSONObject时,将引发空指针异常