-1

I tried to display Values on TextView but if I want to populate my TextViews on PostExecute I get a NullPointerException. I already checked if the LIST<ArrayList<Hashmap>> is empty with Log but it is populated.

My code:

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 ERROR:

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)

NOTE: I already read the article "What is a NullPointerException" and a few other articles about this topic on stackoverflow but I still can't find the right solution.

3 Answers3

2

You need to add : setContentView(R.layout.your_xml); inside onCreate

UPDATE:

for the seceond issue you have, you actually putting the instance map on LIST not the values of the map, you need to do somthing like this instead :

List<String> list = new ArrayList<Sting>(map.values());
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
1

You missed setContentView(); in 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();

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Venky G
  • 180
  • 7
0

you can not add in activity in setContentView Method..

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