Tencent/VasSonic · error · NullPointerException
SonicRuntime context con not be null!
Error message
SonicRuntime context con not be null!
What it means
SonicRuntime is the abstract base providing app context and platform services to Sonic. Its constructor throws NullPointerException when passed a null Context, since nearly every runtime operation (file cache, network, main-thread handlers) requires a valid application context.
Solutions
- Pass the Application context (getApplicationContext()) to the runtime constructor instead of an Activity context
- Ensure the runtime is constructed after the Application/Activity is fully attached (e.g. in Application.onCreate)
- Null-check the context provider at the call site before constructing the runtime
- Avoid constructing SonicRuntime in lifecycle callbacks where context may not exist (onAttach before, onDestroy after)
Example fix
// before SonicEngine.createInstance(new SonicRuntimeImpl(getActivity()), config); // NPE when detached // after Context appCtx = MyApplication.getInstance().getApplicationContext(); SonicEngine.createInstance(new SonicRuntimeImpl(appCtx), config);
Defensive patterns
Strategy: validation
Validate before calling
Context ctx = getActivity() != null ? getActivity().getApplicationContext() : MyApplication.getInstance();
if (ctx == null) throw new IllegalStateException("No context available yet");
new SonicRuntimeImpl(ctx); Type guard
// Java
static boolean hasContext(Fragment f) {
return f.isAdded() && f.getContext() != null;
} Try / catch
try {
SonicEngine.createInstance(new SonicRuntimeImpl(ctx), config);
} catch (NullPointerException e) {
Log.e(TAG, "Context was null; defer Sonic init to Application.onCreate", e);
} Prevention
- Always use getApplicationContext() for SDK singletons
- Construct the runtime only in Application.onCreate
- Never build the runtime from detached fragments or destroyed activities
When it happens
Trigger: Invoking a SonicRuntime subclass constructor (directly or via SonicEngine.createInstance) with a null Context argument, e.g. getActivity()/getApplication() returning null in a detached context or during early startup.
Common situations: Creating the runtime in a Fragment/View that is detached; calling createInstance before Application is attached; passing an uninitialized field; refactors that swap a lazy Context getter returning null.
Related errors
- SonicDBHelper::createInstance() needs to be called before…
- SonicEngine::createInstance() needs to be called before…
- This isn't a hierarchical URI.
- Attempt to verify non-SSL socket
- Cannot verify SSL socket without session
AI-assisted analysis of Tencent/VasSonic@59936beff6 (2026-09-08).
Data as JSON: /api/errors/3f4ff2dc547357fa.
Report an issue: GitHub.
Appendix: source
Thrown at sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicRuntime.java:59
/**
* Log filter
*/
private final static String TAG = SonicConstants.SONIC_SDK_LOG_PREFIX + "SonicRuntime";
/**
* A context for this runtime, it's expected to be ApplicationContext
*/
protected final Context context;
/**
* This handle thread use to save sonic cache.
*/
protected volatile static HandlerThread fileHandlerThread;
public SonicRuntime(Context context) {
if (null == context) {
throw new NullPointerException("SonicRuntime context con not be null!");
}
this.context = context;
}
public Context getContext() {
return context;
}
/**
* Make a unique session id for the url, it can be account related.
* @param url Url which need to make session id
* @param isAccountRelated Is account related or not
* @return A unique session id
*/
public String makeSessionId(String url, boolean isAccountRelated) {
if (isSonicUrl(url)) {
StringBuilder sessionIdBuilder = new StringBuilder();View on GitHub (pinned to 59936beff6)