0

Before I get started explaining what is going on, I just wanted to say that I have read a similar question but none of the answers on that were able to solve my problem. My app still crashes when I use the methods discussed in the previous question.

I have a text file where each line has an acronym, term, and definition separated by a percent sign. When a user searches for a specific acronym or term, it splits the acronym, term and definition by the percent sign and displays each index of the array on the screen. I want to only display the acronym in the listview which is always myArray[0]. I will figure out the clicking part on my own but right now I'm just having an issue getting the acronyms to display. In a previous answer, at the end, the user said to put: ArrayAdapter adapter = new ArrayAdapter(this,R.id.yourListView,lines) but this did caused an error for me as the middle thing should be a layout. Here is my code: **Note: the piece of code that is giving me an error is surrounded by stars.

package com.redacted.CSRenA.acronymlookup;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Browse extends AppCompatActivity {
    ListView listView= (ListView) findViewById(R.id.acrolist);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        try{


            InputStream is = this.getResources().openRawResource(R.raw.acronyms2);
            InputStreamReader inputreader = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));


            ArrayList<String> lines = new ArrayList<String>();
            boolean hasNextLine =true;
            String[] str;
            while (hasNextLine){
                String line =  reader.readLine();
                str = line.split("%", 5);
                lines.add(str[0]);
                hasNextLine = line != null;
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,**R.id.acrolist**,lines);

            listView.setAdapter(adapter);

            is.close();

        }
        catch(java.io.FileNotFoundException e){

        }catch(java.io.IOException e){

        }
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

Logs (Scroll right):

07-12 15:57:23.804 13752-13752/com.nesassociates.laurenanderson.acronymlookup E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                Process: com.nesassociates.laurenanderson.acronymlookup, PID: 13752
                                                                                                java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nesassociates.laurenanderson.acronymlookup/com.nesassociates.laurenanderson.acronymlookup.Browse}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
                                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                                    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)
                                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                                    at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                                                    at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
                                                                                                    at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                                                    at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                                                    at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
                                                                                                    at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
                                                                                                    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
                                                                                                    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                                                    at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
                                                                                                    at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
                                                                                                    at com.nesassociates.laurenanderson.acronymlookup.Browse.<init>(Browse.java:18)
                                                                                                    at java.lang.Class.newInstance(Native Method)
                                                                                                    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                                    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) 
CSRenA
  • 147
  • 1
  • 8

1 Answers1

0

You're getting a NullPointerException trying to dereference listView.

In order for findViewById to work, a content view needs to have been set for your Activity first.

This line:

ListView listView= (ListView) findViewById(R.id.acrolist);

Must be done after you've set your content view. Otherwise, it will always return null.

Simply move it to onCreate just below where you call setContentView.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
  • Thank you for your answer. I went ahead and did that but I'm still getting an issue with ArrayAdapter adapter = new ArrayAdapter(this,R.id.acrolist,lines); It still says: expected resource of type layout. does acrolist not count as a layout because it's a listview in my XML? and if so, how do I fix this? – CSRenA Jul 12 '17 at 20:22
  • When you create an ArrayAdapter, the second param should be a resource id of a layout containing a TextView that the adapter will use to render each item in the list. Android provides some default layouts for this purpose. Try doing ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,lines); instead. – Michael Krause Jul 12 '17 at 20:28
  • The only issue with that is that I don't have any list items currently, I was relying on the code above that to fill the list for me. Will I need text views in order to do this? – CSRenA Jul 12 '17 at 20:30
  • Each entry in your lines List will end up being an item in your ListView. Just try what I suggested and see if it works. – Michael Krause Jul 12 '17 at 20:32
  • I was misunderstanding what simple_list_item was and thought that it was a list item that I had to input. I looked it up and figured out what you meant. Unfortunately it is still crashing. – CSRenA Jul 12 '17 at 20:39