user3701420 user3701420 - 19 days ago 12
Android Question

NullPointerException when trying to add a TableRow to a TableLayout programmatically

I'm trying to add a TableRow to a TableLayout programmatically, but it keeps giving me a NPE.

This is my onCreate method:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

try {
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
TableLayout table = (TableLayout) findViewById(R.id.table_servers);
String line = null;
while ((line = br.readLine()) != null){
String[] values = line.split(",");
MCServer server = new MCServer(values[0], values[1], values[2], values[3], Boolean.parseBoolean(values[4]));
servers.add(server);
TableRow row = new TableRow(this);
TextView name = new TextView(this);
TextView ip = new TextView(this);
TextView interval = new TextView(this);
TextView status = new TextView(this);
name.setText(values[0]);
ip.setText(values[1] + ":" + values[2]);
interval.setText(values[3]);
status.setText("----");
row.addView(name);
row.addView(ip);
row.addView(interval);
row.addView(status);
table.addView(row);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}


This my fragment_main.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_servers"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="23dp"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/servers_table_r1c1" />
<TextView
android:layout_width="0dp"
android:layout_height="23dp"
android:layout_weight="1"
android:padding="3dip"
android:text="@string/servers_table_r1c2" />

<TextView
android:layout_width="0dp"
android:layout_height="23dp"
android:layout_weight="0.6"
android:padding="3dip"
android:text="@string/servers_table_r1c3" />

<TextView
android:layout_width="0dp"
android:layout_height="23dp"
android:layout_weight="0.6"
android:padding="3dip"
android:text="@string/servers_table_r1c4" />
</TableRow>
<!-- This one is for testing purposes -->
<TableRow>
<TextView
android:text="@string/servers_table_r1c1"
android:padding="3dip" />
<TextView
android:text="@string/servers_table_r1c2"
android:padding="3dip" />
<TextView
android:text="@string/servers_table_r1c3"
android:padding="3dip" />
<TextView
android:text="@string/servers_table_r1c4"
android:padding="3dip" />
</TableRow>

</TableLayout>


It gives me a NPE on this line:

table.addView(row);


I don't know what the problem is, how can I fix this?

Answer

Since you placed those views in the Fragment's layout, they are not part of the activity until the fragment is in place.

In short: move the try... code into the Fragment's onCreateView(). And instead of findViewById, use rootView.findViewById(), where rootView is the view that is being returned by the method.

Comments