Can't edit a settings. always null
-
venerdì 3 agosto 2012 21:51
Hello I am having trouble editing my settings. I have multiple listview settings that I need to edit. I use an if statement to decide which one needs to be edited. The if statement determines the right listview and textbox but when setting the listview setting, the setting is always null. but i can read and edit just a normal text setting just fine
Note: i tred a string collection setting and had the same error
i get this error un the "Add case on the "curLV.Item.add()" line and happens for the other cases also when i have the correct code in their lol
error: "Object reference not set to an instance of an object."
public partial class Form1 : Form { //Variables string moviename; Int16 Year; string Res; string finalFileName; bool fileNameChack = true; ListView currentListview = new ListView(); TextBox currentTextbox = new TextBox(); //Edit the FileList private void AddRemClrList(object sender, EventArgs e) { var myButton = (Button)sender; //Determine listview, textbox, and sttings to work with if (myButton.Name.Contains("btngbs")) { currentListview = lvgbsSubstrings; currentTextbox = txtgbsAddString; curLV = Properties.Settings.Default.tlrString; } if (myButton.Name.Contains("btngbe")) { currentListview = lvgbeExtentions; currentTextbox = txtgbeAddExten; curLV = Properties.Settings.Default.tlrExt; } if (myButton.Name.Contains("btngbm")) { currentListview = lvgbmFolderStructre; currentTextbox = txtgbmFile; curLV = Properties.Settings.Default.folderStructure; } switch (myButton.Text.ToString()) { case "Add": if (currentTextbox.Text.Trim().ToString() != "") { currentListview.Items.Add(currentTextbox.Text); curLV.Items.Add(currentTextbox.Text); } break; case "Remove": foreach (ListViewItem itm in currentListview.SelectedItems) { currentListview.Items.Remove(itm); //StringCol.Remove(itm.Text); } break; case "Clear": currentListview.Items.Clear();
//StringCol.Clear(); break; } } }
Tutte le risposte
-
venerdì 3 agosto 2012 22:09
Hi,
Where this error occurs exactly, or which line of code (use break point to locate it).
Mitja
-
venerdì 3 agosto 2012 22:39
-
venerdì 3 agosto 2012 23:13
1. Are you sure you have initialized this listView?
2. If so, does this listView have columns?
You can try using ListViewItem class:
//for example that your listView has 2 columns: ListViewItem lvi = new ListViewItem(); lvi.Text = "text for 1st column"; lvi.Subitems.Add("text for 2nd column"); //if you have mor e columns do the same way! //add items to listView listView1.Items.Add(lvi);
But 1st make sure those 2 points are true!
Mitja
-
venerdì 3 agosto 2012 23:22
I have initialized the listview. and the listview im trying to edit is actually a setting. not a visual listview.
I get this same error when I use stringcollections so i dont think its a problem with the listview.
-
venerdì 3 agosto 2012 23:23Like a problem with the assignment of the current listview in the if statments
-
sabato 4 agosto 2012 08:47
Hi,
just out of curiosity, in your example code there is no declaration of curLV. What is it?
Also, are you sure, that one of your three
if (myButton.Name.Contains(...
is really processed, because I can only see the assignment to curLV in this code?
Cheers, Dany
-
domenica 5 agosto 2012 00:41
Yea i noticed that i never declared it after i posted the code. i added it and i still get the same error.
curLV is a listview setting.
and yes the if statement does get processed. it selects the right gui listbox and textbox, but when assigning the curLV which is a settings assigned as a listbox for some reason curLV ends up being null.
-
martedì 7 agosto 2012 17:40
Your user.config file is probably without your data. After some investigation I found that ListView is not fit for this type of serialization.
In Application Settings(MSDN) you can read that
Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/ FromString.
and unfortunately ListView is not XML serializable.
You might want to do the serialization / deserialization by your own code.
I would recommend - when your list items are strings - that you join the items into a string setting and split that afterwards again to fill in your items collection.
Cheers, Dany

