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

  • Home
  • SEARCH
  • 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 8583637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:35:04+00:00 2026-06-11T21:35:04+00:00

I came across a concept while surfing to create the dynamic table rows and

  • 0

I came across a concept while surfing to create the dynamic table rows and add content to that. Describing below the code
This code throws NullPointerException.

Layout:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:stretchColumns="0,1"
                android:id="@+id/tabledisplay" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="match_parent">
        </TableLayout>

code

public void display_table(){
    try{
    TableLayout tl = (TableLayout)findViewById(R.id.tabledisplay);

    DBConnection db= new DBConnection();
    Connection con=db.getConnection();
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select report_name,report_createddate,user_name from reports");
    TableRow tr_head = new TableRow(this);
    tr_head.setId(10);
    tr_head.setBackgroundColor(Color.GRAY);
    tr_head.setLayoutParams(new LayoutParams(
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));
    TextView label_date = new TextView(this);
    label_date.setId(20);
    label_date.setText("DATE");
    label_date.setTextColor(Color.WHITE);
    label_date.setPadding(5, 5, 5, 5);
    tr_head.addView(label_date);// add the column to the table row here

    TextView label_weight_kg = new TextView(this);
    label_weight_kg.setId(21);// define id that must be unique
    label_weight_kg.setText("Wt(Kg.)"); // set the text for the header 
    label_weight_kg.setTextColor(Color.WHITE); // set the color
    label_weight_kg.setPadding(5, 5, 5, 5); // set the padding (if required)
    tr_head.addView(label_weight_kg); // add the column to the table row here
    tl.addView(tr_head, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    Integer count=0;
    while (rs.next()) {
   String date = (rs.getString(1));// get the first variable
   String weight_kg = (rs.getString(2));// get the second variable
   // Create the table row
   TableRow tr = new TableRow(this);
   if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
   tr.setId(100+count);
   tr.setLayoutParams(new LayoutParams(
   LayoutParams.FILL_PARENT,
   LayoutParams.WRAP_CONTENT));

   //Create two columns to add as table data
    // Create a TextView to add date
   TextView labelDATE = new TextView(this);
   labelDATE.setId(200+count); 
   labelDATE.setText(date);
   labelDATE.setPadding(2, 0, 5, 0);
   labelDATE.setTextColor(Color.WHITE);
   tr.addView(labelDATE);
   TextView labelWEIGHT = new TextView(this);
   labelWEIGHT.setId(200+count);
   labelWEIGHT.setText(weight_kg.toString());
   labelWEIGHT.setTextColor(Color.WHITE);
   tr.addView(labelWEIGHT);

   // finally add this to the table row
   tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
               count++;
            }   


    }catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
    }

}

Please let me know what goes wrong in my code?

LOG error

09-25 10:02:58.125: I/System.out(560): java.lang.NullPointerException 09-25
10:02:58.154: I/System.out(560):    at  
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 09-25  
10:02:58.154: I/System.out(560):    at
co.analuo.app.HorzScrollWithListMenu.display_table(HorzScrollWithListMenu.java:189) 
09-25 10:02:58.164: I/System.out(560):  at 
co.analuo.app.HorzScrollWithListMenu$1.onItemClick(HorzScrollWithListMenu.java:111)   
09-25 10:02:58.164: I/System.out(560):  at 
android.widget.AdapterView.performItemClick(AdapterView.java:284) 09-25 10:02:58.164:  
I/System.out(560):  at android.widget.ListView.performItemClick(ListView.java:3513) 
09-25 10:02:58.164: I/System.out(560):  at 
android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 09-25 10:02:58.175: 
I/System.out(560):  at android.os.Handler.handleCallback(Handler.java
:587)
09-25 10:02:58.185: I/System.out(560):  at   
android.os.Handler.dispatchMessage(Handler.java:92)
09-25 10:02:58.185: I/System.out(560):  at android.os.Looper.loop(Looper.java:123)
09-25 10:02:58.185: I/System.out(560):  at 
android.app.ActivityThread.main(ActivityThread.java:3683)
09-25 10:02:58.204: I/System.out(560):  at 
java.lang.reflect.Method.invokeNative(Native Method)
09-25 10:02:58.204: I/System.out(560):  at 
 java.lang.reflect.Method.invoke(Method.java:507)
 09-25 10:02:58.204: I/System.out(560):     at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-25 10:02:58.204: I/System.out(560):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-25 10:02:58.204: I/System.out(560):  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-11T21:35:06+00:00Added an answer on June 11, 2026 at 9:35 pm

    Finally i got the solution

    If findViewById() is returning a NPE, try some of these:

    • Clean the project via Project -> Clean… -> check your project -> OK
    • Ensure you have absolutely no spelling errors in the ID
    • Make sure the contentView is displaying the correct layout where the TableLayout exists
    • Make sure you setContentView before findViewById

    I’m assuming the 3rd option I’ve listed is most likely your problem.

    Re-cleaning the project may also help after each of these steps.

    Refered from https://stackoverflow.com/a/11671533/1602230

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the below lines in code unsigned char A = 0xB9; unsigned
What is a middle-level semantic concept? I came across it in a paper that
While trying to understand the Authorization concept in SAP, I came across the stage
I'm studying inheritance and polymorphism now and I've came across the concept that the
I just came across the concept of expression trees which I have heard multiple
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came across this Linq to Sql code in an application I am maintaining:
I came across a piece of code which looks like this: jQuery(function($) { $('#saySomething').click(function()
I came across this due to a bug in my code and I'm curious
I came across the following code : int i; for(; scanf(%s, &i);) printf(hello); As

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.