-1

I have been following an example I found and when doing the simplest thing as setting text in Textview it gives me an error which I will display below. The most usual problem is that the textview is assigned before the view is inflated but that is not the case here and changing, trying different layouts doesn't work either. I believe there is a simple thing I'm missing here but could anyone tell me what it is.

public class MainActivity extends AppCompatActivity {

TextView pitchText, noteText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AudioDispatcher dispatcher =
            AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);

    pitchText = (TextView) findViewById(R.id.pitchText);
    pitchText = (TextView) findViewById(R.id.noteText);

    PitchDetectionHandler pdh = new PitchDetectionHandler() {
        @Override
        public void handlePitch(PitchDetectionResult res, AudioEvent e){
            final float pitchInHz = res.getPitch();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    processPitch(pitchInHz);
                }
            });
        }
    };
    AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(pitchProcessor);

    Thread audioThread = new Thread(dispatcher, "Audio Thread");
    audioThread.start();

}

public void processPitch(float pitchInHz) {

    pitchText.setText("" + pitchInHz);

    if(pitchInHz >= 110 && pitchInHz < 123.47) {
        //A
        noteText.setText("A");
    }
    else if(pitchInHz >= 123.47 && pitchInHz < 130.81) {
        //B
        noteText.setText("B");
    }
    else if(pitchInHz >= 130.81 && pitchInHz < 146.83) {
        //C
        noteText.setText("C");
    }
    else if(pitchInHz >= 146.83 && pitchInHz < 164.81) {
        //D
        noteText.setText("D");
    }
    else if(pitchInHz >= 164.81 && pitchInHz <= 174.61) {
        //E
        noteText.setText("E");
    }
    else if(pitchInHz >= 174.61 && pitchInHz < 185) {
        //F
        noteText.setText("F");
    }
    else if(pitchInHz >= 185 && pitchInHz < 196) {
        //G
        noteText.setText("G");
    }
}

Error code:

03-05 15:12:38.297 7642-7642/com.example.richard.jtransformtest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.richard.jtransformtest, PID: 7642
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at com.example.richard.jtransformtest.MainActivity.processPitch(MainActivity.java:62)
                                                                                  at com.example.richard.jtransformtest.MainActivity$1$1.run(MainActivity.java:43)
                                                                                  at android.os.Handler.handleCallback(Handler.java:836)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                  at android.os.Looper.loop(Looper.java:203)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6251)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

And finally the XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/pitchText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/noteText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />
</LinearLayout>

Any help would be really welcome.

Sorry... I was Really stupid and didnt notice that there is a duplicate.. thank you all for answers

Richard
  • 1,087
  • 18
  • 52

3 Answers3

1

Your noteText is null because You just forgot to do correct findViewById in your noteText TextView check it

Use this

pitchText = (TextView) findViewById(R.id.pitchText);
noteText = (TextView) findViewById(R.id.noteText);

Insetad of this

pitchText = (TextView) findViewById(R.id.pitchText);
pitchText = (TextView) findViewById(R.id.noteText);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1
pitchText = (TextView) findViewById(R.id.pitchText);
pitchText = (TextView) findViewById(R.id.noteText);

Your noteText is null because you're not assigning it.

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
1

you have to change this :

 pitchText = (TextView) findViewById(R.id.pitchText);
 pitchText = (TextView) findViewById(R.id.noteText);

like following :

 pitchText = (TextView) findViewById(R.id.pitchText);
 noteText= (TextView) findViewById(R.id.noteText);
Aj 27
  • 2,316
  • 21
  • 29