Suhyung Park
Suhyung Park

Reputation: 3

nullpointerexception thread main exiting due to error

Well the error is:

 W/dalvikvm(320): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
 E/AndroidRuntime(320): Uncaught handler: thread main exiting due to uncaught exception
 E/AndroidRuntime(320): java.lang.NullPointerException
 E/AndroidRuntime(320):     at com.android.vosori.Amp$1.onCheckedChanged(Amp.java:58)
 E/AndroidRuntime(320):     at android.widget.CompoundButton.setChecked(CompoundButton.java:122)
E/AndroidRuntime(320):  at android.widget.ToggleButton.setChecked(ToggleButton.java:66)
 E/AndroidRuntime(320):     at android.widget.CompoundButton.toggle(CompoundButton.java:85)
 E/AndroidRuntime(320):     at android.widget.CompoundButton.performClick(CompoundButton.java:97)
 E/AndroidRuntime(320):     at android.view.View.onTouchEvent(View.java:4179)
 E/AndroidRuntime(320):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
 E/AndroidRuntime(320):     at android.view.View.dispatchTouchEvent(View.java:3709)      E/AndroidRuntime(320):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 E/AndroidRuntime(320):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 E/AndroidRuntime(320):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 E/AndroidRuntime(320):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
E/AndroidRuntime(320):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
E/AndroidRuntime(320):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
 E/AndroidRuntime(320):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
 E/AndroidRuntime(320):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
 E/AndroidRuntime(320):     at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(320):     at android.os.Looper.loop(Looper.java:123)
 E/AndroidRuntime(320):     at android.app.ActivityThread.main(ActivityThread.java:4363)
 E/AndroidRuntime(320):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(320):  at java.lang.reflect.Method.invoke(Method.java:521)
 E/AndroidRuntime(320):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 E/AndroidRuntime(320):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 E/AndroidRuntime(320):     at dalvik.system.NativeStart.main(Native Method)

and.. source is :

public class Amp extends Activity implements OnSeekBarChangeListener {

    WakeLock wL;
    public boolean tb;
    SeekBar volume;
    AudioManager am;
    MediaPlayer mp;
    AudioRecord arec;
    AudioTrack atrack;
    ToggleButton tbs;
    static final int buffersize = 200000;
    final short[] buffer = new short[buffersize];
    short[] readBuffer = new short[buffersize];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // wake-lock
        PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE);
        WakeLock wL = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");

        super.onCreate(savedInstanceState);
        wL.acquire();
        // fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
            android.os.Process
                    .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            int bufferSize = AudioRecord.getMinBufferSize(8000,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);

            arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize);
            atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                    AudioTrack.MODE_STREAM);
            atrack.setPlaybackRate(8000);
            byte[] buffer = new byte[bufferSize];


            while (tb) {
                arec.read(buffer, 0, bufferSize);
                atrack.write(readBuffer, 0, buffer.length);
            }

        tbs = (ToggleButton) findViewById(R.id.tbS);
        tbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    arec.startRecording();
                    atrack.play();
                } else {
                    arec.stop();
                    atrack.stop();

                }

            }
        });

        volume = (SeekBar) findViewById(R.id.sb1);
        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int maxV = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curV = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        volume.setMax(maxV);
        volume.setProgress(curV);
        volume.setOnSeekBarChangeListener(this);
    }

    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        // TODO Auto-generated method stub
        am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        am.setSpeakerphoneOn(true);

    }

    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }


}

Can you please help me? I'm stuck with this days, can't find way to fix this

updated Edit: sorry D: i'm not that good with this D: i probably have a mistake here also tried also to move those thing into oncheckedchanged method but didnt work :S

Upvotes: 0

Views: 659

Answers (1)

dymmeh
dymmeh

Reputation: 22306

You aren't initializing your arec and atrack variables until your method "run" is called. I don't see anywhere in your code that you are calling the method run() so those variables will never get initialized.

Move initializing those variables into the onCreate and that problem should go away

Upvotes: 1

Related Questions