PostExecute上的NullPointerException

时间:2018-03-21 12:01:52

标签: java android nullpointerexception

我尝试在TextView上显示值,但如果我想在TextViews上填充PostExecute,我会收到NullPointerException。我已检查LIST<ArrayList<Hashmap>>是否为Log为空,但已填充。

我的代码:

public class DBResult extends MainActivity {

    //Erstellen eines JSON Parser Objekts
    JSONParser jParser = new JSONParser();
    ArrayList<HashMap<String,String>> LIST = new ArrayList<>();

    // url to get all products list
    private static String url_dataget = "DATAGET_PHP";

    // JSON Node 
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_NAME = "name";
    private static final String TAG_INN = "inn";
    private static final String TAG_AnalgetikaGroup= "analgetikagroup";
    private static final String TAG_WHOLevel = "wholevel";
    private static final String TAG_DailyDose = "dailydose";
    private static final String TAG_contraindication = "contraindication";
    private static final String TAG_SideEffect = "sideeffect";
    private static final String TAG_GastricProtection = "gastricprotection";


    // products JSONArray
    JSONArray products = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Background Thread
        new LoadMed().execute();

    }
    class LoadMed extends AsyncTask<String,String,String>{
    protected String doInBackground(String...args){

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_dataget, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("Medic: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable

                    String name = c.getString(TAG_NAME);
                    String INN = c.getString(TAG_INN);
                    String AnalgetikaGroup = c.getString(TAG_AnalgetikaGroup);
                    String WHOLevel = c.getString(TAG_WHOLevel);
                    String DailyDose = c.getString(TAG_DailyDose);
                    String contraindication = c.getString(TAG_contraindication);
                    String SideEffect = c.getString(TAG_SideEffect);
                    String GastricProtection = c.getString(TAG_GastricProtection);
                    HashMap<String,String> map = new HashMap<>();
                    map.put(TAG_NAME,name);
                    map.put(TAG_INN,INN);
                    map.put(TAG_AnalgetikaGroup,AnalgetikaGroup);
                    map.put(TAG_WHOLevel,WHOLevel);
                    map.put(TAG_DailyDose,DailyDose);
                    map.put(TAG_contraindication,contraindication);
                    map.put(TAG_SideEffect,SideEffect);
                    map.put(TAG_GastricProtection,GastricProtection);
                    Log.d("LIL",map.toString());
                    LIST.add(map);





                }
            }
        }catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
     protected void onPostExecute(String file_url) {

        runOnUiThread(new Runnable() {
            public void run() {
                Log.d("sad", LIST.toString());
                TextView mednamefield = findViewById(R.id.medname);
                TextView sideeffectfield = findViewById(R.id.sideeffect);
                TextView medWHOfield = findViewById(R.id.wholevel);
                TextView medGastricProtectionfield = findViewById(R.id.gastricprotecio);
                TextView medINNfield = findViewById(R.id.INN);
                TextView medDailyDosefield = findViewById(R.id.dailydose);
                TextView medanalgeticafield = findViewById(R.id.analgeticagroup);
                TextView contraindicationfield = findViewById(R.id.contraindication);


                mednamefield.setText(LIST.get(0).get(TAG_NAME));
                sideeffectfield.setText(LIST.get(1).get(TAG_SideEffect));
                medWHOfield.setText((LIST.get(2).get(TAG_WHOLevel)));
                medGastricProtectionfield.setText("Magenschutz empfohlen: " + LIST.get(3).get(TAG_GastricProtection));
                contraindicationfield.setText(LIST.get(4).get(TAG_contraindication));
                medINNfield.setText("INN: " + LIST.get(5).get(TAG_INN));
                medDailyDosefield.setText(LIST.get(6).get(TAG_DailyDose));
                medanalgeticafield.setText(LIST.get(7).get(TAG_AnalgetikaGroup));

            }

    });
    }


    }

LOGCAT错误:

FATAL EXCEPTION: main
Process: com.example.simon.test123, PID: 4019
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.simon.test123.DBResult$LoadMed$1.run(DBResult.java:128)
at android.app.Activity.runOnUiThread(Activity.java:5866)
at com.example.simon.test123.DBResult$LoadMed.onPostExecute(DBResult.java:113)
at com.example.simon.test123.DBResult$LoadMed.onPostExecute(DBResult.java:55)
at android.os.AsyncTask.finish(AsyncTask.java:667)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

注意: 我已经阅读了文章“什么是NullPointerException”以及有关stackoverflow上此主题的一些其他文章,但我仍然找不到合适的解决方案。

3 个答案:

答案 0 :(得分:2)

您需要在setContentView(R.layout.your_xml);

中添加:onCreate

更新:

对于您遇到的seceond问题,您实际上将实例map放在LIST上  不是地图的价值,你需要做这样的事情:

List<String> list = new ArrayList<Sting>(map.values());

答案 1 :(得分:1)

您错过了setContentView();

中的onCreate()
  setContentView(R.layout.your_xml);

的onCreate:

@Override
public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_xml);

    //Background Thread
    new LoadMed().execute();

}

答案 2 :(得分:0)

你不能在setContentView方法中添加活动..

    setContentView(R.layout.layout_issue);//your layout.