{"record":{"id":"a0f5cb2c14945625","repo":"dgraph-io/dgraph","slug":"invalid-conversion-s-to-nil","errorCode":null,"errorMessage":"invalid conversion %s to nil","messagePattern":"invalid conversion (.+?) to nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"types/conversion.go","lineNumber":483,"sourceCode":"\t\t\t\tvc := BytesAsFloatArray(data)\n\t\t\t\t*res = vc\n\t\t\tcase StringID:\n\t\t\t\tvc := BytesAsFloatArray(data)\n\t\t\t\tsa := FloatArrayAsString(vc)\n\t\t\t\t*res = sa\n\t\t\tdefault:\n\t\t\t\treturn to, cantConvert(fromID, toID)\n\t\t\t}\n\t\t}\n\tdefault:\n\t\treturn to, cantConvert(fromID, toID)\n\t}\n\treturn to, nil\n}\n\nfunc Marshal(from Val, to *Val) error {\n\tif to == nil {\n\t\treturn errors.Errorf(\"invalid conversion %s to nil\", from.Tid.Name())\n\t}\n\n\tfromID := from.Tid\n\ttoID := to.Tid\n\tval := from.Value\n\tres := &to.Value\n\n\t// This is a default value from sg.fillVars, don't convert it's empty.\n\t// Fixes issue #2980.\n\tif val == nil {\n\t\t*to = ValueForType(toID)\n\t\treturn nil\n\t}\n\n\tswitch fromID {\n\tcase BinaryID:\n\t\tvc := val.([]byte)\n\t\tswitch toID {","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/types/conversion.go#L465-L501","documentation":"types.Marshal returns this when the destination *Val is nil. Marshal writes the converted value through the destination pointer, so a nil target cannot receive the result. The error message names the source type (from.Tid.Name()) to help identify the call site.","triggerScenarios":"Calling types.Marshal(from, nil) — commonly a nil *Val variable passed by mistake, or a function returning a nil *Val that is forwarded to Marshal.","commonSituations":"Refactors that changed a Val value to a *Val pointer left nil on early-return paths; structs holding *Val fields that were never initialized.","solutions":["Initialize the destination: out := &types.Val{Tid: types.StringID} (or the expected target type) before calling Marshal","Check for nil before calling and return a caller-appropriate error","Fix upstream functions that may return a nil *Val"],"exampleFix":"// before\nvar out *types.Val\ntypes.Marshal(types.Val{Tid: types.StringID, Value: \"x\"}, out) // panics path / error\n// after\nout := &types.Val{Tid: types.StringID}\nerr := types.Marshal(types.Val{Tid: types.StringID, Value: \"x\"}, out)","handlingStrategy":"type-guard","validationCode":"if out == nil {\n    return errors.New(\"destination Val must be initialized before Marshal\")\n}\nerr := types.Marshal(from, out)","typeGuard":"func marshalSafe(from types.Val, to *types.Val) error {\n    if to == nil { return errors.New(\"nil destination Val\") }\n    return types.Marshal(from, to)\n}","tryCatchPattern":"var out *types.Val // may be nil from upstream\nif err := types.Marshal(from, out); err != nil && strings.Contains(err.Error(), \"to nil\") {\n    out = &types.Val{Tid: from.Tid}\n    err = types.Marshal(from, out)\n}","preventionTips":["Initialize destination Vals at declaration: out := &types.Val{...}","Audit functions that return *Val for nil returns","Prefer value types plus explicit allocation over possibly-nil pointers"],"tags":["types","marshal","nil-pointer","api-misuse"],"backgroundTag":"nil-argument","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}