Basically I have a TableLayout with several TableRows, each containg 5 cells (Name, Price, Count, IncreaseButton, DecreaseButton), and then I have written a function to return the price of a certain row, but I keep getting an exception.
This is the code:
private int getRowPrice(View target) //target is a Button
{
TableRow row = (TableRow) target.getParent();
TextView priceCell = (TextView) row.getChildAt(1);
String price = priceCell.getText().toString();
//The cell text may start with $ or another currency
if (price.startsWith(getString(R.string.currency)))
{
price = price.substring(getString(R.string.currency).length()).toString();
}
return Integer.getInteger(price); //Here I get a "java.lang.NullPointerException"
}
If I Log.d() the price it outputs the correct value, and if I try to return getInteger(“10”) that works fine, also the debugger classifies ‘price’ as a string. So I must admit I have no idea why this error keeps occuring.
And please let me know if there is a better, less-error-prone way to get a cell of a certain row)
You need to use Integer.valueOf() instead of Integer.getInteger(). If you look at the API docs for Integer.getInteger() you’ll see that it “Returns the Integer value of the system property identified by the given string”. The method name is a bit misleading.