0

While developing a button, I want the output file generated by the button to have a predefined name consisting in a constant string "PositionalAccuracySamplePoints" and a variable string consisting on the first 15 characters of my gdb Name.

I am new to coding and struggling with understanding what I am doing wrong when trying to call the gdbName variable. I acutally believe that it has to do with the _gdbName value that I define outside the method, that is not getting the substring value that is given inside the method.

    private static String _inputGdbPath = ("C:\\Users\\GMartin\\Documents\\Entorno pruebas\\Datos_SEA\\SEA19_0308_07C_20190513.gdb");
    public static String _gdbName;

    public Salida(String gdbName)
    {

        GeodatabaseManage gdbManageInput = new GeodatabaseManage(_inputGdbPath);
        gdbName = _inputGdbPath;
        _gdbName = gdbName.Substring(gdbName.LastIndexOf('\\') + 15);

    }


    public string fcName = String.Format("PositionalAccuracySamplePoints" + " _ " + "{0}", _gdbName);

With this code I get a NullValueReference exception, as mentioned has to do with the _gdbName of the last line not getting the value of de _gdbName of the inside but the empty value of the String.

Thanks in advance

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • 2
    I do not see where you have assigned any value for _inputGdbPath – Mukul Keshari Nov 06 '19 at 08:35
  • Lets asume its like this for now: public static String _inputGdbPath = ("C:\\Users\\GMartin\\Documents\\Entorno pruebas\\Datos_SEA\\SEA19_0308_07C_20190513.gdb"); In any case, I am not able to link the value outside with the value inside. I either get the NullReferenceExecption, or get simple that my_gdbName is empty (the name of the final shp. is "PositionalAccuracySamplePoints_" but no code – Guillermo Martín Jiménez Nov 06 '19 at 09:45
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stephen Kennedy Nov 06 '19 at 20:28

1 Answers1

0

It seems that there are some problems with your code. Here is what I can see based on what you share.

  1. The variable _inputGdbPath is never assigned.
  2. You are doing the logic inside the constructor from the button. It expects a string gdbName that you are replacing with the _inputGdbPath, which, as said on item 1, is never assigned.
  3. You try to get a GeodatabaseManage based on the _inputGdbPath, which is never used and could return some null reference, as the _inputGdbPath is null.
  4. Your last line inside the constructor is doing _gdbName = gdbName.Substring(15). The gdbName is the parameter received and that now is being replaced by the (null) _inputGdbPath. If you just need this parameter, you can remove the rest of the code on the constructor and leave only this line.
  5. You are using static variables. That's not recommended, as every button will try to share the same _inputGdbPath and gdbName.
  6. Another point, but more as a kind of improvement, you could create a property with private set and assign the value directly inside the constructor. This way, the app will not do the concatenation of the string every time you try to get it. And you for sure can improve the format of the string.
public string FcName { get; private set; }

public Salida(string gdbName)
{
    //GeodatabaseManage gdbManageInput = new GeodatabaseManage(_inputGdbPath);
    //gdbName = _inputGdbPath;
    //_gdbName = gdbName.Substring(15);
    FcName = string.Format("PositionalAccuracySamplePoints_{0}", gdbName.Substring(15));
}