致命异常:主要是java.lang.NullPointerException

时间:2015-07-16 14:29:58

标签: java android exception nullpointerexception

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

我得到致命异常/空指针异常,我不知道为什么。

如果您要求我发布程序中的更多文件,请告诉我们 提前致谢

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/android/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 的 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 android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您的String JSONObject对象已创建,但它是一个null对象。您永远不会为其分配实际值。然后,当您尝试使用它来创建allowDrop = (ev) -> ev.preventDefault() drag = (ev) -> ev.dataTransfer.setData("image", ev.target.id) drop = (ev) -> ev.preventDefault() data = ev.dataTransfer.getData("image") ev.target.appendChild(document.getElementById(data)) 时,将抛出空指针异常。