0

It is the class my MainClass. I had create TableLayout and added one TableRow to my TableLayout. There are 3 TextView in my TableRow. I want to add TableRow automatically when data insert

For That I had created AsyncTask to get data from php and turn it to list(model list) and then want to send it to my table.

public class MainActivity extends AppCompatActivity {
private TableLayout myTable;
private TableRow tr;
private TextView txtId, txtName, txtSurname, txtAge, txtUsername, txtPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myTable = (TableLayout) findViewById(R.id.myTable);
    String type="geldim";
    BackgroundProcess backgroundProcess=new BackgroundProcess(this);
    backgroundProcess.execute(type);


}

public void setData(List<Test> getList) {
    for (int i = 0; i < getList.size(); i++) {
        tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        txtId = new TextView(this);
        txtName = new TextView(this);
        txtSurname = new TextView(this);
        txtAge = new TextView(this);
        txtUsername = new TextView(this);
        txtPassword = new TextView(this);
        System.out.println(getList.get(i).getName());
        txtId.setText(getList.get(i).getId());
        txtName.setText(getList.get(i).getName());
        txtSurname.setText(getList.get(i).getSurname());
        txtAge.setText(getList.get(i).getAge());
        txtUsername.setText(getList.get(i).getUsername());
        txtPassword.setText(getList.get(i).getPassword());

        tr.addView(txtId);
        tr.addView(txtName);
        tr.addView(txtSurname);
        tr.addView(txtAge);
        tr.addView(txtUsername);
        tr.addView(txtPassword);
        myTable.addView(tr);
    }
}

}

And it is my background class:

public class BackgroundProcess extends AsyncTask<String,Void,String> {
 Context context;
 private MainActivity mainActivity=new MainActivity();

BackgroundProcess(Context ctx) {
    context = ctx;
}

@Override
protected String doInBackground(String... params) {
    String a=params[0];
    String myLink="http://192.168.31.129/showData.php";

    if(a.equals("geldim")){
        try {
            System.out.println(1111);
            URL url=new URL(myLink);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("GET");

            int respondCode=httpURLConnection.getResponseCode();
            if(respondCode==HttpURLConnection.HTTP_OK) {
                System.out.println(2223);
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                System.out.println(3333);
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                return result;
            }else {
                return ("unsuccessful");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }else {
        return  null;
    }
    return  null;


}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onPostExecute(String value) {
    System.out.println(value);
    List<Test> getList=new ArrayList<>();
    if(value!=null){
        try {
            JSONObject object=new JSONObject(value);
            JSONArray array=object.getJSONArray("menim");
            System.out.println("say bu qederdir"+array.length());
            for(int i=0;i<array.length();i++){
                JSONObject result=array.getJSONObject(i);
                Test test=new Test(result.getInt("id"),result.getString("name"),result.getString("surname"),
                        result.getInt("age"),result.getString("username"),result.getString("password"));
                getList.add(test);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(getList);
        mainActivity.setData(getList);
    }
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

I got this error when i run it:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.view.ViewConfiguration.get(ViewConfiguration.java:431)
    at android.view.View.<init>(View.java:4535)
    at android.view.View.<init>(View.java:4668)
    at android.widget.TextView.<init>(TextView.java:824)
    at android.widget.TextView.<init>(TextView.java:818)
    at android.widget.TextView.<init>(TextView.java:814)
    at android.widget.TextView.<init>(TextView.java:810)
    at com.example.firstprog.TabOne.setData(TabOne.java:110)
    at com.example.firstprog.BackgroundProcess.onPostExecute(BackgroundProcess.java:96)
    at com.example.firstprog.BackgroundProcess.onPostExecute(BackgroundProcess.java:22)
    at android.os.AsyncTask.finish(AsyncTask.java:695)
    at android.os.AsyncTask.-wrap1(Unknown Source:0)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  • how are you not getting an out of memory error is what i want to know. You are creating a new main activity in your background class, which creates a new background class which creates a new main activity etc forever – John Lord Nov 10 '19 at 06:05
  • my computer has 16gb ram and 1 tb hdd 256 gb ssd. maybe it is so powerful. But my problem is not memory error, it is null pointer error what i searched everwhere in the internet but no one know it and never someone asked question like that! – Mehman Hummetli Nov 10 '19 at 12:04
  • i would suggest you will get one once you fix the other error. Debugging inside an async class is pretty difficult. I would suggest you remove the threading until you get the problem solved. You'll get a more useful logcat. Currently we really can't even tell which control is null. – John Lord Nov 10 '19 at 19:32
  • also yes they have. https://stackoverflow.com/questions/39293294/attempt-to-invoke-virtual-method-android-content-context-getresources-on-a-n – John Lord Nov 10 '19 at 19:34
  • thank you very much. I found the error. But really dont know what other way can i use. Problem is that async task is working late and you need to write codes other button and task must write in onCreate function. Because async task must need to finish and then you can work on it. – Mehman Hummetli Nov 10 '19 at 20:21
  • and I wanna say that wasnt found answer what you sent me - stackoverflow.com/questions/39293294/… – Mehman Hummetli Nov 10 '19 at 20:21
  • and thanks for to try help me – Mehman Hummetli Nov 10 '19 at 20:22
  • you can easily tell when the task is finished using the onpostexecute. see here: https://stackoverflow.com/questions/21649037/how-to-know-when-asynctask-is-finished. That's where you could put the rest of your code that needs to wait on it. – John Lord Nov 10 '19 at 20:43
  • the handler class is mean that wait codes to his (AsyncTask) finishing? – Mehman Hummetli Nov 10 '19 at 21:10
  • not 100% sure what you are asking (language barrier) but ... you are already extending asynctask so you should be able to just implement onpostexecute. – John Lord Nov 10 '19 at 21:20
  • first of all, sorry for my language. I mean, handler is that to make my codes wait until onpostexecute done his work? or what? – Mehman Hummetli Nov 10 '19 at 22:28
  • you don't need to apologize for knowing multiple languages. Yes. The onpostexecute could either run your final code directly or notify the UI thread that IT can run it. There have to be plenty of examples you can look at. It's an override function so you simply need to add it. – John Lord Nov 10 '19 at 22:41
  • Thank you very much John Lord how can i give you mark in this site? Because want to give mark 5+. I absolutely understand what is that. I created fragment that was same problem in there too. But finally i understand why that was a problem – Mehman Hummetli Nov 10 '19 at 22:57
  • and can you give me a advice? what can i read for increase my knowledge about android and database relationship? Do i need to learn about REST API? – Mehman Hummetli Nov 10 '19 at 23:01
  • if you want to rep my, you can upvote my comments. that's worth a point. It's unrelated to your original question so i didn't post it as an answer – John Lord Nov 11 '19 at 00:00
  • ok John thank you for you attention. Do you use facebook? Can you send me your fb link? – Mehman Hummetli Nov 11 '19 at 09:54

0 Answers0