Hey guys im trying to make a dynamic scrollable list of selectiondata which a user can add or remove a spinner and editText when they need to. I have the Spinner and editText being added without an issue, but when i try to delete the one on the bottom of the list.. I get a null pointer error.
At first i thought it was because i was not referencing them properly (relevant editText and spinner data is held in a hashmap) however i have printed out the key names and they appear correct.
Here is the code..
Variables
int count = 0;
HashMap<String,Spinner> spinnerMap = new HashMap<String,Spinner>();
HashMap<String,EditText> editTextMap = new HashMap<String,EditText>();
LinearLayout horzLayout;
LinearLayout layout;
addRow method
public void addRow()
{
Toast.makeText(getApplicationContext(), "editText"+count, Toast.LENGTH_SHORT).show();
layout = (LinearLayout) findViewById(R.id.scrollerLayout);
LinearLayout horzLayout = new LinearLayout(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
editTextMap.put("editText"+count, new EditText(this));
editTextMap.get("editText"+count).setWidth(100);
editTextMap.get("editText"+count).setHeight(40);
spinnerMap.put("spinner"+count, new Spinner(this));
List<String> spinnerItems = new ArrayList<String>();
spinnerItems.add("Super Duper Bad Evil Weed");
spinnerItems.add("Super Duper Edible Weed");
spinnerItems.add("Funky Looking Weed");
spinnerItems.add("Special Weed");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerItems);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerMap.get("spinner"+count).setAdapter(dataAdapter);
horzLayout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(horzLayout, p);
horzLayout.addView(spinnerMap.get("spinner"+count));
horzLayout.addView(editTextMap.get("editText"+count));
count++;
}
RemoveRow Method
public void removeRow()
{
count--;
Toast.makeText(getApplicationContext(), "editText"+count, Toast.LENGTH_SHORT).show();
horzLayout.removeView(spinnerMap.get("spinner"+count));
spinnerMap.remove("spinner"+count);
horzLayout.removeView(editTextMap.get("editText"+count));
editTextMap.remove("editText"+count);
}
Exact Error Message..
> 06-09 23:48:33.369: E/AndroidRuntime(560): FATAL EXCEPTION: main
06-09 23:48:33.369: E/AndroidRuntime(560): java.lang.NullPointerException
06-09 23:48:33.369: E/AndroidRuntime(560): at org.unisa.paddockpad.ScreenTwo.removeRow(ScreenTwo.java:83)
06-09 23:48:33.369: E/AndroidRuntime(560): at org.unisa.paddockpad.ScreenTwo$2.onClick(ScreenTwo.java:52)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.view.View.performClick(View.java:3511)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.view.View$PerformClick.run(View.java:14105)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.os.Handler.handleCallback(Handler.java:605)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.os.Looper.loop(Looper.java:137)
06-09 23:48:33.369: E/AndroidRuntime(560): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-09 23:48:33.369: E/AndroidRuntime(560): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 23:48:33.369: E/AndroidRuntime(560): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 23:48:33.369: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-09 23:48:33.369: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-09 23:48:33.369: E/AndroidRuntime(560): at dalvik.system.NativeStart.main(Native Method)
Any help would be greatly appreciated!
update
I am able to delete the views right after they are created inside that method.
horzLayout initialized in addRow ( as local variable ) but not in remove row (where it global)…