Click here to Skip to main content
14,699,840 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

using WMPLib;

using System.Media;

namespace folderBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        internal class ListItem
        {
            string path;
            private string v;

            public ListItem(string v)
            {
                this.v = v;
            }
        }
        private string v;
        public ListBox.SelectedIndexCollection SelectedIndices { get; }
        SoundPlayer player = new SoundPlayer();
        WindowsMediaPlayer myplayer = new WindowsMediaPlayer();

        private void button1_Click(object sender, EventArgs e)
        {
            string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };

            var directory = new DirectoryInfo(@"D:\Dogukan\Ingcart");

            var files = new List<fileinfo>();

            foreach (var filter in filters)
            {
                var results = directory.GetFiles(filter, SearchOption.AllDirectories);
                files.AddRange(results);
            }

            foreach (FileInfo file in files)
            {
                //ListItem a = new ListItem(Path.GetFileNameWithoutExtension(file.FullName));
                //listBox1.Items.Add(a);
                listBox1.Items.Add(Path.GetFileNameWithoutExtension(file.FullName));
            }
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {

                 pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedValue).FullName);
                // pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).ToString());
                textBox1.Text = listBox1.SelectedItem.ToString();


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName);
            pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedValue).FullName);
            textBox1.Text = listBox1.SelectedItem.ToString();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            myplayer.URL = @"D:\Dogukan\" + "//" + listBox1.SelectedItem.ToString() + ".mp3";

            myplayer.controls.play();
        }
        private void FindMyString(string searchString)
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (listBox1.Items[i].ToString().IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    listBox1.SetSelected(i, true);
                }
                else {
                    // Do this if you want to select in the ListBox only the results of the latest search.
                    listBox1.SetSelected(i, false);
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //int index = listBox1.FindString(this.textBox2.Text);
            //if (0 <= index)
            //{
            //    listBox1.SelectedIndex = index;
            //}

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
 }


What I have tried:

private void listBox1_MouseClick(object sender, MouseEventArgs e)
I write pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName); code, when i started program and load picture, view files on the listBox1 than i clicked mouse on file error System.NullReferenceException' occurred in folderBrowser.exe. What must i do ?
Posted
Updated 29-Mar-17 21:54pm
v2
Comments
[no name] 29-Mar-17 9:02am
   
"What must i do ?", debug your code and fix your null reference.
Member 13066145 29-Mar-17 9:13am
   
Sorry, i don't know very well c#. where will i fix my null reference?

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
   
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   
v2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

using WMPLib;

using System.Media;

namespace folderBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

class fileItem
{
public string FileName { get; set; }
public string fileText { get; set; }
public string fullName { get; set; }
}

public ListBox.SelectedIndexCollection SelectedIndices { get; }
SoundPlayer player = new SoundPlayer();
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();

private void button1_Click(object sender, EventArgs e)
{
string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };

var directory = new DirectoryInfo(@"D:\Dogukan\Ingcart");

var files = new List<fileinfo>();

foreach (var filter in filters)
{
var results = directory.GetFiles(filter, SearchOption.AllDirectories);
files.AddRange(results);
}

foreach (FileInfo file in files)
{
listBox1.Items.Add(new fileItem { FileName = file.Name, fileText = Path.GetFileNameWithoutExtension(file.Name), fullName = file.FullName });
}
}

private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
pictureBox1.Image = Image.FromFile(((fileItem)listBox1.SelectedItem).fullName);
textBox1.Text = ((fileItem)listBox1.SelectedItem).fileText;
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((fileItem)listBox1.SelectedItem).fullName);
textBox1.Text = ((fileItem)listBox1.SelectedItem).fileText;

}

private void pictureBox1_Click(object sender, EventArgs e)
{
myplayer.URL = @"D:\Dogukan\" + "//" + ((fileItem)listBox1.SelectedItem).fileText +".mp3";

myplayer.controls.play();
}
private void FindMyString(string searchString)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (listBox1.Items[i].ToString().IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0)
{
listBox1.SetSelected(i, true);
}
else {
// Do this if you want to select in the ListBox only the results of the latest search.
listBox1.SetSelected(i, false);
}
}
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox2.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}

}

private void Form1_Load(object sender, EventArgs e)
{
listBox1.DisplayMember = "fileText";
listBox1.ValueMember = "FileName";

}
}
}
   
v2
Comments
Richard Deeming 30-Mar-17 12:32pm
   
If you want to update your question, click the green "Improve question" link at the bottom of the question.

DO NOT post your update as a "solution".
Patrice T 30-Mar-17 18:38pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
and delete this solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900