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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:10:06+00:00 2026-05-22T17:10:06+00:00

Help! I’ve really come to appreciate Stack Overflow and it’s contributors over the last

  • 0

Help! I’ve really come to appreciate Stack Overflow and it’s contributors over the last few months. Many questions I’ve had, I’ve found the answers here… but this one I can’t seem to find anywhere… I’m a noob to Java and Android, and I’ve been trying to figure this out for days. For some reason I have a ListView object called fileList, and it is returning null… Everything compiles fine, but I get a NullPointerException the moment I try to use fileList… I’ve been able to isolate it to it’s declaration:

ListView fileList = (ListView)findViewById(R.id.open_ListView);

But I can’t for the life of me understand what is wrong with this line! I’ve included lots of code below, theoretically it should have any and all code that could be related to this error in any way.

Please, any help on this would be really appreciated! Thanks!


This is the offending section of code. It’s just the OPEN_DIALOG part of the switch block, and all the other switches work perfectly fine to display their newDialog’s. I’ve marked the offending line with stars…

@Override
protected Dialog onCreateDialog(int id) 
{
    Dialog newDialog = new Dialog(Minervalia.this);
    switch(id)
    {
    case DIALOG_FILE_NEW:
        newDialog.setContentView(R.layout.new_file);
        newDialog.setTitle("New File");
        newDialog.setCancelable(true);

        Button okBtn = (Button) newDialog.findViewById(R.id.ok_btn);
        Button cancelBtn = (Button) newDialog.findViewById(R.id.cancel_btn);
        final EditText widthEt = (EditText) newDialog.findViewById(R.id.width_edit);
        final EditText heightEt = (EditText) newDialog.findViewById(R.id.height_edit);

        okBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                file_width = Integer.parseInt(widthEt.getText().toString());
                file_height = Integer.parseInt(heightEt.getText().toString());
                onCreate(null);
                dismissDialog(DIALOG_FILE_NEW);
            }
        });

        cancelBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dismissDialog(DIALOG_FILE_NEW);
            }
        });
        return newDialog;
    case DIALOG_OPEN:
        newDialog.setContentView(R.layout.open_file);
        newDialog.setTitle("Open File");
        newDialog.setCancelable(true);

// ********** This next line returns null! Why?
        ListView fileList = (ListView)findViewById(R.id.open_ListView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, loadFileList());
        fileList.setAdapter(adapter);
        fileList.setTextFilterEnabled(true);

        fileList.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
                //  When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });

        return newDialog;
    case DIALOG_SAVE:
        newDialog.setContentView(R.layout.save_file);
        newDialog.setTitle("Save File");
        newDialog.setCancelable(true);

//--==[[ Define the important TextViews for our Save Dialog ]]==--\\            
        TextView pathTxt = (TextView) newDialog.findViewById(R.id.save_path_info);
        EditText fnTxt = (EditText) newDialog.findViewById(R.id.save_filename_edit);

//--==[[ Define the Radio Buttons for our Save Dialog ]]==--\\          
        RadioButton JPEGoption = (RadioButton) newDialog.findViewById(R.id.save_JPEGoption);
        RadioButton PNGoption = (RadioButton) newDialog.findViewById(R.id.save_PNGoption);

        file_type = TYPE_JPEG; // Set this as the default option

        JPEGoption.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                file_type = TYPE_JPEG;
            }
        });

        PNGoption.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                file_type = TYPE_PNG;
            }
        });

        Button save_okBtn = (Button) newDialog.findViewById(R.id.save_ok_btn);
        Button save_cancelBtn = (Button) newDialog.findViewById(R.id.save_cancel_btn);

        path = pathTxt.getText().toString();

        fnTxt.addTextChangedListener(new TextWatcher() 
        { 
            public void afterTextChanged(Editable s) 
            { 
            } 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) 
            { 
            } 
            public void onTextChanged(CharSequence s, int start, int before, int count) 
            { 
                filename = s.toString(); 
            } 
        }); 
        Toast.makeText(this, path, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, filename, Toast.LENGTH_SHORT).show();
        save_okBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                try 
                {
                    String fullName = path + filename;
                    Bitmap.CompressFormat compForm = Bitmap.CompressFormat.JPEG; // make sure our format is initialized
                    if(file_type == TYPE_JPEG) 
                    {
                        fullName = fullName + ".jpg";
                        compForm = Bitmap.CompressFormat.JPEG;
                    }
                    if(file_type == TYPE_PNG)
                    {
                        fullName = fullName + ".png";
                        compForm = Bitmap.CompressFormat.PNG;
                    }
                    File thisImage = new File(fullName);
                    FileOutputStream out = new FileOutputStream(thisImage);
                    mBitmap.compress(compForm, 90, out);

                    new SingleMediaScanner(mContext, thisImage);
                    out.flush();
                    out.close();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }

                dismissDialog(DIALOG_SAVE);
            }
        });

        save_cancelBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dismissDialog(DIALOG_SAVE);
            }
        });
        return newDialog;
    }
    return null;
}

private String[] loadFileList()
{
    String[] mFileList = new String[0]; // generate empty array to avoid NullPointerException
    try
    {
        filePath.canWrite();
    }
    catch(SecurityException e)
    {
// Why isn't TAG recognized?...
//           Log.e(TAG, "unable to write on the sd card " + e.toString());
    }
    if(filePath.exists())
    {
        FilenameFilter filter = new FilenameFilter()
        {
            public boolean accept(File dir, String filename)
            {
                File sel = new File(dir, filename);
                return filename.contains(".jpg") || filename.contains(".png") || sel.isDirectory();
            }
        };
        mFileList = filePath.list(filter);
    }
    else
    {
        mFileList = new String[0];
    }
    return mFileList;
}

This is my open_file.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView 
        android:id="@+id/open_ListView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ListView>
    <LinearLayout
        android:id="@+id/open_ButtonLinearLayout"
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button 
            android:id="@+id/open_ok_btn"
            android:text="Open" 
            android:layout_width="150dp" 
            android:layout_height="wrap_content">
        </Button>       
        <Button 
            android:id="@+id/open_cancel_btn" 
            android:text="Cancel"
            android:layout_width="150dp" 
            android:layout_height="wrap_content">
        </Button>       
    </LinearLayout>
</LinearLayout>

And this is my list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>
  • 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-05-22T17:10:07+00:00Added an answer on May 22, 2026 at 5:10 pm

    Try this instead, since it’s inside the dialog’s layout, not the activity’s:

    ListView fileList = (ListView)newDialog.findViewById(R.id.open_ListView);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HELP......Just moved to C# from vb and Im really lost with this. var ldapmembershipUser
Help. I recently purchased a new PC. I had my eclipse set up and
Help iv'e seen other questions but I'm a beginner with c# Code: namespace Input_Program
Help! I have an Axis web service that is being consumed by a C#
Help me ..my page index is not working in visual studio. my page load
Help! I am using jQuery to make an AJAX call to fill in a
Help! I have a PHP (PHP 5.2.5) script on HOST1 trying to connect to
help me to manage IIS (what i am trying to do is create virtual
Help I am busy making changes to a type library in a Datasnap project.
HELP! I just setup a virtual host for two sites that have a lot

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.