Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
  1  using Microsoft.VisualBasic;
  2  using Microsoft.VisualBasic.CompilerServices;
  3  using System;
  4  using System.Collections;
  5  using System.Collections.Generic;
  6  using System.ComponentModel;
  7  using System.Data;
  8  using System.Drawing;
  9  using System.IO;
 10  using System.IO.Compression;
 11  using System.Linq;
 12  using System.Runtime.CompilerServices;
 13  using System.Text;
 14  using System.Threading.Tasks;
 15  using System.Windows.Forms;
 16  using System.Xml;
 17  
 18  namespace SD
 19  {
 20      public partial class Form1 : Form
 21      {
 22          public Form1()
 23          {
 24              InitializeComponent();
 25          }
 26          private string fileName;
 27  
 28          private void Form1_Load(object sender, EventArgs e)
 29          {
 30          }
 31          private void button1_Click(object sender, EventArgs e)
 32          {
 33              List<string> filenames = FindFiles(path.Text, exten.Text,check.Checked);
 34              lstFiles.Items.Clear();
 35              foreach (string filename in filenames)
 36              {
 37                  lstFiles.Items.Add(filename);
 38              }
 39          }
 40          private List<string> FindFiles(string dir_name, string patterns, bool search_subdirectories)
 41          {
 42              List<string> files = new List<string>();
 43              string[] pattern_array = patterns.Split(';');
 44              SearchOption search_option = SearchOption.AllDirectories;
 45              foreach (string pattern in pattern_array)
 46              {
 47                  foreach (string filename in Directory.GetFiles(dir_name, pattern, search_option))
 48                  {
 49                      if (!files.Contains(filename)) files.Add(filename);
 50                  }
 51              }
 52              label1.Text = files.Count.ToString();
 53              files.Sort();
 54              return files;
 55          }
 56          private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
 57          {
 58          }
 59          private byte[] DecompressArray(byte[] content)
 60          {
 61              byte[] array;
 62              int num;
 63              using (MemoryStream memoryStream = new MemoryStream())
 64              {
 65                  using (MemoryStream memoryStream1 = new MemoryStream(content))
 66                  {
 67                      using (GZipStream gZipStream = new GZipStream(memoryStream1, CompressionMode.Decompress))
 68                      {
 69                          byte[] numArray = new byte[1024];
 70                          do
 71                          {
 72                              num = gZipStream.Read(numArray, 0, (int)numArray.Length);
 73                              memoryStream.Write(numArray, 0, num);
 74                          }
 75                          while (num > 0);
 76                      }
 77                  }
 78                  array = memoryStream.ToArray();
 79              }
 80              return array;
 81          }
 82          private void UnzipDirectory(byte[] compressed, string outputPath)
 83          {
 84              IEnumerator enumerator = null;
 85              IEnumerator enumerator1 = null;
 86              if (!Directory.Exists(outputPath))
 87              {
 88                  Directory.CreateDirectory(outputPath);
 89              }
 90              byte[] numArray = this.DecompressArray(compressed);
 91              XmlDocument xmlDocument = new XmlDocument();
 92              using (MemoryStream memoryStream = new MemoryStream(numArray))
 93              {
 94                  xmlDocument.Load(memoryStream);
 95                  XmlNode firstChild = xmlDocument.FirstChild;
 96                  XmlNode xmlNodes = firstChild.FirstChild;
 97                  XmlNode nextSibling = firstChild.FirstChild.NextSibling;
 98                  try
 99                  {
100                      enumerator = xmlNodes.ChildNodes.GetEnumerator();
101                      while (enumerator.MoveNext())
102                      {
103                          object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
104                          Directory.CreateDirectory(Path.Combine(outputPath, ((XmlNode)objectValue).Attributes.Item(0).Value));
105                      }
106                  }
107                  finally
108                  {
109                      if (enumerator is IDisposable)
110                      {
111                          (enumerator as IDisposable).Dispose();
112                      }
113                  }
114                  try
115                  {
116                      enumerator1 = nextSibling.ChildNodes.GetEnumerator();
117                      while (enumerator1.MoveNext())
118                      {
119                          object obj = RuntimeHelpers.GetObjectValue(enumerator1.Current);
120                          string str = Path.Combine(outputPath, ((XmlNode)obj).Attributes.Item(0).Value);
121                          byte[] numArray1 = Convert.FromBase64String(((XmlNode)obj).InnerText);
122                          File.WriteAllBytes(str, numArray1);
123                      }
124                  }
125                  finally
126                  {
127                      if (enumerator1 is IDisposable)
128                      {
129                          (enumerator1 as IDisposable).Dispose();
130                      }
131                  }
132              }
133          }
134          private void lstFiles_DoubleClick(object sender, EventArgs e)
135          {
136              if (lstFiles.SelectedItem != null)
137              {
138  
139                  lstFiles.SelectedItem.ToString();
140                  FileInfo fileInfo = new FileInfo(fileName);
141                  try
142                  {
143                      if (File.Exists(fileName))
144                      {
145                          UnzipDirectory(File.ReadAllBytes(fileName), fileInfo.DirectoryName);
146                      }
147                  }
148                  catch (Exception exception)
149                  {
150                      ProjectData.SetProjectError(exception);
151                      Interaction.MsgBox("error", MsgBoxStyle.OkOnly, null);
152                      ProjectData.ClearProjectError();
153                      return;
154                  }
155                  Interaction.MsgBox("Complete", MsgBoxStyle.OkOnly, null);
156              }
157          }
158      }
159  }


What I have tried:

My problem is when i select the file from listbox with double click the code return with error in line 140 Value cannot be null...
Posted
Updated 14-Apr-20 2:58am
v3

Seems, you forgot to initiate your variable...
C#
private void lstFiles_DoubleClick(object sender, EventArgs e)
{
    if (lstFiles.SelectedItem != null)
    {
        fileName = lstFiles.SelectedItem.ToString(); //missing part!
        FileInfo fileInfo = new FileInfo(fileName);
        //skipped lines...


Extra notes:
1)
One of the most important skill of developer is to use debugger.
First look at the debugger - Visual Studio | Microsoft Docs[^]
Debugging in Visual Studio - Visual Studio | Microsoft Docs[^]
This may help you to resolve most of issues.

2)
What's for are thsese lines?
C#
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;

I'd suggest to remove unused namespaces in using section.
First method: Organise Usings > Remove Unused Usings
Second method: [Ctrl] + [R] + [G]
 
Share this answer
 
v2
Comments
Stm21 14-Apr-20 6:32am    
Thank you so much for your answer!!!!!!!!!!!!
Maciej Los 14-Apr-20 6:56am    
You're very welcome.
If this resolves your issue, please, mark my solution as an answer (use green button).
TheRealSteveJudge 14-Apr-20 9:22am    
5*
Maciej Los 14-Apr-20 9:34am    
Thank you.
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!
 
Share this answer
 
Comments
Stm21 14-Apr-20 6:14am    
Thank you for the quick response
I am not a programmer but i try my very best with many google search but many times i can't find solution like this time.
OriginalGriff 14-Apr-20 6:24am    
You don't solve null reference problems with a google search - you solve them by debugging your code to find out what is null, and then look back through your code to find out why.

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