-2

How to allow null in Context.Request:

context.Response.Write(retrieveList(context.Request["SalCode"].ToString(null), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
ppeterka
  • 20,583
  • 6
  • 63
  • 78
ChristineS
  • 503
  • 1
  • 6
  • 12

2 Answers2

2

Firstly, Request["..."] already returns a string, so there is no need to call ToString() on it and thus no need to worry, at this stage, if it returns null (i.e. if the key is not present in the request).

Thus you can call e.g.

retrieveList(
    context.Request["SalCode"],
    context.Request["groupKeyword"],
    context.Request["text"]);

without worrying if any of the three are null.

You can then alter retrieveList to respond correctly if any of the inputs is null. For example, you could return null:

private string /*or whatever*/ retrieveList(
    string salCode, string groupKeyword, string text)
{
    if (String.IsNullOrEmpty(salCode) ||
        String.IsNullOrEmpty(groupKeyword) ||
        String.IsNullOrEmpty(text))
    {
        return null;
    }
    ...
}

Then, note that Response.Write doesn't care if you give it a null, it just writes nothing, so you can keep the call to Write as above.

Alternatively, you could for example check the return value and write a message if it is null:

var list = retrieveList(
    context.Request["SalCode"],
    context.Request["groupKeyword"],
    context.Request["text"]));
if (!String.IsNullOrEmpty(list))
{
    context.Response.Write(list);
}
else
{
    context.Response.Write("Missing request parameter.");
}
Rawling
  • 49,248
  • 7
  • 89
  • 127
0

TYou didn't specify anything! Please add information, you even didn't specify the environment, the classes, or the general context in which this problem occurs. nor do we know what the signature of retrieveList() is! This makes us very difficult to help you! How much would you like to answer the question, one of my colleagues faced once: 'This crap doesn't work!' ? (Yes, it is not even a question, but happened in a real life support situatiuon!)

One thing I noted is that you use *T*oString() instead of *t*oString(), I assume that is a typo. (edited, as it is clear that this is C#) Also, I don't know what .toString(null) means. Did you want to tell us that that statement causes the NullPointerException? In that case, you could have committed a bit more effort towards us to understand your question, e.g. by writing this down...

BTW, if that's the case, I'd say, this will solve your problem:

Object salCode = context.Request["SalCode"];
context.Response.Write(retrieveList(salCode==null?"":salCode.ToString(), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));

EDIT I think (but have no means to test) that if the null is the problem, this would fix it. If however, the underlying code does not work properly with empty String specified, that should be checked in retrieveList() like this (pasted from referenced post):

private string retrieveList(string SalCode, string groupKeyword, string text)
{
    SqlConnection _sqlCon = default(SqlConnection);
    SqlCommand _sqlCom = default(SqlCommand);
    SqlDataReader _sqlReader = default(SqlDataReader);
    StringBuilder _sb = new StringBuilder();
    List<TokenInputJSON> _out = null;

    try
    {
        _sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SalesianBlastConnectionString"].ConnectionString);
        _sqlCom = new SqlCommand("getTokenInput", _sqlCon);
        _sqlCom.CommandType = CommandType.StoredProcedure;
        _sqlCon.Open();

        //This is the edited part
        if(SalCode==null || SalCode.Equals("")) {
            _sqlCom.Parameters.Add(new SqlParameter("@salCode", SqlDbType.VarChar, 4)).Value = SalCode;
        }
        //... continue with the other parts

Quick check: I just had an idea: use a constant value when calling retrieveList to find out if this is the problem:

context.Response.Write(retrieveList("enterAValidSalCodeHere", context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • this is where my whole code lies and my problem its in C# (http://stackoverflow.com/questions/12832385/webhandler-object-reference-not-set-to-an-instance-of-an-object) – ChristineS Oct 11 '12 at 06:41
  • Oops, I mistakenly thought it was Java. I'll try to enhance my answer to be correct - though I didn't program in C# since a really long time... Hoewver, the problem is that the SalCode, which is used as a parameter to your query in retrieveList(), is null. Is that a valid working condition? How should your program work when SalCode does not contain a valid value? – ppeterka Oct 11 '12 at 06:59
  • 1
    You could also use the null coalescing operator (??) like this: `salCode.ToString() ?? ""` - which basically translates as "if salCode.ToString() is null, use "" "; although since you're not doing an assignment this might not work. – Tim Oct 11 '12 at 07:19
  • Edited my answer, please check if that works. Sadly, I could not test it, as I don't have access to Visual Studio anywhere near... – ppeterka Oct 11 '12 at 07:19
  • @Tim i tried it but the error says "Object reference not set to an instance of an object." – ChristineS Oct 11 '12 at 07:25
  • Hmmmm, just curious (I'm not entirely familiar with C# in this context): is the 'context' a valid object, or does it happen to be null? – ppeterka Oct 11 '12 at 07:28
  • @ppeterka I'm still trying your idea. – ChristineS Oct 11 '12 at 07:29
  • @ppeterka - In this case, I would expect context to be valid (it's an HttpContext object) as OP is implementing IHttpHandler - though it's possible it's null. Which might indicate a larger problem. Easy enough to add a null check on context at the start to find out. – Tim Oct 11 '12 at 07:35
  • @Tim: thanks for clarifying, I never coded anything network related in C#, that's why I'm a bit lost here, but I'm convinced that the problem is related to something along a lot more obvious lines. – ppeterka Oct 11 '12 at 07:41
  • @ppeterka - You're welcome. I agree - I think it might be something with context itself. – Tim Oct 11 '12 at 07:44
  • @ChristineS did the error change? Can you post the stack trace in the post? – ppeterka Oct 11 '12 at 08:20