0

So I am making a simple app. It's just basically making a list of win with a custom list view at the end.

It starts off on the main screen where there are two buttons, one is an "Add" button which takes you to the Add activity. If you push this it'll take you to a page where you type in the name,price, description of the wine and then hit a submit button to the list. The other button on the main screen is a "Go to List" button which just takes you directly to the list activity.

If I go through the Add button, add a wine, and then go to the list, it works fine. I can see the list. It even works if I don't add anything to the list. I can see the empty list activity.

When I push the "Go to List" button on the main screen though, it crashes and says "The application has stopped".

I don't get why I can go through the Add button to get to the list fine, but this button doesn't work at all.

Could I get some help?

Here are the three relevant activities, the AddActivity, the ListActivity, and the MainActivity.

AddActivity:

public class AddActivity extends AppCompatActivity {


EditText editWineName;
EditText editWinePrice;
EditText editWineDescription;
Button btnSubmit;
Button btnGoToList;
String stringWineName;
String stringWinePrice;
String stringWineDescription;
ArrayList<String> listWineName = new ArrayList<>();
ArrayList<String> listPrice = new ArrayList<>();
ArrayList<String> listWineDescription = new ArrayList<>();

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



    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setVariables();
            listWineName.add(stringWineName);
            listPrice.add(stringWinePrice);
            listWineDescription.add(stringWineDescription);
            Toast.makeText(AddActivity.this, stringWineName + " was added to the list.", Toast.LENGTH_SHORT).show();
            clearEditText();

        }
    });

    btnGoToList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentGoToList = new Intent(AddActivity.this,ListActivity.class);
            intentGoToList.putStringArrayListExtra("WINENAME", listWineName);
            intentGoToList.putStringArrayListExtra("WINEPRICE", listPrice);
            intentGoToList.putStringArrayListExtra("WINEDESCRIPTION", listWineDescription);
            startActivity(intentGoToList);
        }
    });

}

private void setVariables(){
    editWineName = (EditText) findViewById(R.id.editName);
    editWinePrice = (EditText) findViewById(R.id.editPrice);
    editWineDescription = (EditText) findViewById(R.id.editDescription);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnGoToList = (Button) findViewById(R.id.btnGoToList);
    stringWineName = editWineName.getText().toString();
    stringWinePrice = "$" + editWinePrice.getText().toString();
    stringWineDescription = editWineDescription.getText().toString();
}

private void clearEditText() {
    editWineName.getText().clear();
    editWinePrice.getText().clear();
    editWineDescription.getText().clear();
}
}

ListActivity:

public class ListActivity extends AppCompatActivity {

ListView wineList;
ArrayAdapter adapter;
Button btnBacktoMain;

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



    ArrayList<String> listWineName = getIntent().getStringArrayListExtra("WINENAME");
    ArrayList<String > listWinePrice = getIntent().getStringArrayListExtra("WINEPRICE");
    ArrayList<String> listWineDescription = getIntent().getStringArrayListExtra("WINEDESCRIPTION");

    adapter = new CustomAdapter(this, listWineName, listWinePrice, listWineDescription);
    wineList.setAdapter(adapter);


    btnBacktoMain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentBackToMain = new Intent(ListActivity.this,MainActivity.class);
            startActivity(intentBackToMain);
        }
    });

}

private void setVariables (){
    btnBacktoMain = (Button) findViewById(R.id.btnBackToMain);
    wineList = (ListView) findViewById(R.id.listWine);

}

}

MainActivity:

public class MainActivity extends AppCompatActivity {

Button btnAdd;
Button btnList;

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

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {                                                //Goes to the add activity
            Intent intentAdd = new Intent(MainActivity.this, AddActivity.class);
            startActivity(intentAdd);
        }
    });

    btnList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {                                              //Goes to the list activity
            Intent intentList = new Intent(MainActivity.this, ListActivity.class);
            startActivity(intentList);
        }
    });
}

private void setVariables(){
    btnAdd = (Button) findViewById(R.id.btnAddWine);
    btnList = (Button) findViewById(R.id.btnViewList);
}

}

Enzokie
  • 7,365
  • 6
  • 33
  • 39
  • Hey - when your app crashes, you need to do two things: first, check the crash logs which show the type of error and stacktrace pinpointing where it happens. If that's doesn't clearly tell you the problem, use your debugger to step through and inspect your code. https://developer.android.com/studio/debug/index.html. If you still can't figure it out, please help us out and post the stacktrace, otherwise we're just guessing about what could have caused your problem. – dominicoder Aug 29 '17 at 00:52
  • Sorry about that. Here is the error message I'm getting.https://pastebin.com/7ygPNsqm – jeremytodd1 Aug 29 '17 at 01:02

1 Answers1

0
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344)
    at android.widget.ListView.setAdapter(ListView.java:493)
    at com.example.jeremy.mywine.ListActivity.onCreate(ListActivity.java:33)

Your crash indicates that the data in the adapter given to the ListView in ListActivity is null. Make it not null. Start at ListActivity.java at line 33 and go backwards to find where you are (or this case are not) initializing the data in the list adapter.

In you case, you are expecting data in your intent. OK, where is your intent set up? In your MainActivity click. Well, there you just launch the activity without passing any data in the intent extras, hence there is nothing to pull out from the intent in ListActivity, hence your crash.

So you need to initialize the data in MainActivity and set it as extras in the Intent you use to launch ListActivity.

Since the ListActivity is expecting this:

ArrayList<String> listWineName = getIntent().getStringArrayListExtra("WINENAME");
ArrayList<String > listWinePrice = getIntent().getStringArrayListExtra("WINEPRICE");
ArrayList<String> listWineDescription = getIntent().getStringArrayListExtra("WINEDESCRIPTION");

You would update your MainActivity to do something like this (where getDescriptions() is a fictitious method you would create to return a list of strings)

btnList.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //Goes to the list activity
        Intent intentList = new Intent(MainActivity.this, ListActivity.class);
        intentList.putExtra("WINENAME", new ArrayList<String>());     // Empty list
        intentList.putExtra("WINEPRICE", Arrays.asList("Foo", "Bar")); // Explicit list named items
        intentList.putExtra("WINEDESCRIPTION", getDescriptions());    // Get list from private method
        startActivity(intentList);
    }
});

Also check this post my be useful for learning how to read and understand a crash log.

And check the documentation on how to start activities.

Hope that helps!

dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • I appreciate the help. I just don't understand it though. The button in the MainActivity is just simply a button that goes to the ListActivity. I'm not trying to pass any extras or anything. Why do I need to pass any extras from the MainActivity? The only thing on MainActivity are two buttons. – jeremytodd1 Aug 29 '17 at 01:46
  • That's precisely your problem. The list activity is expecting three lists of strings (names, prices, and descriptions) but you're not sending them. So boom goes the dynamite. Where do you expect `listWineName` and the other two to come from? – dominicoder Aug 29 '17 at 01:50
  • So I now get that the "ArrayList" lines in the ListActivity are requiring that there be data when the intent is pulled up. My question now is how do I insert empty data? I tried null and I'm not getting it. I'm sorry with the pretty nooby questions. I'm really new at this stuff yet. https://pastebin.com/ydLZAGep – jeremytodd1 Aug 29 '17 at 01:57
  • Ooh. I just fixed it actually using an if/else statement. Basically I said that if the arraylist items are null, to not do anything, but if someone is in those items then do the command. I got it working. Not sure if it's a good fix but it's a fix. Here is the new ListActivity: https://pastebin.com/XJg1Udcm It's the only difference I made. No changes were made to any of the other activities. – jeremytodd1 Aug 29 '17 at 02:07
  • Your update will prevent a crash, sure, but renders your activity fairly useless =P - See my updated answer. – dominicoder Aug 29 '17 at 02:16
  • I'll work on putting in that new code. My questions is though is how did the if/else statement make the activity useless? It seems to be working fine for me yet. When I start up the app I can hit the Go to List button and it pulls up the empty list. When I add items to the list through the Add button, they pull up in the list just fine. – jeremytodd1 Aug 29 '17 at 02:28
  • So here is my new MainActivity with your code. I removed my if/else statement "fix" I did. Is something like this what you were thinking? https://pastebin.com/KFyHVWWY – jeremytodd1 Aug 29 '17 at 02:36
  • Ah - you have ways of adding things to the list from the activity itself. Cool. I was being sassy because I thought your ... um ... _intent_ ... was to start the activity with an initial data set. If that's optional, then your if check is good enough. – dominicoder Aug 29 '17 at 02:39
  • Well that makes me feel better now that I know my if/else fixed it. I still am at the state where I'm doubting everything I do with this stuff because I just don't know enough yet. And yeah, I'm sure I'm doing things in a very weird way compared to what an experienced person would do. I'm just following random tutorials I've been finding to help me through things. But yeah, man, I can't thank you enough for taking the time to help me with this. I feel like I'm in over my heard but people like you make me feel like I can get through this. – jeremytodd1 Aug 29 '17 at 02:43
  • No problem - we're all at that stage at some point :) Just keep at it - practice and experience are the best teachers. And if you found the answer helpful, please accept it :) – dominicoder Aug 29 '17 at 02:53