C# Object reference not set to an instance - reference

what does it mean Object reference not set to an instance of an object in the line below?
case "S Connected":
List dd6 = new List(textBox1.Text);
**dd6.textBox2.Text = id.ToString();**
dd6.ShowDialog();

Thanks, guys. The first case works perfectly, the W connected and the S connected retrieve the error:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
var id = dataGridView1.CurrentRow.Cells["Id"].Value;
var BBB = dataGridView1.CurrentRow.Cells["Connected"].Value;
string idd = id.ToString();
string BBB2 = BBB.ToString();
textBox1.Text = id.ToString();
switch (BBB2)
{
case "Standalone":
ListStandalone dd = new ListStandalone(textBox1.Text);
dd.textBox2.Text = id.ToString();
dd.ShowDialog();
break;
case "W Connected":
ListW da2 = new ListW(textBox1.Text);
da2.textBox2.Text = id.ToString();
da2.ShowDialog();
break;
case "S Connected":
ListS dd6 = new ListS(textBox1.Text);
dd6.textBox2.Text = id.ToString();
dd6.ShowDialog();
break;
case "Retail":
PrintRetail dd4 = new PrintRetail(textBox1.Text);
dd4.Show();
break;
}

Related

Creating Customer Location

My requirement is to create a customer location part of downloading order from 3rd party shopping cart.
I have tried this below code and It is not saving any location and also not raising any error.
private static void CreateCustomerLocation(Customer cust, string locationcode, OrderDTO ord, OrderDownloadActivityEntry grp)
{
try
{
LocationMaint graph = CustomerLocationMaint.CreateInstance<CustomerLocationMaint>();
SelectedLocation loc = new SelectedLocation();
loc.BAccountID = cust.BAccountID;
loc.LocationCD = locationcode;
loc.Descr = ord.CustomerLocationName;
loc.IsContactSameAsMain = false;
loc.IsAddressSameAsMain = false;
graph.Location.Insert(loc);
Contact contact = new Contact();
contact.Attention = ord.OrderCustomerContactName;
contact.Phone1 = ord.OrderCustomerContactPhone;
contact.DisplayName = ord.CustomerLocationName;
contact.LastName = ord.OrderCustomerContactName;
contact = graph.Contact.Update(contact);
Address address = new Address();
address.AddressLine1 = ord.OrderShippingLocationAddress1;
address.AddressLine2 = ord.OrderShippingLocationAddress2;
address.City = ord.OrderShippingLocationCity;
address.State = ord.OrderShippingLocationState;
address.PostalCode = ord.OrderShippingLocationZip;
address.CountryID = "US";
contact = graph.Contact.Update(contact);
address = graph.Address.Update(address);
loc.DefAddressID = address.AddressID;
loc.DefContactID = contact.ContactID;
graph.Location.Update(loc);
graph.Save.Press();
}
catch(Exception e)
{
grp.AddLogData(SessionID, "Create Location", "Create Location falied", null, null, e.StackTrace);
}
}
I am not able to figure out where i am making mistake. any suggestion for this issue?
Update
I have tried the following code and I am getting the following error
CARAccountLocationID' cannot be empty.
private static void CreateCustomerLocation(Customer cust, string locationcode, OrderDTO ord, OrderDownloadActivityEntry grp)
{
try
{
LocationMaint graph = PXGraph.CreateInstance<CustomerLocationMaint>();
graph.BusinessAccount.Current = PXSelect<BAccount, Where<BAccount.bAccountID, Equal<Required<BAccount.bAccountID>>>>.Select(graph, cust.BAccountID);
var newLocation = (Location)graph.Location.Cache.CreateInstance();
var locType = LocTypeList.CustomerLoc;
newLocation.LocType = locType;
graph.Location.Insert(newLocation);
var loc = (Location)graph.Location.Cache.CreateCopy(graph.Location.Current);
Contact contact = graph.Contact.Cache.CreateCopy(graph.Contact.Current) as Contact;
contact.Attention = ord.OrderCustomerContactName;
contact.Phone1 = ord.OrderCustomerContactPhone;
contact.DisplayName = ord.CustomerLocationName;
contact.LastName = ord.OrderCustomerContactName;
contact = graph.Contact.Update(contact);
Address address = graph.Address.Cache.CreateCopy(graph.Address.Current) as Address;
address.AddressLine1 = ord.OrderShippingLocationAddress1;
address.AddressLine2 = ord.OrderShippingLocationAddress2;
address.City = ord.OrderShippingLocationCity;
address.State = ord.OrderShippingLocationState;
address.PostalCode = ord.OrderShippingLocationZip;
address.CountryID = "US";
contact = graph.Contact.Update(contact);
address = graph.Address.Update(address);
contact.DefAddressID = address.AddressID;
loc.IsAddressSameAsMain = false;
loc.IsContactSameAsMain = false;
loc.IsAPAccountSameAsMain = true;
loc.IsAPPaymentInfoSameAsMain = true;
loc.IsARAccountSameAsMain = true;
loc.LocationCD = locationcode;
loc.Descr = ord.CustomerLocationName;
loc = graph.Location.Update(loc);
loc.BAccountID = cust.BAccountID;
graph.Location.Cache.RaiseFieldUpdated<Location.isARAccountSameAsMain>(loc, null);
if (loc.CARAccountLocationID == null)
loc.CARAccountLocationID = cust.DefLocationID;
graph.Location.Update(loc);
graph.Save.Press();
}
catch(Exception e)
{
grp.AddLogData(SessionID, "Create Location", "Create Location falied", null, null, e.StackTrace);
}
}
CARAccountLocationID is the LocationID of the MAIN location for a given BAccount/Customer. It is used by the business logic when setting GLAccounts.SameAsDefaultLocationS on screen AR303020.
I've seen the "'CARAccountLocationID' cannot be empty." error when creating a location without first setting the Customer.
The resolution was to first set the customer, then set SameAsDefaultLocationS, then set the rest of the fields.
In the screen API order of operations matters.
In your case you might need to directly set loc.CARAccountLocationID to the LocationID of the customer's MAIN location.

Inserting new Activity for Case Screen by code

I am trying to insert a new Activity for Case. The code which I have written executes without any error but it even does not inserts any Activity. Let me know if I am missing anything.
Here is the code
CRActivity actiCloud9 = new CRActivity();
actiCloud9.Type = "N"; // Note
actiCloud9.Subject = current.Summary;
actiCloud9.Body = current.Description;
actiCloud9.UIStatus = current.Status;
actiCloud9.StartDate = current.StartDate;
actiCloud9.IsPrivate = true; // current.IsInternal;
actiCloud9.RefNoteID = Base.CaseCurrent.Current.NoteID;
Base.Activities.Cache.Insert(actiCloud9);
Try this.
[PXOverride]
public void Persist(Action del)
{
if ((Base.Case.Cache.GetStatus(Base.Case.Current) == PXEntryStatus.Inserted || Base.Case.Cache.GetStatus(Base.Case.Current) == PXEntryStatus.Updated))
{
CRActivityMaint objGraph = PXGraph.CreateInstance<CRActivityMaint>();
CRActivity actiCloud9 = new CRActivity();
actiCloud9.Type = "N"; // Note
actiCloud9.Subject = "Subject";
actiCloud9.Body = "Body";
actiCloud9.RefNoteID = Base.CaseCurrent.Current.NoteID;
objGraph.Activities.Cache.Insert(actiCloud9);
objGraph.Actions.PressSave();
}
del();
}

Returning list of objects from Database

I am trying to return an object array from my database but can't get the call correct or the return object[s].
In the example below I just return an empty String trying to get this working. The listings[cnt] are being filled properly but I'm not sure how to make the call and return the Objects?
I am making 2 different calls to new Houses in getData() and that doesn't seem right either.
Houses[] listings = new Houses[cnt]; // Seems like I need this to initialize the array to a certain size.
listings[cnt] = new Houses(z,a,b,c,d,e,f,g,h); // Then I fill the Object with variables.
then return listings somehow.
// ??? How do I set it up so that I get an array of objects back?
// I am making the call from MainActivity here
myHelper = new GrumpyDB(this);
String h = new String();
h = myHelper.getData();
public String getData() {
Log.d(TAG, "Starting getData");
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {myHelper.UID, myHelper.ADDRESS, myHelper.STREET, myHelper.CITY, myHelper.STATE, myHelper.ZIP, myHelper.ENTIRE_ADDRESS,myHelper.UPLOADS,myHelper.DOWNLOADS};
Cursor cursor = db.query(myHelper.TABLE_NAME_LOCATION, columns, null, null, null, null, null);
int cnt = cursor.getCount();
Houses[] listings = new Houses[cnt];
cnt = 0;
while (cursor.moveToNext()) {
int index0 = cursor.getColumnIndex(myHelper.UID);
int index1 = cursor.getColumnIndex(myHelper.ADDRESS);
int index2 = cursor.getColumnIndex(myHelper.STREET);
int index3 = cursor.getColumnIndex(myHelper.CITY);
int index4 = cursor.getColumnIndex(myHelper.STATE);
int index5 = cursor.getColumnIndex(myHelper.ZIP);
int index6 = cursor.getColumnIndex(myHelper.ENTIRE_ADDRESS);
int index7 = cursor.getColumnIndex(myHelper.UPLOADS);
int index8 = cursor.getColumnIndex(myHelper.DOWNLOADS);
String z = cursor.getString(index0);
String a = cursor.getString(index1);
String b = cursor.getString(index2);
String c = cursor.getString(index3);
String d = cursor.getString(index4);
String e = cursor.getString(index5);
String f = cursor.getString(index6);
String g = cursor.getString(index7);
String h = cursor.getString(index8);
listings[cnt] = new Houses(z,a,b,c,d,e,f,g,h);
Log.d(TAG, listings[cnt].UID + listings[cnt].address + listings[cnt].street + listings[cnt].city + listings[cnt].state + listings[cnt].zip + listings[cnt].upload + listings[cnt].download);
//buffer.append(f + "\n");
cnt++;
}
//Log.d(TAG, "Returning From getData");
//return listings;
cursor.close();
db.close();
return "";
//return listings;
}
I got it ..... this works.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GrumpyDB myHelper;
myHelper = new GrumpyDB(this);
//String h = new String();
//h = myHelper.getData();
//Houses House;
Houses myList[] = myHelper.getData();
for(int i =0; i < myList.length; i++) {
Log.d(TAG,myList[i].street);
}
Button myBtn1 = (Button)findViewById(R.id.addHouseButton);
myBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), AddHouse.class);
intent.putExtra("func", "update"); // Use to pass items to next page
startActivity(intent); // Start Activity
}
});
}
public Houses[] getData() {
Log.d(TAG, "Starting getData");
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {myHelper.UID, myHelper.ADDRESS, myHelper.STREET, myHelper.CITY, myHelper.STATE, myHelper.ZIP, myHelper.ENTIRE_ADDRESS, myHelper.UPLOADS, myHelper.DOWNLOADS};
Cursor cursor = db.query(myHelper.TABLE_NAME_LOCATION, columns, null, null, null, null, null);
int cnt = cursor.getCount();
Houses[] listings;
listings = new Houses[cnt];
cnt = 0;
while (cursor.moveToNext()) {
int index0 = cursor.getColumnIndex(myHelper.UID);
int index1 = cursor.getColumnIndex(myHelper.ADDRESS);
int index2 = cursor.getColumnIndex(myHelper.STREET);
int index3 = cursor.getColumnIndex(myHelper.CITY);
int index4 = cursor.getColumnIndex(myHelper.STATE);
int index5 = cursor.getColumnIndex(myHelper.ZIP);
int index6 = cursor.getColumnIndex(myHelper.ENTIRE_ADDRESS);
int index7 = cursor.getColumnIndex(myHelper.UPLOADS);
int index8 = cursor.getColumnIndex(myHelper.DOWNLOADS);
String z = cursor.getString(index0);
String a = cursor.getString(index1);
String b = cursor.getString(index2);
String c = cursor.getString(index3);
String d = cursor.getString(index4);
String e = cursor.getString(index5);
String f = cursor.getString(index6);
String g = cursor.getString(index7);
String h = cursor.getString(index8);
listings[cnt] = new Houses(z, a, b, c, d, e, f, g, h);
Log.d(TAG, listings[cnt].UID + listings[cnt].address + listings[cnt].street + listings[cnt].city + listings[cnt].state + listings[cnt].zip + listings[cnt].upload + listings[cnt].download);
//buffer.append(f + "\n");
cnt++;
}
//Log.d(TAG, "Returning From getData");
//return listings;
cursor.close();
db.close();
//return "";
return listings;
}
Change your method signature to
public Houses[] getData() {

text assigned to String attribute does not store properly

I have assigned a text from a txt file to three attributes, but whenever I call any of those attributes with a Get Method to another class, the value displayed is "null".
Furthermore, I have confirmed that these values are displayed in the method leerArchivo (whenever I use a println for m_linea1-2-3).
Please Help.
public class ArchivoCasillas
{
String m_linea1;
String m_linea2;
String m_linea3;
public void crearArchivo()
{
try
{
FileWriter fw = new FileWriter("ReglasDelTablero.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(fw);
bw.write("<7,0> , <0,0>");
bw.newLine();
bw.write("<4,1> , <7,2> | <2,7> , <5,5> | <1,2> , <7,4> | <0,4> , <2,5>");
bw.newLine();
bw.write("<7,7> , <3,6> | <6,4> , <3,5> | <4,0> , <2,1> | <2,4> , <0,3>");
bw.close();
}
catch(IOException e)
{
System.out.println("error");
}
}
public void leerArchivo()
{
String linea;
int i = 1;
try
{
FileReader fr = new FileReader("ReglasDelTablero.txt");
BufferedReader br = new BufferedReader(fr);
Tablero serpientesEscaleras = new Tablero();
while( (linea = br.readLine() ) != null)
{
switch(i)
{
case 1: m_linea1 = linea;
break;
case 2: m_linea2 = linea;
break;
case 3: m_linea3 = linea;
break;
}
i++;
}
br.close();
}
catch(IOException e)
{
}
}
public String getM_linea1()
{
return m_linea1;
}
}
Problem solved. It was a conceptual problem on my part. I created an Object in another class, but I never called the leerArchivo method with that object, therefore, the value of the attributes was null. Leaving an answer in case it's useful for someone in the future, as I didn't get a reply.

Google API throws exception at specified lat & long position?

Below is my sample code
public static string GetGeoLoc(string latitude, string longitude,
out string Address_ShortCountryName,
out string Address_country,
out string Address_administrative_area_level_1,
out string Address_administrative_area_level_1_short_name,
out string Address_administrative_area_level_2,
out string Address_administrative_area_level_3,
out string Address_colloquial_area,
out string Address_locality,
out string Address_sublocality,
out string Address_neighborhood)
{
Address_ShortCountryName = "";
Address_country = "";
Address_administrative_area_level_1 = "";
Address_administrative_area_level_1_short_name = "";
Address_administrative_area_level_2 = "";
Address_administrative_area_level_3 = "";
Address_colloquial_area = "";
Address_locality = "";
Address_sublocality = "";
Address_neighborhood = "";
XmlDocument doc = new XmlDocument();
try
{
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false");
XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
if (element.InnerText == "ZERO_RESULTS")
{
return ("No data available for the specified location");
}
else
{
element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
string longname = "";
string shortname = "";
string typename = "";
XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");
foreach (XmlNode xn in xnList)
{
try
{
longname = xn["long_name"].InnerText;
shortname = xn["short_name"].InnerText;
typename = xn["type"].InnerText;
switch (typename)
{
case "country":
{
Address_country = longname;
Address_ShortCountryName = shortname;
break;
}
case "locality":
{
Address_locality = longname;
break;
}
case "sublocality":
{
Address_sublocality = longname;
break;
}
case "neighborhood":
{
Address_neighborhood = longname;
break;
}
case "colloquial_area":
{
Address_colloquial_area = longname;
break;
}
case "administrative_area_level_1":
{
Address_administrative_area_level_1 = longname;
Address_administrative_area_level_1_short_name = shortname;
break;
}
case "administrative_area_level_2":
{
Address_administrative_area_level_2 = longname;
break;
}
case "administrative_area_level_3":
{
Address_administrative_area_level_3 = longname;
break;
}
default:
break;
}
}
catch (Exception e)
{
clsExHandler.Instance.Write(e);
}
}
return (element.InnerText);
}
}
catch (Exception ex)
{
return ("(Address lookup failed: ) " + ex.Message);
}
}
try passing latitude as 33.4965 & longitude as -112.205
i'm getting an exception object reference to invalid object in the line
**typename = xn["type"].InnerText;**
when i debug step by step there is no such attribute like ["type"]
Also there are some other lingual character why?
How could i resolve this issue.
I'm not familiar with c# and Im not sure if your code is correct at all(e.g. types is not an attribute, it's an elementNode).
Assuming that your code is correct and you can select nodes by using node['nameOfChildNode'] , when you inspect the XML-File: http://maps.googleapis.com/maps/api/geocode/xml?latlng=33.4965,-112.205&sensor=false you will see that there are address_components with 2 <type>'s and also address_components without any <type> .
I guess your code breaks not at the missing <type>, it breaks when you try to access a property(InnerText) of the missing <type>.
What you can do: use selectSingleNode to select the <type> and when it returns null implement a fallback or leave the further processing.
http://maps.googleapis.com/maps/api/geocode/json?latlng=33.4965%20,%20-112.205&sensor=false
returns
{
"results" : [],
"status" : "ZERO_RESULTS"
}
Therefore
XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
if (element.InnerText == "ZERO_RESULTS")
{
return ("No data available for the specified location");
}
is not catching ZERO_RESULTS.
I am not familiar with C# so I cannot help further.

Resources