Original: Getweatherbycityname (city), the server was unable to process the request. ---> Object references are not set to an instance of the object.
Workaround: Do not use service reference directly, add as Web reference
recently used C # in WinForm to make a weather forecast applet, searched the Internet a lot of information, including ready-made programs, such as Http://blog.csdn.net /qishuangquan/article/details/6039287, but running to Getweatherbycityname (city) will make an error, prompting the server to fail to process the request. ---> Object references are not set to an instance of the object. Search the cause of the problem on the Internet, also did not find the answer, suspect the. NET Framework version of the problem, replaced 3.0, 3.5, 4.0 are the same, of course 2.0 is no service Reference, but there is a web Reference, in the Web Reference add webservice address, modifying weathertest. Weather. weatherwebservicesoapclient w = new Weathertest. Weather. weatherwebservicesoapclient (); for Weathertest. Weather.weatherwebservice WEBC = new weathertest. Weather.weatherwebservice (); Debugging incredibly successful, and then want to
⑴ on the item that needs to call WebService, click the right mouse button, select Add Service Reference, go to the ' Add Service Reference ' interface
⑵ Click ' Advanced ' button to enter ' Service Reference Settings ' interface
⑶ Click the ' Add Web Reference ' button to go to the ' Add Web Reference ' interface
① in the ' URL ' text box, fill in the address of the webservice you want to refer to (for example:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)
② Click on the Green Box button on the right, the program will automatically go to the given address to find the WebService service
③ in the ' Web Reference name ' text box, customize the name of this added webservice (for example: Weather)
④ Click the ' Add Reference ' button, the program automatically will find the webservice added in the current project, and automatically put in the Web References folder.
I wrote a program, test success, the main code is as follows:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.ServiceModel;
Using System.Runtime.InteropServices;
Namespace Weather2
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
Try
{
Weather2. Weather.weatherwebservice WEBC = new Weather2. Weather.weatherwebservice ();
String[] s = new string[23];//declares a string array to hold the result of its return
String city = This.textBox1.Text.Trim ();//Get the contents of text box 1
s = webc.getweatherbycityname (city);
MessageBox.Show ("Xi ' an meteorology:" + "\ r \ n" + "weather:" +s[6] + "\ r \ n" + "Temperature:" + s[5]);
}
catch (Exception E1)
{
MessageBox.Show (E1. ToString ());
}
}
}
}
See the test diagram below:
Back to the server could not process the request. ---> Do not set object references to an instance of an object "This problem, is estimated to be a vs version of the problem, because the Internet to see people using VS2008, but I use is VS2010, regardless of how, this question still gave an explanation, can ann according to the method above to visit the weather webservice.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Resolution: Getweatherbycityname (city), the server cannot process the request. ---> Object references are not set to an instance of the object.