Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this piece of code:

C#
 string getOrder = "SELECT * FROM `wpet_woocommerce_order_items` a LEFT JOIN `wpet_woocommerce_order_itemmeta` b ON a.order_item_id = b.order_item_id LEFT JOIN `wpet_users` f ON f.ID = b.meta_value AND b.meta_key = 'seller_id' LEFT JOIN `wpet_dokan_orders` c  ON a.order_id = c.order_id LEFT JOIN `wpet_posts` d ON a.order_id = d.ID   WHERE a.order_id =" + cx.Field<ulong>("ID")  + " AND a.order_item_type = 'line_item' AND d.post_status = 'wc-completed';";
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand(getOrder, conn);

                    status.Text = conn.State.ToString();
                    // conn.Open();
                    MySqlDataAdapter dt = new MySqlDataAdapter(cmd);

                    DataTable wcOrder_data = new DataTable();
                    dt.Fill(wcOrder_data);

                    GridView3.DataSource = wcOrder_data;
                    GridView3.DataBind();

                    var toListing = wcOrder_data.Select().ToList();
                    // var reform = wcOrder_data.AsEnumerable();
                    conn.Close();

//ERROR POINT -->
                    var getUniqueItems = wcOrder_data.AsEnumerable().Where(x => x.Field<ulong>("order_item_id") > 0).GroupBy(x => x.Field<ulong>("order_item_id")).Select(g => new
                    {
                        ItemID = g.Key.ToString(),
                        ItemName = g.Select(x => x.Field<string>("order_item_name")).FirstOrDefault(),
                        Order = g.Select(x => x.Field<UInt64>("order_id").ToString()).FirstOrDefault(),
                        Vendor = g.Select(x => x.Field<Int64>("seller_id")).FirstOrDefault().ToString(),
                        ItemAmount = g.Where(x => x.Field<string>("meta_key") == "_line_subtotal").Select(x => x.Field<string>("meta_value").ToString()).FirstOrDefault(),
                        ItemLineTotal = g.Where(x => x.Field<string>("meta_key") == "_line_total").Select(x => x.Field<string>("meta_value").ToString()).FirstOrDefault(),

                        Commission = g.Where(x => x.Field<string>("meta_key") == "_dokan_commission_rate").Select(x => x.Field<string>("meta_value").ToString()).FirstOrDefault(),
                        Qty = g.Where(x => x.Field<string>("meta_key") == "_qty").Select(x => x.Field<string>("meta_value").ToString()).FirstOrDefault(),
                        CommissionToMall = double.Parse(g.Where(x => x.Field<string>("meta_key") == "_line_subtotal").Select(x => x.Field<string>("meta_value")).FirstOrDefault()) * (double.Parse(g.Where(x => x.Field<string>("meta_key") == "_dokan_commission_rate").Select(x => x.Field<string>("meta_value")).FirstOrDefault()) / 100),
                        OrderDate = g.Select(x => x.Field<DateTime>("post_date")).FirstOrDefault(),
                        SellerEmail = g.Select(x => x.Field<string>("user_email")).FirstOrDefault(),
                        VendorName = g.Select(x => x.Field<string>("display_name")).FirstOrDefault(),




                    });


Which returns a null reference error, I am truly stumped by this because the gridview in this code returns data, I do not know why I have this error:

ERROR: Value cannot be null. Parameter name: value::::::mscorlib::: at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Double.Parse(String s) at App.tester.<>c.b__2_2(IGrouping`2 g) in C:\Users\Kent\Documents\Visual Studio 2015\Projects\App\tester.aspx.cs:line 60 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at App.tester.Button1_Click(Object sender, EventArgs e) in C:\Users\Kent\Documents\Visual Studio 2015\Projects\App\tester.aspx.cs:line 84


What I have tried:

I have tried all sorts of fixes, getting the data into a list, but it keeps returning null reference. The error also I do not fully understand. Please let someone assist me with this. Thanks.
Posted
Updated 14-Aug-20 13:46pm
v2
Comments
Jin Vincent Necesario 14-Aug-20 8:36am    
I think the problem lies in this line: CommissionToMall = double.Parse(g.Where(x => x.Field<string>("meta_key") == "_line_subtotal").Select(x => x.Field<string>("meta_value")).FirstOrDefault()) * (double.Parse(g.Where(x => x.Field<string>("meta_key") == "_dokan_commission_rate").Select(x => x.Field<string>("meta_value")).FirstOrDefault()) / 100).
F-ES Sitecore 14-Aug-20 9:01am    
Break that line down into individual statements, it will make it much easier to know exactly which bit has the problem. One of your "Where" statements is probably returning a null value.
Noetico 14-Aug-20 11:52am    
Yes will do, thank you so much
Noetico 14-Aug-20 11:52am    
Thank you! On it right now!
Noetico 14-Aug-20 12:31pm    
You are on the mark Sir! I commented out these lines and the script moved on, will update the thread. Thanks.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - one or more of the values is null. There may be other data in your gridview, but something is null - and we can't tell what, because we don't have any access to your data!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Be aware this is not going to be a quick job: you are going to have to look very closely at your data to find out exactly where the null is - and then start working backwards from there to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
v2
Comments
Noetico 14-Aug-20 9:19am    
Oh yes, there comes the funny part, there is another error with System.EnterprisesServices dll on the computer, I have been chasing this and replaced all the dlls with good ones, no solution, i have no debugger, visual studio is currently plagued with errors, I would reinstall everything but need to solve this as its time-pressed. No problem, thanks.
OriginalGriff 14-Aug-20 10:04am    
You aren't going to be able to fix this without VS: either to run the debugger, or to compile you app over and over again.

There are two ways to debug an app: use a debugger, or add tracing statements. The latter is by far the slowest way, but it's the one we started with and it' still effective.
Start adding tracing statements to log what is in your data, and what is about to be done to it. Then add more to show what happened to it. This is going to mean breaking your code into separate lines, and monitoring each and every one of them.
Then refine your logging to show more detail on the error data. and repeat, and repeat until you can see where the problem is and why.

I'd scrub and reinstall myself - it'll probably be quicker!
Noetico 14-Aug-20 11:46am    
Thank you, I will do this, will add a few statements to trace my values, the best I can.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900