0

I am fairly new to Java, and even newer to coding within Android Studio. I'm trying to read a basic .txt file located in the Assets folder of my practice project. My main goal is to grab a random line from the file after pressing a Button and to have that line printed in the TextView of my MainActivity. However, whenever I run the app on my phone via USB, the app crashes (after the button is pressed).

I have included a snippet of the code I have so far(MainActivity Code), as well as a shot of the Logcat showing the error code (Logcat).

public class MainActivity extends AppCompatActivity {

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

    Button btn1=findViewById(R.id.buttonShowText);
    btn1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            try {
                AssetManager as=getResources().getAssets();
                InputStream is=as.open("insult1.txt");
                BufferedReader br1=new BufferedReader(new InputStreamReader(is));
                StringBuilder out=new StringBuilder();
                String line1;
                while((line1=br1.readLine())!=null){
                    out.append(line1);
                }
                String dissFinal=out.toString();
                br1.close();

                TextView text1=findViewById(R.id.mainTextView);
                text1.setText(dissFinal);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Exception Reached");

}

02-21 14:39:08.076 30321-30321/com.asmodeus.practice E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.asmodeus.practice, PID: 30321
                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                       at com.asmodeus.practice.MainActivity$1.onClick(MainActivity.java:47)
                                                                       at android.view.View.performClick(View.java:5637)
                                                                       at android.view.View$PerformClick.run(View.java:22434)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Again, I am relatively new, so take it easy on me! Thanks :)

1 Answers1

-1

You need add TextView with id: mainTextView in activity_main layout

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
  • Wow, I realized that I changed my **TextView** id to **textViewTitle**. After changing that, everything appeared on the screen after the button press. Thanks for the help! – jagged_prospect Feb 21 '18 at 19:54