• Post Reply Bookmark Topic Watch Topic
  • New Topic

[Solved] Clearing arraylist of lists crashes program  RSS feed

 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning everyone,

I am just starting out in android programming and been working on a dynamic 2d array. After some searching and some swearing, I got a working bit to add what I want. However when I go to clear the list, the program hangs and then crashes. Below is the relevant code. So far I have tried .clear, cycling thru the list and removing the items individually, reinitiating the arraylist variable and all have crashed the program. It is almost like it doesn't want to remove the list from the array. The new array being added is a string description and a string decimal. All help is greatly appreciated!

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to track down where exactly the crash happens. Run the application in an emulator, or attach the phone to your computer will running, and use LogCat to track what is going on. Once you get the error message, find the first line in the stack trace that originates in your code (this can be pretty deep in android stack traces) and see what that line does.

Without knowing what your code is or where the error is, all you (or I) can do is guess, which is never productive. But the most likely problem is that you have some location that is trying to use the list or using a value in the array and is either getting a null pointer or an index out of bounds. You need to make sure said location gets updated with the list's current state so it doesn't try to access data that doesn't exist. A very likely candidate for this would be an Adapter used in a ListView, GridView, or some similar view. If so, make sure you notify the adapter that things have changed. Another likely problem is event handlers where you pass specific indexes for the data to work on, but when the event is called those indexes are gone. you should either pass the data, not the index, or check if the index exists and fail gracefully if it doesn't in this case.

But that is speculation. You need to read the logs and the code to see exactly what is happening.
 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The function fnClear is associated with a button's onClick (just in the xml, no onclicklisteners). There isn't anything else using the arraylist, all of the code you see is all of the code that involves the arrItems. The crash happens when I press the button that calls the function fnClear.

Will take a look at data logging and see if I can get that implemented.
 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reviewed the logcat. I think I narrowed it down. Pretty much it is the only thing that looks like it is cycling thru something in the errors. However none of it makes any sense to me. Below is an excerpt of what I think is the relevant data.

Any ideas?

11-16 09:41:49.854 E/overlay (198): Failed to call ioctl MSMFB_OVERLAY_SET err=Operation not permitted
11-16 09:41:49.854 E/overlay (198): MdpCtrl failed to setOverlay, restoring last known good ov info
11-16 09:41:49.854 E/overlay (198): == Bad OVInfo is: mdp_overlay z=1 fg=0 alpha=255 mask=-1 flags=0x860000 id=-1
11-16 09:41:49.854 E/overlay (198): src msmfb_img w=736 h=1280 format=10 MDP_Y_CRCB_H1V2
11-16 09:41:49.854 E/overlay (198): src_rect mdp_rect x=0 y=50 w=720 h=1230
11-16 09:41:49.854 E/overlay (198): dst_rect mdp_rect x=0 y=50 w=720 h=1230
11-16 09:41:49.854 E/overlay (198): user_data user_data cnt=8
11-16 09:41:49.854 E/overlay (198): i=0 val=0
11-16 09:41:49.854 E/overlay (198): i=1 val=0
11-16 09:41:49.854 E/overlay (198): i=2 val=0
11-16 09:41:49.854 E/overlay (198): i=3 val=0
11-16 09:41:49.854 E/overlay (198): i=4 val=0
11-16 09:41:49.854 E/overlay (198): i=5 val=0
11-16 09:41:49.854 E/overlay (198): i=6 val=0
11-16 09:41:49.854 E/overlay (198): i=7 val=0
11-16 09:41:49.854 E/overlay (198): == Last good known OVInfo is: mdp_overlay z=0 fg=0 alpha=0 mask=0 flags=0x0 id=-1
11-16 09:41:49.854 E/overlay (198): src msmfb_img w=0 h=0 format=0 MDP_RGB_565
11-16 09:41:49.854 E/overlay (198): src_rect mdp_rect x=0 y=0 w=0 h=0
11-16 09:41:49.854 E/overlay (198): dst_rect mdp_rect x=0 y=0 w=0 h=0
11-16 09:41:49.854 E/overlay (198): user_data user_data cnt=8
11-16 09:41:49.854 E/overlay (198): i=0 val=0
11-16 09:41:49.854 E/overlay (198): i=1 val=0
11-16 09:41:49.854 E/overlay (198): i=2 val=0
11-16 09:41:49.854 E/overlay (198): i=3 val=0
11-16 09:41:49.854 E/overlay (198): i=4 val=0
11-16 09:41:49.854 E/overlay (198): i=5 val=0
11-16 09:41:49.854 E/overlay (198): i=6 val=0
11-16 09:41:49.854 E/overlay (198): i=7 val=0
11-16 09:41:49.854 E/overlay (198): MdpCtrl Lkgo ov has id -1, will not restore
11-16 09:41:49.854 E/overlay (198): Ctrl commit failed set overlay
11-16 09:41:49.854 E/overlay (198): OverlayImpl p0 failed to commit
11-16 09:41:49.854 E/overlay (198): Overlay commit failed
11-16 09:41:52.857 E/overlay (198): Failed to call ioctl MSMFB_OVERLAY_SET err=Operation not permitted
11-16 09:41:52.857 E/overlay (198): MdpCtrl failed to setOverlay, restoring last known good ov info
11-16 09:41:52.857 E/overlay (198): == Bad OVInfo is: mdp_overlay z=0 fg=1 alpha=255 mask=-1 flags=0x840000 id=-1
11-16 09:41:52.857 E/overlay (198): src msmfb_img w=736 h=1280 format=10 MDP_Y_CRCB_H1V2
11-16 09:41:52.857 E/overlay (198): src_rect mdp_rect x=0 y=50 w=720 h=1230
11-16 09:41:52.857 E/overlay (198): dst_rect mdp_rect x=0 y=50 w=720 h=1230
11-16 09:41:52.857 E/overlay (198): user_data user_data cnt=8
11-16 09:41:52.857 E/overlay (198): i=0 val=0
11-16 09:41:52.857 E/overlay (198): i=1 val=0
.....
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bulsatar monkey wrote:The function fnClear is associated with a button's onClick (just in the xml, no onclicklisteners). There isn't anything else using the arraylist, all of the code you see is all of the code that involves the arrItems. The crash happens when I press the button that calls the function fnClear.

Then there's plainly something you're missing from what you've showed us because, under normal circumstances, arrItems.clear() should work just fine and be all you need.

I think you need to describe what you're doing in more detail and provide all relevant code (please don't just dump everything you've written).

Also: please DontWriteLongLines; it makes your post very hard to follow. I've broken yours up this time.

Winston
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bulsatar monkey wrote:I reviewed the logcat. I think I narrowed it down. Pretty much it is the only thing that looks like it is cycling thru something in the errors. However none of it makes any sense to me. Below is an excerpt of what I think is the relevant data.

Nope, none of that is relevant. There will be exceptions, and you need to track them down. LogCat can filter based on application name, and entry level, try to remove the spam from other, non-meaningful applications using filters.

If you can't figure out how to use LogCat to isolate the error start adding break points at and around every line of code where the array may be references, used, or methods which use it might be called.
 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't think my code was long. The suggestions said 80 characters in 1 line and my code doesn't come anywhere close to that. If you are talking about the logcat that I posted, I apologize and I will do it up as an attachment next time.

Here is my java in it's entirety for the first project I am trying to do in android. it is a simple budget program where users can add costs (just a description and a monetary value) to a list (will eventually be displayed in a ListView but not there yet) and will calculate how much of my paycheck is left. Right now, I am just focusing on the basics of adding an entry (completed), totalling the entries (completed) and deleting all the entries (where my problem lies). The zip attached is the xml and the java. There isn't even 100 lines total in the java as I am in the very early stages and just trying to get a handle on how java works but as you can see, the lines I posted first are all of the lines that reference the array.

~~ok..I tried to upload .txt, .zip and .rtf but apparently this site doesn't like anything uploaded...here is the java minus 1 function that doesn't touch the array. I have no idea what is important or not so apologies if this isn't what you were asking for.


 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is AddEntry() (used on line 44 of the posted code)?

Line 45 is an error: if (rtn=false) This is an assignment inside the if statement. This is legal, it will make the rtn variable false, but also the result of the expression is false, so the else clause will always be executed. That should read if(!rtn) which means "if not rtn" or "if rtn is false." That way it is impossible to make this sort of error (you should never compare booleans with == because this error is too easy to make).

How does fnAddCost() get called?

How does fnClear() get called?
 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The AddEntry is the function that anchors down the description and cost. fnAddCost and fnClear are called by an onClick in the xml, they are buttons. Will take a look at the boolean and see if that fixes things. I am not getting any errors when it is compiled (both in eclipse and I use AIDE on my phone so it is easier to check and run) Here is the AddEntry:



~~editing the boolean to !rtn didn't do anything. Everything still works the same (can add items and it totals everything correctly) but it crashes as soon as I call the fnClear function by pressing the button.

here is the xml of the button that calls the fnClear function. It is setup like the button that calls fnAddCost function and it works...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bulsatar monkey wrote:The AddEntry is the function that anchors down the description and cost. fnAddCost and fnClear are called by an onClick in the xml, they are buttons.


Ahh, now I see it! Look at your fnAddCost() method, which does get called. Now look at the fnClear() method. Let me tell you what the error message will be something to the effect of: "Method fnClear(...) in class com.example.simplebudget.MainActivity can not be found. Do you see the big difference between the two onClick methods? You don't need to look at the code bodies, just the signature.

I am not getting any errors when it is compiled

Knowing what the answer is now, I am a bit concerned - I would have hoped there would be an error message in the XML file. But I guess that is one of the dangers of using XML to configure the method name (another being the method must be present in the Activity, a problem if you use Fragments). Personally I never use that XML attribute, I get the view from the inflated display and set it in code. That is something you may want to pick up later when your apps get more complex.


Ack. You are using a method to call and set instance variables so you can then use those instance variables almost immediately in another method. A suggestion:
1) Make a class to hold the data, rather than a String array. This will help because the cost isn't a String anyway, so you can store the data in a meaningful form without having to convert it for calculations.
- example:

2) It also lets you create a new Instance of an Object to store the data in and pass that instance as the return value of the method. So that is what you should do. Get rid of the instance values, create a new BudgetItem in your addEntry() method, and return that. You can send a special value for 'false' if the proper parameters aren't set. For example, return null, or return a specific BudgetItem which represents a NullObject.
3) Add that BudgetItem to the list, not a StringArray

Note, these are just guidelines for better programming style. The problem with using instance variables is that they get really hard to work with safely when the display rotates, the button is pushed multiple times, or threading comes into play. Start with good practices now and your life gets easier down the road.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bulsatar monkey wrote:

Also, the line 8: if (... && sDesc != "") ... that equality operator doesn't do quite what you think it does. You should AvoidTheEqualityOperator (<- link) and always use .equals(). if (sDesc != null && !sDesc.equals(""))

~~editing the boolean to !rtn didn't do anything. Everything still works the same (can add items and it totals everything correctly) but it crashes as soon as I call the fnClear function by pressing the button.

The difference would be when the AddEntry() method returned false... in that case you would have expected the values of strDesc and strCost not to be added to the list, and the total to not be re-calculated. But they would have been.
 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, understood about 80% which is enough for me to start with your suggestions. Will work on building the class and such and will look up what I am not familiar with.

Thanks for the help!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see the problem? It will be a real simple fix and should get you running right away.
 
bulsatar monkey
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't include the (View view). I did now that you told me to relook at it. I changed it and yerp, it worked fine. Will still take your advice and will be doing some rewriting.

Thanks again for your help! much appreciated.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, I am glad you were able to find the problem, and even happier that you posted back the solution so others can see it For that, have a cow!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!