Search code examples
c++jsonnlohmann-json

I'm getting a [json.exception.type_error.302] for some reason in my code. I know what the error means but I don't know where there's a fault


My code goes through the values of each sample in a JSON file and gives out the number of sorting comparisons, the amount of memory, and time it takes for the sorting to happen. But for some reason the code is throwing me an error. Would love it if someone could help me out. I'm getting a [json.exception.type_error.302] for some reason in my code. I know what the error means but I don't know where there's a fault

To be exact, this is the error I'm getting - "libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::type_error: [json.exception.type_error.302] type must be number, but is null"

#include <iostream>
#include <fstream>
#include "json.hpp"
#include "mergesort.h"
#include "insertionsort.h"
#include "quicksort.h"
#include <ctime>
#include <iomanip>

int main(int argc, char* argv[]) {
  std::ifstream file;
  file.open(argv[1]);
  nlohmann::json jsonObject;
  if (file.is_open()) {
    file >> jsonObject;
  } else {
    std::cout << "Invalid Argument" << std::endl;
  }

  clock_t mergeTime, quickTime, insertTime;
  extern int insertMemAccesses, insertCompare, mergeMemAccesses, mergeCompare, quickCompare, quickMemAccesses;
//    std::cout << jsonObject;
//    nlohmann::json jsonOutput;

  std::cout << "Sample,InsertSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess";

  int arraysize = jsonObject["metadata"]["arraySize"];
  int numsamples = jsonObject["metadata"]["numSamples"];


  for (int i = 0; i <= numsamples; i++) {
    std::string sample;
    if (i < 9) {
      sample = "Sample0" + std::to_string(i+1);
    } else {
      sample = "Sample" + std::to_string(i+1);
    }

    std::vector<int> insertion;
    std::vector<int> merge;
    std::vector<int> quick;

    for(int j = 0; j <= arraysize; j++){
      insertion.push_back(jsonObject[sample][j]);
      merge.push_back(jsonObject[sample][j]);
      quick.push_back(jsonObject[sample][j]);
    }

    insertTime = clock();
    InsertionSort(&insertion);
    insertTime = clock() - insertTime;

    insertTime = ((unsigned long)insertTime)/CLOCKS_PER_SEC;

    mergeTime = clock();
    MergeSort(&merge);
    mergeTime = clock() - mergeTime;

    mergeTime = ((unsigned long)mergeTime)/CLOCKS_PER_SEC;

    quickTime = clock();
    QuickSort(&quick);
    quickTime = clock() - quickTime;

    quickTime = ((unsigned long)quickTime)/CLOCKS_PER_SEC;

    std::cout<<sample<<",";
    printf("%.6lu,%d,%d,%.6lu,%d,%d,%.6lu,%d,%d\n",insertTime,insertCompare,insertMemAccesses,mergeTime,mergeCompare,mergeMemAccesses,quickTime,quickCompare,quickMemAccesses);


    insertCompare = 0;
    insertMemAccesses = 0;
    mergeCompare = 0;
    mergeMemAccesses = 0;
    quickCompare = 0;
    quickMemAccesses = 0;

  }
  return 0;
}

Solution

  • Step through the code, and which line does the exception happen on? If you don't have a way to step through, then add in some std::cerr calls throughout to see where it is failing. I would assume that the error occurs on one of the push_back lines, most likely the first. You have j <= arraysize, which most likely goes out of bounds, so when you try to access jsonObject[sample][arraysize] this causes the error.

    Also, you could probably just do quick = merge = insertion = jsonObject[sample].get<std::vector<int>>(), instead of needing the loop