locked
[Solved]System.NullReferenceException was unhandled??? RRS feed

  • Question

  •  

    Hi could any one tell me whats wrong with this code.

     

    WebReference.Item[] items = new WindowsApplication1.WebReference.Item[5];

    items[0].Description = "Desc";

    items[0].Discount = 0;

    items[0].Price = 12;

    items[0].Quantity = 6;

    items[0].Sku = "WEBSTAR001";

    items[0].TotalAmountIncludingVat = 0;

    items[0].VatAmount = 0;

     

     

    When I try to run the application I am getting this error

     

    System.NullReferenceException was unhandled 

     

    Plz help me whats wrong with it

    Thanks

     

    Thursday, October 4, 2007 10:45 AM

Answers

  • //You need to create the objects one by one.

    Item i1 = new Item();

    i1.Description = "Desc";

     

    //And then add them to the array

    items[0] = i1;

     

     

    or

    Code Block

    for (int i = 0; i < items.Length; i++ )

    {

    items[i] = new Item();

    }

     

     

     

    And then you can assign it.

    Thursday, October 4, 2007 11:00 AM
  •  

    Hi,

     

    I was sending null value for deliveryoption variables, because of this I was getting this error.

     

    Thanks

     

    Tuesday, October 9, 2007 8:06 AM

All replies

  • //You need to create the objects one by one.

    Item i1 = new Item();

    i1.Description = "Desc";

     

    //And then add them to the array

    items[0] = i1;

     

     

    or

    Code Block

    for (int i = 0; i < items.Length; i++ )

    {

    items[i] = new Item();

    }

     

     

     

    And then you can assign it.

    Thursday, October 4, 2007 11:00 AM
  • Thanks for your help. It is sorted but I am facing same  problem with this

     

    WebReference.Ordering obj = new WindowsApplication1.WebReference.Ordering();

     

    obj.PlaceOrder(ordFrm);

     

    Any idea.

     

    Thanks

     

    Thursday, October 4, 2007 11:12 AM
  • You can also instantiate all elements of an Array in one step:

    Array items = Array.CreateInstance(typeof(WebReference.Item), 5);

    The first parameter tells the Array object what type of elements it should hold, in this case WebReference.Item elements. The second parameter indicates the initial number of elements.

    Best regards,
    John Willemse
    Thursday, October 4, 2007 12:16 PM
  •  Ifahad wrote:

    Thanks for your help. It is sorted but I am facing same  problem with this

     

    WebReference.Ordering obj = new WindowsApplication1.WebReference.Ordering();

     

    obj.PlaceOrder(ordFrm);

     

    Any idea.

     

    Thanks

     



    In order to analyze this, you should supply us with the exception you get, on which line it is thrown, and some relevant code from the Ordering class (constructor and PlaceOrder method). Also, how did you declare ordFrm?

    Best regards,
    John Willemse
    Thursday, October 4, 2007 12:18 PM
  •  

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsApplication1

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    WebReference.Ordering obj = new WindowsApplication1.WebReference.Ordering();

    WebReference.Address cAdd = new WindowsApplication1.WebReference.Address();

    cAdd.AddressLine1 = "Test C#";

    cAdd.AddressLine2 = "Test C#2";

    cAdd.Town = "Manchester";

    cAdd.Postcode = "M50 2GF";

    cAdd.County = "Lancashire";

    cAdd.Country = "UK";

    WebReference.CreditCardDetails crd = new WindowsApplication1.WebReference.CreditCardDetails();

    crd.CreditCardType = WindowsApplication1.WebReference.CreditCardDetailsCreditCardType.AmericanExpress;

    crd.LastFourDigits = 1234;

    WebReference.Item[] items = new WindowsApplication1.WebReference.Item[1];

    items[0] = new WindowsApplication1.WebReference.Item();

    items[0].Description = "Desc";

    items[0].Discount = 0;

    items[0].Price = 12;

    items[0].Quantity = 6;

    items[0].Sku = "WEBSTAR001";

    items[0].TotalAmountIncludingVat = 0;

    items[0].VatAmount = 0;

     

    WebReference.OrderForm ord = new WindowsApplication1.WebReference.OrderForm();

    ord.AccountNo = "WEB002";

    ord.Address = cAdd;

    ord.BudgetHolder = "";

    ord.CreditCardDetails = crd;

    ord.DeliveryCharge = 2;

    ord.DeliveryName = "c#";

    ord.Item = items;

    ord.OrderLevelDiscount = 0;

    ord.OrderNumber = "CSHP001";

    ord.Store = WindowsApplication1.WebReference.OrderFormStore.Star;

    obj.PlaceOrder(ord);

     

     

    }

    }

    }

     

    I am recieving exception

     

     

    System.Web.Services.Protocols.SoapException was unhandled
      Message="Server was unable to process request. ---> Object reference not set to an instance of an object."
      Source="System.Web.Services"
      Actor=""
      Lang=""
      Node=""
      Role=""
      StackTrace:
           at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
           at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
           at WindowsApplication1.WebReference.Ordering.PlaceOrder(OrderForm OrderForm) in C:\Documents and Settings\FahadA\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Web References\WebReference\Reference.cs:line 83
           at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\FahadA\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs:line 55
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at WindowsApplication1.Program.Main() in C:\Documents and Settings\FahadA\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Program.cs:line 17
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

     

    I am getting this on line:

     

     

    obj.PlaceOrder(ord);

     

     

     

     

     

    Thursday, October 4, 2007 12:42 PM
  • Well, since none of the provided code can throw a System.Web.Services.Protocols.SoapException, the problem must lie inside the PlaceOrder(OrderForm ord) method. Can you provide the code for that method, or better yet, the class it resides in? Most likely, the response stream is not initialized.

    Best regards,
    John Willemse
    Thursday, October 4, 2007 12:51 PM
  •  

    HI,

    I dont have access to the actual soap class I can give you the url of the WSDL file .

     

    http://91.102.62.67:7080/starwslive/WMSOrderFulfilment.asmx?WSDL

     

    Thanks

     

    Thursday, October 4, 2007 1:33 PM
  • Hi Ifahad,

    If you've solved your problem, could you please share your experience with others so that the community will benifit from it.

    Thanks

    Monday, October 8, 2007 10:47 AM
  •  

    Hi,

     

    I was sending null value for deliveryoption variables, because of this I was getting this error.

     

    Thanks

     

    Tuesday, October 9, 2007 8:06 AM