Search code examples
androidarraylistandroid-popupwindow

SetText NullPointerException for ArrayList in PopupWindow. Android


I want to display an Array in a TextView for a Popup window. Only 1 item from the Array randomly. In this case I am using an ArrayList in my MainActivity and calling randomArray() to display. It does not work on the Screen_popup.xml. But it does work in the Main_activity.xml. I am using TextView textView1 for the main_activity & TextView txtViewArrayContent for the popup window. I think the TextView is initialized correctly but has something to do with setContentView? Any pointers would be great, thank you!

The error in log cat is:

07-17 09:25:34.655  25779-25779/com.example.testarray_02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testarray_02, PID: 25779
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testarray_02/com.example.testarray_02.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Main Activity Java:

public class MainActivity extends Activity {

Button btnClosePopup;
Button btnCreatePopup;
TextView newArray;
String item;

//***** Random Generator & ArrayList *****
final Random randomGenerator = new Random();
final ArrayList sample = new ArrayList() {{ add("Random Facts about Stuff"); add("Random Facts about Stuff 2"); add("Random Facts about Stuff 3"); add("Random Facts about Stuff 4");}};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Works - main activity layout
   // newArray = (TextView)findViewById(R.id.textView1);

    // Does NOT work. ... popup layout
    newArray = (TextView)findViewById(R.id.txtViewArrayContent);

    randomArray();
    btnCreatePopup = (Button) findViewById(R.id.button1);
    btnCreatePopup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            customPopupWindow(v);
        }
    });
}

private void randomArray() {
    //setContentView(R.layout.screen_popup);
    item = (String) sample.get(randomGenerator.nextInt(sample.size()));
    newArray.setText(item);  // Error logcat points to this line
                             // Null Exception Error
}
private PopupWindow popupWin;
private void customPopupWindow(View v){
    try {
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                (ViewGroup) findViewById(R.id.popup_element));
        popupWin = new PopupWindow(layout, 600, 600, true);
        popupWin.showAtLocation(layout, Gravity.CENTER, 0, 0);
        btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
        btnClosePopup.setOnClickListener(cancel_button_click_listener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
    public void onClick(View v) {
        popupWin.dismiss();
    }
};
}

Snippet of XML screen popupWindow

    <TextView
    android:id="@+id/txtViewArrayContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5sp"
    android:text="" />

Solution

  • You are trying to initialize newArray by trying to find it by id in activity_main.xml file, but it's not there, so you get NullPointerException. To fix your code, remove newArray initialization and call of randomArray() from onCreate() method and add it in customPopupWindwo(View v) as follows:

    private void customPopupWindow(View v) {
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                (ViewGroup) findViewById(R.id.popup_element));
        newArray = (TextView) layout.findViewById(R.id.txtViewArrayContent);
        //omitted rest for brevity
    }