Hide Payment Method: NullReferenceException: Object reference not set to an instance of an object.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
I tried Hide Cash on Delivery Payment when customer choose Pick-in store method.
Here my HidePaymentMethod
  public bool HidePaymentMethod(IList<ShoppingCartItem> cart)
        {
            var customer = cart.FirstOrDefault(item => item.Customer != null)?.Customer;
            var shippingOption = _genericAttributeService.GetAttribute<ShippingOption>(customer, NopCustomerDefaults.SelectedShippingOptionAttribute, _storeContext.CurrentStore.Id);
            var shippingMethodSystemName = "Nop.Shipping.PickupInStore";

            if (shippingOption.ShippingRateComputationMethodSystemName.Equals(shippingMethodSystemName))
                return true;

            return false;


        }

Here are errors

Raw exception details:
System.NullReferenceException: Object reference not set to an instance of an object.
   at Nop.Plugin.Payments.CashOnDelivery.CashOnDeliveryPaymentProcessor.HidePaymentMethod(IList`1 cart)
   at Nop.Web.Factories.ShoppingCartModelFactory.<>c__DisplayClass53_0.<PrepareShoppingCartModel>b__2(IPaymentMethod pm) in D:\website\hoc\src\Presentation\Nop.Web\Factories\ShoppingCartModelFactory.cs:line 881
   at System.Linq.Enumerable.WhereListIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Nop.Web.Factories.ShoppingCartModelFactory.PrepareShoppingCartModel(ShoppingCartModel model, IList`1 cart, Boolean isEditable, Boolean validateCheckoutAttributes, Boolean prepareAndDisplayOrderReviewData) in D:\website\hoc\src\Presentation\Nop.Web\Factories\ShoppingCartModelFactory.cs:line 879
   at Nop.Web.Controllers.ShoppingCartController.Cart() in D:\website\hoc\src\Presentation\Nop.Web\Controllers\ShoppingCartController.cs:line 1295
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at WebMarkupMin.AspNetCore2.WebMarkupMinMiddleware.Invoke(HttpContext context)
   at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\projects\dotnet\src\MiniProfiler.AspNetCore\MiniProfilerMiddleware.cs:line 94
   at Nop.Services.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) in D:\website\hoc\src\Libraries\Nop.Services\Authentication\AuthenticationMiddleware.cs:line 79
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Nop.Services.Installation.InstallUrlMiddleware.Invoke(HttpContext context, IWebHelper webHelper) in D:\website\hoc\src\Libraries\Nop.Services\Installation\InstallUrlMiddleware.cs:line 52
   at Nop.Services.Common.KeepAliveMiddleware.Invoke(HttpContext context, IWebHelper webHelper) in D:\website\hoc\src\Libraries\Nop.Services\Common\KeepAliveMiddleware.cs:line 49
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
   at Nop.Web.Framework.Infrastructure.Extensions.ApplicationBuilderExtensions.<>c.<UseNopExceptionHandler>b__1_1(HttpContext context) in D:\website\hoc\src\Presentation\Nop.Web.Framework\Infrastructure\Extensions\ApplicationBuilderExtensions.cs:line 89
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
4 years ago
1) Did you compile your plugin in Debug mode?  Because the exception message you show is not showing where in your plugin the exception occurred.

2) I suspect it is here, because GetAttribute may not find a match:
            var shippingOption = _genericAttributeService.GetAttribute<ShippingOption>(...

3) You don't say what you were doing at the time of the exception, but note that the shopping cart page itself may attempt to look at what payment methods are available (e.g. to determine button payment methods ,etc.), so you can't always rely on the customer having selected a shipping method yet.
4 years ago
New York wrote:
1) Did you compile your plugin in Debug mode?  Because the exception message you show is not showing where in your plugin the exception occurred.

2) I suspect it is here, because GetAttribute may not find a match:
            var shippingOption = _genericAttributeService.GetAttribute<ShippingOption>(...

3) You don't say what you were doing at the time of the exception, but note that the shopping cart page itself may attempt to look at what payment methods are available (e.g. to determine button payment methods ,etc.), so you can't always rely on the customer having selected a shipping method yet.

I want compile in debug mode, but when I tried rebuild and run on local but not working. then I tried upload to server, but still can not install a plugin. I get this message on view Log: The plugin "Payments.CashOnDelivery" not installed.
4 years ago
Post the  entire log message here
4 years ago
honjuhoxx wrote:
I tried Hide Cash on Delivery Payment when customer choose Pick-in store method.
Here my HidePaymentMethod
  public bool HidePaymentMethod(IList<ShoppingCartItem> cart)
        {
            var customer = cart.FirstOrDefault(item => item.Customer != null)?.Customer;
            var shippingOption = _genericAttributeService.GetAttribute<ShippingOption>(customer, NopCustomerDefaults.SelectedShippingOptionAttribute, _storeContext.CurrentStore.Id);
            var shippingMethodSystemName = "Nop.Shipping.PickupInStore";

            if (shippingOption.ShippingRateComputationMethodSystemName.Equals(shippingMethodSystemName))
                return true;

            return false;


        }


Did you try with this code?

public bool HidePaymentMethod(IList<ShoppingCartItem> cart)
{
    var customer = cart.FirstOrDefault(item => item.Customer != null)?.Customer;
    var shippingOption = _genericAttributeService.GetAttribute<ShippingOption>(customer, NopCustomerDefaults.SelectedShippingOptionAttribute, _storeContext.CurrentStore.Id);
    var shippingMethodSystemName = "Nop.Shipping.PickupInStore";

    if (shippingOption?.ShippingRateComputationMethodSystemName == shippingMethodSystemName)
        return true;

    return false;
}
4 years ago
New York wrote:
Post the  entire log message here

Here my log message
   Nop.Core.NopException: No constructor was found that had all the dependencies satisfied. ---> Nop.Core.NopException: Unknown dependency
   at Nop.Core.Infrastructure.NopEngine.<ResolveUnregistered>b__14_0(ParameterInfo parameter) in D:\nop\nopCommerce_4.20_Source\src\Libraries\Nop.Core\Infrastructure\NopEngine.cs:line 253
   at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Nop.Core.Infrastructure.NopEngine.ResolveUnregistered(Type type) in D:\nop\nopCommerce_4.20_Source\src\Libraries\Nop.Core\Infrastructure\NopEngine.cs:line 257
   --- End of inner exception stack trace ---
   at Nop.Core.Infrastructure.NopEngine.ResolveUnregistered(Type type) in D:\nop\nopCommerce_4.20_Source\src\Libraries\Nop.Core\Infrastructure\NopEngine.cs:line 266
   at Nop.Services.Plugins.PluginDescriptor.Instance[TPlugin]() in D:\nop\nopCommerce_4.20_Source\src\Libraries\Nop.Services\Plugins\PluginDescriptor.cs:line 46
   at Nop.Services.Plugins.PluginService.InstallPlugins() in D:\nop\nopCommerce_4.20_Source\src\Libraries\Nop.Services\Plugins\PluginService.cs:line 419
4 years ago
mhsjaber wrote:

Did you try with this code?

I tried rebuild the plugin, but I cant not install the plugin
4 years ago
New York wrote:
Post the  entire log message here

I fixed the constructor, I think my code not working
4 years ago
If you're still having problems with this, then you should consider using Payment Director plugin.  It's very easy to show a payment method conditionally.   In your case, to show COD only if not a pick up point (= an address was entered)

Option
Payments.CashOnDelivery
ShippingAddress <> null
4 years ago
New York wrote:
If you're still having problems with this, then you should consider using Payment Director plugin.  It's very easy to show a payment method conditionally.   In your case, to show COD only if not a pick up point (= an address was entered)

Option
Payments.CashOnDelivery
ShippingAddress <> null

Thanks you very much, I wil try your plugin.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.