{"record":{"id":"9fbee4ba9dd72dcf","repo":"lionsoul2014/ip2region","slug":"both-xdbfile-and-xdbpath-is-null","errorCode":null,"errorMessage":"Both xdbFile and xdbPath is null","messagePattern":"Both xdbFile and xdbPath is null","errorType":"validation","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"binding/java/src/main/java/org/lionsoul/ip2region/service/ConfigBuilder.java","lineNumber":115,"sourceCode":"\n            // 2, load the header\n            final Header header = Searcher.loadHeaderFromBuffer(cBuffer);\n\n            // 3, verify the xdb from the buffer\n            Searcher.verify(header, cBuffer.length());\n\n            // create the config without xdbFile and vIndex\n            return new Config(cachePolicy, ipVersion, null, header, null, cBuffer, searchers, fairLock);\n        }\n\n        // load the header and the cache buffer\n        final File xdbFile;\n        if (this.xdbFile != null) {\n            xdbFile = this.xdbFile;\n        } else if (this.xdbPath != null) {\n            xdbFile = new File(this.xdbPath);\n        } else {\n            throw new InvalidConfigException(\"Both xdbFile and xdbPath is null\");\n        }\n\n        final RandomAccessFile raf = new RandomAccessFile(xdbFile, \"r\");\n\n        // 1, verify the xdb\n        Searcher.verify(raf);\n\n        // 2, load the header\n        final Header header = Searcher.loadHeader(raf);\n\n        // 3, check and load the vector index buffer\n        final byte[] vIndex = cachePolicy == Config.VIndexCache ? Searcher.loadVectorIndex(raf) : null;\n\n        // 4, check and load the content buffer\n        final LongByteArray cBuffer = cachePolicy == Config.BufferCache ? Searcher.loadContent(raf, cacheSliceBytes) : null;\n\n        raf.close();\n        return new Config(cachePolicy, ipVersion, xdbFile, header, vIndex, cBuffer, searchers, fairLock);","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/java/src/main/java/org/lionsoul/ip2region/service/ConfigBuilder.java#L97-L133","documentation":"ConfigBuilder.build() requires a source xdb file. Neither setXdbFile nor setXdbPath was called (and no stream was provided), so there is nothing to open with RandomAccessFile. The builder validates this up front instead of failing later with a cryptic NPE or FileNotFoundException.","triggerScenarios":"Creating a new ConfigBuilder and calling asV4()/asV6() without ever invoking setXdbFile(File) or setXdbPath(String), or invoking them with null arguments.","commonSituations":"Building the searcher programmatically from config where the xdb path property was missing/empty; forgetting the builder step when copying sample initialization code.","solutions":["Call builder.setXdbPath(\"/path/to/ip2region.xdb\") or builder.setXdbFile(new File(...)) before asV4()/asV6()","Check that your configuration source (properties/env) actually supplies the xdb path and that it is not null/empty","If you intended stream-based init, call setXdbInputStream with cachePolicy=Config.BufferCache instead"],"exampleFix":"// before\nIp2Region region = new ConfigBuilder().asV4();\n// after\nIp2Region region = new ConfigBuilder()\n    .setXdbPath(\"./data/ip2region.xdb\")\n    .asV4();","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(config.getXdbFile(), \"xdbFile must be set\");\nObjects.requireNonNull(config.getXdbPath(), \"xdbPath must be set\");\nif (config.getXdbFile() == null && config.getXdbPath() == null) {\n    throw new IllegalStateException(\"provide xdbFile or xdbPath before building\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ip2Region = new ConfigBuilder().setXdbPath(xdbPath).asV4();\n} catch (InvalidConfigException | IOException e) {\n    log.error(\"xdb source missing or unreadable\", e);\n}","preventionTips":["Centralize xdb path configuration and fail fast at startup if missing","Never call asV4/asV6 on a bare ConfigBuilder in code reviews' sample snippets","Prefer setXdbPath from a validated config property with a default location"],"tags":["java","configuration","missing-file"],"backgroundTag":"missing-xdb-file","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}