0

I've created a double linked list of car imported from a text file.

I have to use the insert function of my DLL to insert the records from the text file into the list but I received the following error.

Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object at Untitled+Node..ctor () [0x00006] in <3d33487723864e3ba8eb4b8ade57b92b>:0 at Untitled+DoubleLinkedList.insert (Untitled+Dealer m) [0x0000b] in <3d33487723864e3ba8eb4b8ade57b92b>:0 at Untitled.Main (System.String[] args) [0x000af] in <3d33487723864e3ba8eb4b8ade57b92b>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Untitled+Node..ctor () [0x00006] in <3d33487723864e3ba8eb4b8ade57b92b>:0 at Untitled+DoubleLinkedList.insert (Untitled+Dealer m) [0x0000b] in <3d33487723864e3ba8eb4b8ade57b92b>:0 at Untitled.Main (System.String[] args) [0x000af] in <3d33487723864e3ba8eb4b8ade57b92b>:0

I know that it has something to do with something being null. Is the Dealer m constructor in the insert method not suppose to be there? Should I add the other remaining data variables?

Hopefully you guys could be of some great assistance.

using System;
using System.IO;
using System.Collections.Generic;

class Untitled 
{
    class Node
    {
        public Dealer data; 
        public Node prev;
        public Node next; 

        public Node()
        {
        data.make = "";
        data.model = "";
        data.year = 0;
        data.mileage = 0;
        data.price = 0; 
        prev = null;
        next = null; 
        } 
        public Node(Dealer m, Dealer md, Dealer y, Dealer mi, Dealer p, 
        Node forwards, Node backwards);
        {
            data = m;
            data = md; 
            data = y; 
            data = mi; 
            data = p; 
            prev = backwards;
            next = forwards;
        }
    }

    class DoubleLinkedList
    {
        public Node head;
        public Node tail; 
        public int size;

        public DoubleLinkedList()
        {
            head = null;
            tail = null;
            size = 0; 
        }
        //insert method
        public void insert(Dealer m)
        {
            if(head == null)
            {
                head = new Node();
                tail = head; 
            }
            else
            {
                tail.next = new Node();
                tail.next.prev = tail;
                tail = tail.next;
            }
        }
        //search method
    }

    public class Dealer
    {
        public string make; 
        public string model;
        public int year; 
        public double mileage; 
        public double price;
    }
}   
Souvik Ghosh
  • 3,824
  • 10
  • 45
  • 64
Cb173
  • 47
  • 1
  • 1
  • 6
  • Check your constructor ``Node(…)``: most parameters have the same type, and you overwrite the member ``data`` numerous times. – dumetrulo Sep 23 '17 at 07:42
  • You have to check your default constructot for `Node`. You want to assign `data.make` etc. but you never assign `data`. Add `data = new Dealer();` before assigning any fields. – Streamline Sep 23 '17 at 07:45
  • Understood now ! Thanks @Streamline. Now I received this error about Ienumerate Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type `Untitled.DoubleLinkedList' to `System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) – Cb173 Sep 23 '17 at 08:09
  • It sounds like you want to use your list in a `foreach` loop. For this to work your list has to implement [IEnumerable](https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx) or the none generic version (tough the generic version is preferred). But without context i can't know for sure what you are missing. – Streamline Sep 25 '17 at 07:55

0 Answers0