0

NOTICE: THIS QUESTION HAS BEEN RESOLVED DUE TO ME CATCHING MY OWN STUPIDITY

I want to add a custom class to a LinearLayout, but for some reason I keep getting a NullPointerException.

Here is the method that deals with the addition:

protected void onPostExecute(String results) {
        System.out.println("ON POST EXECUTE : " + results);
        try {
            if(!results.equals(((MessageBlurb)container.getChildAt(0)).getMessage())){
                try {
                    container.removeViewAt(30);
                    for (int i = 29; i > 0; i--) {
                        container.addView(container.getChildAt(i-1), i);
                        container.removeViewAt(i-1);
                    }
                    container.addView(new MessageBlurb(getApplicationContext(), results, Color.BLACK), 0);
                } catch (NullPointerException e) {
                    // TODO: handle exception
                }
            }
        }
        catch (Exception e) {
            MessageBlurb mb = new MessageBlurb(getApplicationContext(), results, Color.BLACK);
            mb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            System.out.println(mb);
            container.addView(mb, 0);
        }


    }

where MessageBlurb extends ViewGroup, because I have a TextView inside the MessageBlurb.

The MessageBlurb class looks like this:

public MessageBlurb(Context context, String message, int color){
    super(context);
    myTV = new TextView(context);
    this.addView(myTV);
    myTV.setText(message);
    System.out.println("THE BLURB IS CREATED");
    this.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            System.out.println("YOU CLIKED THE BLURB");

        }
    });

}

I printed out the description of mb, and it gives me a memory location. As well as that, the logcat error points to this line:

container.addView(mb, 0);

The container itself is a LinearLayout defined in the activity_main.xml file. It is initialized through the line of code:

container = (LinearLayout)findViewById(R.id.container);

The id of the Layout in the xml file is also called container

Can someone see what I'm doing wrong? Thanks!

6
  • If you're getting a null pointer exception at that line, that means container is null, so you need to show us where that's intialized. Otherwise clarify your post by posting the full stacktrace. Commented Dec 14, 2012 at 4:01
  • If this is in your Activity's onCreate override, you may not have called super.onCreate(...) at the beginning of this function. It's also possible that you're trying to do this in your Activity's constructor when no UI elements exist yet. Commented Dec 14, 2012 at 4:03
  • @gsingh2011 and Mickael I've edited my question. Commented Dec 14, 2012 at 5:28
  • 1
    Instead of posting snippets of code, post the full body of code causing the problem. If that code is too large, make a simple working example of it that we can test. I don't see anything wrong from what you've posted, so we can only speculate until seeing the entire code. Commented Dec 14, 2012 at 5:31
  • Are there any other indicators of error in LogCat apart from the specified line? Does it print out "THE BLURB IS CREATED"? Commented Dec 19, 2012 at 21:49

2 Answers 2

2
+50

I'm not 100% sure if this is where the problem is, but the following code looks like it's just asking for trouble:

                container.removeViewAt(30);
                for (int i = 29; i > 0; i--) {
                    container.addView(container.getChildAt(i-1), i);
                    container.removeViewAt(i-1);
                }
                container.addView(new MessageBlurb(getApplicationContext(), results, Color.BLACK), 0);

It looks like you are trying to remove the last view in the container and add a new MessageBlurb at the top. But you don't need to shift all the views down manually like that, simply adding a new view at position 0 will do all that for you. Try replacing it with this:

                container.removeViewAt(30);
                container.addView(new MessageBlurb(getApplicationContext(), results, Color.BLACK), 0);

Also, do you have a good reason for using getApplicationContext() when creating the message blurb? Your code looks like its part of an AsyncTask class nested inside an Activity class, so you can and should pass the activity itself as the context (i.e. new MessageBlurb(Activity.this, results, Color.BLACK) or something similar).

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. I didn't realize that addView with an index shifts everything down one.
+1 for suggesting a proper usage of app context and activity context.
0

It turns out that I was being a complete idiot. I looked through my onCreate() and found that I had commented out the line: setContentView(...)

Even so, I want to thank everyone who replied to this thread. Sorry for all the trouble! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.