Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:19:41+00:00 2026-05-30T21:19:41+00:00

I perform some decoding process in the native code and call a Java method

  • 0

I perform some decoding process in the native code and call a Java method from it to write samples into the AudioTrack instance. Decoding process runs fine, the callback from native code to Java:

private void writeToAudioTrack(final byte[] buffer)

is called , but once the Runnable object starts to write samples into the AudioTrack I get a NullPointerException immediately. I am quite sure it is caused by wrong threading, but cannot figure out what is wrong there. I am attaching the complete Java code:

public class Player
{
    private AudioTrack track;
    private boolean isInitialized = false;
    private static Handler handler = new Handler();

    public void init(String mediaSource)
    {
        // Call native function initEngine
        isInitialized = initEngine(mediaSource);

        if (isInitialized)
        {
            int bufSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
            track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufSize, AudioTrack.MODE_STREAM);
            track.play();
        }
    }

    public void play()
    {
        // Call native rendering function in a separate thread
        if (isInitialized)
        {
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    renderAudio();
                }
            }).start();
        }
    }

    public void release()
    {
        isInitialized = false;
        releaseEngine();
    }

    // This is callback from native code
    private void writeToAudioTrack(final byte[] buffer)
    {
        handler.post(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    track.write(buffer, 0, buffer.length);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    // 
    static
    {
        // Load native library
        System.loadLibrary("decoder");
    }

    // Private native methods
    private static native boolean initEngine(String mediaSource);
    private static native void releaseEngine();
    private static native void renderAudio();

Even if I create a buffer in place of try-catch block:

byte [] buffer = new byte[256];
track.write(buffer, 0, buffer.length);

I get the same result – NullPointerException.

Stack Trace:

java.lang.NullPointerException
at com.mautilus.audioplayer.Player$2.run(Player.java:99)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:19:42+00:00Added an answer on May 30, 2026 at 9:19 pm

    The error was in the type of one of the native methods – initEngine, it must not be declared as static – in this case.

    According to the documentation:

    http://docs.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html#wp16660

    The GetMethodID() causes an uninitialized class to be initialized. So if the library loading is not tied with any class instance (that’s normal):

    static
    {
        // Load native library
        System.loadLibrary("decoder");
    }
    

    then the native code:

    jmethodID writeToAudioTrackMethodID = NULL;
    jclass cls = (*env)->FindClass(env, "com.mautilus.audioplayer.Player");
    
    if (!cls)
    writeToAudioTrackMethodID = (*env)->GetMethodID(env, cls, "writeToAudioTrack", "([B)V");
    

    called from a static method – initEngine in my case, can’t obtain any existing instance of the class (even if such instance exists) and creates new one (with fields initialized to default values) – that’s it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to perform some logic in the table.modifiedField method
I have to perform some inserts into an Oracle DB. I have some dates
I'm trying to perform some offline maintenance (dev database restore from live backup) on
I want to perform some AJAX style data retrieval using the Java Play Framework
I want to perform some operation (Pause game) in my application when a call
I am using the get method to perform some operation like, approve, markasspam, delete,
I need to perform some action when my application receives focus. I've tried hooking
I want to perform some empirical trade-off's to assess the performance of applications written
I am trying to perform some unit testing on the iphone but for some
I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.