Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:06:47+00:00 2026-06-13T16:06:47+00:00

I’m creating a small app in which you can add or delete a table

  • 0

I’m creating a small app in which you can add or delete a table row by button click. Whenever I click the “add line” button, the button adds a table row to the layout and does what it’s supposed to do. However, the “delete line” doesn’t remove the the newly added table row, but it does decrement (int count) like it’s supposed to. I’ve worked and searched on it for about 4 to 5 hours now and I’m having no luck. Here’s the code:

int count = 0;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.activity_main);
    Button buttonAdd = (Button) findViewById(R.id.button1);
    Button buttonDel = (Button) findViewById(R.id.button2);
    buttonAdd.setOnClickListener(this);
    buttonDel.setOnClickListener(this);
}



public void onClick(View v) {
    TableLayout tableLayout1;
    EditText editText1;
    EditText editText2;
    TableRow tempRow;
    EditText tempText1;
    EditText tempText2;
    TableRow tableRow1;

    tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    tempRow = new TableRow(MainActivity.this);
    tempText1 = new EditText(MainActivity.this);
    tempText2 = new EditText(MainActivity.this);
    tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
    tempText2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
    tableRow1 = (TableRow) findViewById(R.id.tableRow1);

    tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    tempText1.setLayoutParams(editText1.getLayoutParams());
    tempText2.setLayoutParams(editText2.getLayoutParams());
    switch(v.getId()){
    case R.id.button1:
        if(count != 9){
            count++;

            tempRow.addView(tempText1);
            tempRow.addView(tempText2);
            tableLayout1.addView(tempRow);
        } else {
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setTitle("Error");
            alertDialog.setMessage("You can only have 10 rows!");

            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                  alertDialog.dismiss();
               }
            });

            alertDialog.show();
        }
        break;
    case R.id.button2:
        if(count != 0){
            count--;
            tempRow.removeView(tempText1);
            tempRow.removeView(tempText2);
            tableLayout1.removeView(tempRow);
            //tableLayout1.invalidate();

        } else {
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setTitle("Error");
            alertDialog.setMessage("You must have at least one row!");

            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                  alertDialog.dismiss();
               }
            });

            alertDialog.show();
        }
        break;
        default:
            //Do something;
    }

}    

.

10-30 21:22:23.379: E/AndroidRuntime(3970): FATAL EXCEPTION: main
10-30 21:22:23.379: E/AndroidRuntime(3970): java.lang.NullPointerException
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.hopegpacalc.MainActivity.onClick(MainActivity.java:57)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.view.View.performClick(View.java:3511)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.view.View$PerformClick.run(View.java:14105)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Handler.handleCallback(Handler.java:605)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Looper.loop(Looper.java:137)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-30 21:22:23.379: E/AndroidRuntime(3970): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 21:22:23.379: E/AndroidRuntime(3970): at java.lang.reflect.Method.invoke(Method.java:511)
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-30 21:22:23.379: E/AndroidRuntime(3970): at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:06:48+00:00Added an answer on June 13, 2026 at 4:06 pm

    It is a matter of organization, try this:

    public void onClick(View v) {
        // These should be class variables, like count, you don't need to re-find it on every click
        TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
        EditText editText1 = (EditText) findViewById(R.id.editText1);
        EditText editText2 = (EditText) findViewById(R.id.editText2);
    
        switch(v.getId()){
        case R.id.button1:
            if(count != 9){
                count++;
    
                // Create the row only when the add button is clicked
                TableRow tempRow = new TableRow(MainActivity.this);
                EditText tempText1 = new EditText(MainActivity.this);
                EditText tempText2 = new EditText(MainActivity.this);
                tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
                tempText2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
    
                tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                tempText1.setLayoutParams(editText1.getLayoutParams());
                tempText2.setLayoutParams(editText2.getLayoutParams());
    
                tempRow.addView(tempText1);
                tempRow.addView(tempText2);
                tableLayout1.addView(tempRow);
            } else {
                // Consider using Activity's showDialog() method, to reuse this "error" dialog rather than create a new one every time
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Error");
                alertDialog.setMessage("You can only have 10 rows!");
    
                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });
    
                alertDialog.show();
            }
            break;
        case R.id.button2:
            if(count != 0){
                // Remove the last row at count or use "tableLayout1.getChildCount() - 1"
                tableLayout1.removeView(tableLayout1.getChildAt(count));
                count--;
            } else {
                // Consider reusing the other "error" Dialog and simply changing the message
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Error");
                alertDialog.setMessage("You must have at least one row!");
    
                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });
    
                alertDialog.show();
            }
            break;
        default:
            //Do something;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.