0

In the below code i have a string value it has a path.I want to place the String value inside the static method But i get Object reference not set to an instance of an object.if i write path in that code it works but not string value which has path .pls help me to solve the issue.

 var projectname = name.ProjectName;
 var batchname = name.BatchName;
 var imagename = name.ImageName;
 string concatenatedStr = "/"+ projectname + "/" + batchname + "/Input/" + imagename; 




[WebMethod]
public static string buttonclickImage(string pageNo)
{

       int iPageNo = 0;
       if (pageNo != string.Empty && pageNo != "undefined")
          iPageNo = Int32.Parse(pageNo);

       FileTransfer.FileTransferClient fileTranz = new FileTransfer.FileTransferClient();
       FileDto file = fileTranz.GetTifftoJPEG("concatenatedStr", iPageNo, "gmasdll"); 


       var fileData = Convert.ToBase64String(file.Content);//throws error
       return fileData;
 }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
user3319384
  • 35
  • 1
  • 11
  • Have you tried debugging? i.e. whack a break-point into the method and **see where it breaks**? and look at what the values are - which are `null` etc – Marc Gravell Mar 27 '14 at 11:30
  • well, you want concatenatedStr to be a variable in `fileTranz.GetTifftoJPEG` if I understand well. Than why don't you build concatenated string in buttonclickImage, or add a parameter in buttonclickImage method ? – Raphaël Althaus Mar 27 '14 at 11:31

4 Answers4

3

it means that either file is null or file.Content is null. You can avoid the exception by

if(file!=null && file.Content!=null)
{
   //your remaining code
}

ideally though you should first check the reason why it is null

Edit: From your comments i infer that you want to pass your varible. Either make your string static, or make your method not static or pass the string to your method

[WebMethod]
public static string buttonclickImage(string pageNo)
{

       int iPageNo = 0;
       if (pageNo != string.Empty && pageNo != "undefined")
          iPageNo = Int32.Parse(pageNo);

       FileTransfer.FileTransferClient fileTranz = new FileTransfer.FileTransferClient();
       //note the change here. no double quotes.
       FileDto file = fileTranz.GetTifftoJPEG(concatenatedStr, iPageNo, "gmasdll"); 


       var fileData = Convert.ToBase64String(file.Content);//throws error
       return fileData;
 }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
1

You're not passing in the value stored in the concatenatedStr variable... you're passing in the literal string "concatenatedStr".

Change this:

FileDto file = fileTranz.GetTifftoJPEG("concatenatedStr", iPageNo, "gmasdll"); 

To this:

FileDto file = fileTranz.GetTifftoJPEG(concatenatedStr, iPageNo, "gmasdll");

You'll also need to make your variables static, since your method is static. Or leave the variables as they are, and make the method non-static, if that's an option.

I'm a little confused about where those variables are located though. They appear to be class-level in scope, but then you wouldn't be able to use var in that location.

I guess you could also modify your method to accept an additional parameter, and then pass in the value from wherever you're calling this.

public static string buttonclickImage(string pageNo, string concatenatedStr)
{
    ...
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

This works

[WebMethod]
        public static string buttonclickImage(string pageNo)
        {
            var name = (name)HttpContext.Current.Session["Projectname"];
            var projectname = name.ProjectName;
            var batchname = name.BatchName;
            var imagename = name.ImageName;
            string concatenatedStr = "/" + projectname + "/" + batchname + "/Input/" + imagename;

            int iPageNo = 0;
            if (pageNo != string.Empty && pageNo != "undefined")
                iPageNo = Int32.Parse(pageNo);

            FileTransfer.FileTransferClient fileTranz = new FileTransfer.FileTransferClient();
            FileDto file = fileTranz.GetTifftoJPEG(concatenatedStr, iPageNo, "gmasdll");


            var fileData = Convert.ToBase64String(file.Content);

            return fileData;
        }
user3319384
  • 35
  • 1
  • 11
0

First of all construct value of "concatenatedStr" variable using String.Format.

For Eg:-

var projectname = name.ProjectName;

var batchname = name.BatchName;

var imagename = name.ImageName;

string concatenatedStr = string.Format("/{0}/{1}/Input/{2}", projectname, batchname, imagename);

Put debug point here and check what value "concatenatedStr" has. If "concatenatedStr" is null then definitely you will get "Nullreference exception"....

So may be there is a problem with "concatenatedStr".....So deeply check concatenation variables too...

Hope this works......

Karna
  • 47
  • 3