Handling Null Result with class object
What is the exception you are getting?  What do you mean returns null and throws Exception?

From putting this code into a class and executing a test on it, I get back an object or null when the record exists or does not respectively.

If you're getting an exception from the above call with cmf.GetByPrimaryKey, is the exception in the call to the constructor of CategorymasterKeys?

Here is the test I used:

[TestMethod()]
public void TestWhenPassingAnIdNotInTheResultSetToSelectByPrimaryKey_ThenNullIsTheResult()
{
    var target = new Class2();
    CategorymasterKeys keys = new CategorymasterKeys { Category_id = 0 };
    Categorymaster actual = target.SelectByPrimaryKey(keys);
    Assert.IsNull(actual);
}