0

I get this weird NullReferenceExcpetion, in a WPF application. I don't get this every time, even if I do same operation. Can anyone please explain the reason for this,

enter image description here

 public class AmazonUrl
 {
        public string Url { get; set; }
 }

 public partial class MainWindow : Window
 {

    public ObservableCollection<AmazonUrl> AmazonUrlList { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;
        AmazonUrlList = new ObservableCollection<AmazonUrl>();
    }

    public List<string> getURLList()
    {
        List<string> urlList = new List<string>();

        for(int i = 0; i < AmazonUrlList.Count; i++)
        {
            AmazonUrl url = AmazonUrlList[i];

            if (url == null)
                continue;

            String str = url.Url.ToString().Trim();

            if (str.Length > 0)
               urlList.Add(str);
       }

       return urlList;
   }

    private void openMenuItem_Click(object sender, RoutedEventArgs e)
    {
        List<string> urlList = getURLList();//This is where exception occur

        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "SCR File (.scr)|*.scr|All Files|*.*";

        if (openFileDialog.ShowDialog() != true){ ... }
    }

Note: After adding these lines I made lots of changes to the application, and recompiled several times. So this is not a problem with Build or Compiling

Edit: My stack trace can find from here, https://pastebin.com/2vyH1qah

17
  • 3
    What's the stack trace? Commented Oct 3, 2017 at 17:36
  • 5
    Stack trace would be useful - my money is on an overridden operator == causing the issue. Commented Oct 3, 2017 at 17:38
  • 3
    Is that definitely all that's in the AmazonUrl class? Are you able to reproduce this with a minimal reproducible example? Commented Oct 3, 2017 at 17:39
  • 3
    @maamaa we need the exception stack trace, not the call stack. It should be printed to the Output window. Commented Oct 3, 2017 at 17:40
  • 2
    @maamaa you can use a collection of strings as a source too, you just need to define the column header yourself in xaml Commented Oct 3, 2017 at 18:09

1 Answer 1

1

I can't reproduce the error, it must be something with your collection. What does your collection look like? This should fix the problem either way--

public partial class MainWindow : Window
{
    private static object lockObj = new object(); //Add

...

public List<string> getURLList()
  {
     List<string> urlList = new List<string>();

     for (int i = 0; i < AmazonUrlList.Count; i++)
     {
        Amazon url = new AmazonUrl();  // Add
        url = AmazonUrlList[i];        // Update

...

private void openMenuItem_Click(object sender, RoutedEventArgs e)
  {
     lock (lockObj) // Add
     {
        List<string> urlList = getURLList(); //This is where exception occur
     }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.