This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass information from one activity to another

 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am trying to do an android app. I have a ListView that has names from Database (that's working fine) but when i click in one of the names i wanted to send the name with additional information to another activity. I've been trying different things but i can't find a way to make this work.

First Activity:




Second Activity:



I can just pass the name but when i want to pass the email too the program crashes.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the log files? They should provide details about the cause of the crash. If you use an IDE then there should be a LogCat view (in Eclipse it is part of the DDMS perspective), and if you are not using and IDE then LogCat is available as a command line tool part of the ADT.
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it says in the beggining java.lan.nullpointerexception, i've tried so many things but i cant seem to understand why
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line has the null pointer exception? In which class?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace of the exception should tell you exactly in which line of code the problem happened.
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is when i try to send the information from the first activity to the second.
I'm almost sure that is the ArrayList that is wrong and i dont know what to change.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben Matt wrote:The problem is when i try to send the information from the first activity to the second.
I'm almost sure that is the ArrayList that is wrong and i dont know what to change.


Unfortunately 'almost sure' isn't really helpful. Like Ulf said, the stack trace in LogCat should tell you exactly what line the exception is at. That will tell us which objects are in use, then you know which one to track down why it has no value. You could be correct in that the problem is the ArrayList - I don't see its purpose. But, on the other hand, that could be a red-herring. The stack trace will tell you where the problem actually manifests.
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace shows this.



You said you dont see the purpose of the ArrayList. Could you tell me another way to pass the information from the database to te other activity (TextViews)??
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So: In line 101 of the MainActivity class some object is unexpectedly null. Which line of code is that? Which objects are being used there?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben Matt wrote:The stack trace shows this.
... at com.exaple.listview.MainActivity$2$1.onItemClick(MainActivity.java:101)

What is line 101 in the MainActivity? It is in the onItemClick() method somewhere.

You said you dont see the purpose of the ArrayList. Could you tell me another way to pass the information from the database to te other activity (TextViews)??


Here is the pertinent code:

In English that would be:
"1) Create a new empty list
2) Add one thing to it
3) Get one thing from it"

If the list only holds one thing, and you immediately pull it out of the list... why do you put the item in the list to begin with? Why not directly assign it to the email variable? Also, the code as posted is an IndexOutOfBoundsException waiting to happen. The List will only ever have one value, but your ListView might have more so the position parameter may be greater than the number of items in emailLists.. If you click on anything but the first email then you will be attempting to get something from the list beyond the List's size.

But none of that would give you a NullPointerException, so it is a side topic - a problem you have to fix when you are done fixing your NPE.
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote
What is line 101 in the MainActivity? It is in the onItemClick() method somewhere.
Yeah it's on the onItemClick method.
I did the List because i was just trying the email, then i wanted to pass also the id and age of the person.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben Matt wrote:
Steve Luke wrote
What is line 101 in the MainActivity? It is in the onItemClick() method somewhere.
Yeah it's on the onItemClick method.

Which specific line of code?
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one:
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben Matt wrote:This one:


Now we are getting somewhere. There is only one reason for a NullPointerException: A variable being used hasn't been assigned a value yet. You use three Objects in that line of code:
emailLists, cursor, and the String "email". The String is hardcoded, so not null. The emailLists variable was assigned an object in the previous line of code, so it isn't null either. The only one left is cursor.

Your cursor variable has no Object reference, so you can't call methods on it. You have to go back to how the cursor is created, when it is assigned a value, and how it is used and figure out why it is null at this point. Then you have to fix it so it gets a value.
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... Thank you for the help, at least i know where to start with now, ill see what can i do
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i got it , thank you so much for you help, i don't think i would have remenbered any of this.

Here's the code:


Now i can just add the other information i want to pass in the While right??
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that will fix the null pointer problem, and now it is clear why you need the ArrayList. There is an efficiency problem though. You are getting all the rows of data, when you really only want one specific person. You would be better off just getting the one row of info you need. And this is where a bit of SQL knowledge might come in. For example, you might be able to do this:


This should be more efficient because you aren't getting a bunch of data you don't need, and then have to cycle through it all to fill your list(s). The other problem with the approach you had was it relied on the details coming back in the same order as the names in the list. This probably isn't guaranteed unless you use an ORDER BY clause both when getting the names and the details.
 
Ruben Matt
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, you're right, i was getting a lot of information when i only need the details of one person. Thanks for the help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic