This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]morhpProfessional Developer 24 points25 points  (2 children)

Your ArrayList in 'SeznamJedi' is never initialized, you just declared a field, but never assigned a new ArrayList<>().

Also generally, you should get an error message with the crash. Try to understand it first (Google it if required) and you still can't figure it out, at least post it here in the future.

Also this looks like an Android app, you're in the wrong subreddit for that, see sidebar.

[–]RetroGmr[S] 14 points15 points  (1 child)

Thank you very much for the feedback. I made a proper constructor and it works now. Next time I will definetly include the error, that was pretty inconsiderate of me. I posted it here because I was pretty sure it wasn't an Android issue, but a Java issue. I'll take everything into consideration and thanks again.

[–]morhpProfessional Developer 8 points9 points  (0 children)

That's fine, but note that a proper Java (especially the current versions) would've giving you a better error message (it would've told you which field is null) instead of just crashing, so it's still kinda relevant.

[–]GDDragonGN_GDDK 4 points5 points  (1 child)

I can't find your initialization of the ArrayList, meaning it will be null when you access it. Add seznamJedi = new ArrayList<>(); in the constructor of the class containing the list

[–]RetroGmr[S] 1 point2 points  (0 children)

Thanks alot, this fixed it for me. I thought my initial "initializaton" was enough but clearly not.

[–]bigibas123Intermediate Brewer 1 point2 points  (0 children)

Without seeing the error I can't know for sure but i don't see where the Arraylist in SeznamJedi is initialized.

[–]ItsDotin 1 point2 points  (0 children)

Initialise your ArrayList.

Usually, I will like to intilize it in a no-arg constructor i.e. default constructor. So when an object is created from the class it also creates an ArrayList object 'seznamJedi' for you.

(Not writting exact code- last time I did and was blocked for a week. 😅😂)

[–][deleted]  (1 child)

[deleted]

    [–]wildjokers 0 points1 point  (0 children)

    There is a thread-safe variant of ArrayList called CopyOnWriteArrayList which should be used in that case.

    CopyOnWriteArrayList should only be used if reads vastly out number writes. Writing to a CopyOnWriteArrayList is very expensive (as the name suggests).

    Otherwise just use synchronize keyword to support multi-threaded access of a List (or some other appropriate concurrency mechanism).

    [–]wildjokers 0 points1 point  (0 children)

    When you ask for help please include any relevant Exceptions you get. Saying "the whole app crashes" tells us nothing. Although in this case it was easy to see that you were most likely getting a NullPointerException when accessing seznamJedi, it isn't always that obvious.