0

I am trying to create bunch buttons for a android program, but I kept getting NullPointerException when I try to "connect" the buttons I created in the XML file to the buttons I created in the Java file. I been trying to figure out this error, was wondering if any of you would be able to shed some light

public class AddActivity extends ActionBarActivity {
    private ExerciseLoader loader = new ExerciseLoader();
    private ArrayList<Button> listofButton = new ArrayList<Button>();
    private Button button=new Button(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add);
        Intent intent = getIntent();
        createButtons();
        textButtons();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void createButtons() {
        for (int i = 0; i < loader.getList().size(); i++) {
            String buttonID = "btn" + i;
            int resID = getResources().getIdentifier(buttonID, "id",
                    getPackageName());
            listofButton.add(((Button) findViewById(resID)));
            // listofButton.get(i).setText("This is a test");
        }
    }
}

Here is the error code

java.lang.RuntimeException: Unable to instantiate activity
    ComponentInfo{com.example.alert/com.***.alert.AddActivity}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'android.content.res.Resources android.content.Context.getResources()'
    on a null object reference
9
  • some where you are calling getResources() method using context which is null. to find issue add full log with question Commented Feb 9, 2015 at 4:28
  • 2
    possible duplicate of NullPointerException in ContextWrapper.getResources() Commented Feb 9, 2015 at 4:29
  • @ρяσѕρєяK : In createButtons() OP has int resID = getResources().getIdentifier(buttonID, "id", getPackageName()); Commented Feb 9, 2015 at 4:30
  • How are you creating this Activity? From the error code text .AddActivity it appears as though you may be calling new on this Activity and then calling onCreate() manually. You cannot call new on this type of component, it can only be created by the framework when an Intent has been sent by something else to it. Commented Feb 9, 2015 at 4:33
  • I'm a bit new at this, so please forgive me if I sound a bit lost. Commented Feb 9, 2015 at 4:38

2 Answers 2

0
for (int i = 0; i < loader.getList().size(); i++) {
        String buttonID = "btn" + i;
        int resID = AddActivity.this.getResources().getIdentifier(buttonID, "id", getPackageName());
        listofButton.add(((Button) findViewById(resID)));
        // listofButton.get(i).setText("This is a test");
    }

Write this code inside onCreate and try to run again. I hope it will work.

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

4 Comments

Yeah, I've tried this. Still the same error. Thanks a bunch for helping out though.
can I get code of ExerciseLoader class? I'll try in my system to execute the code.
knock yourself out. ExerciseLoader.java:puu.sh/fFORT/195b75a28d.png Exercise.java:puu.sh/fFOWf/a32e76ff16.png
please refer this stackoverflow.com/questions/13400318/… link to Create multiple buttons in android.
0

Try Instantiating your button in onCreate()

Like this;

    public class AddActivity extends ActionBarActivity 
{
    private ExerciseLoader loader = new ExerciseLoader();
    private ArrayList<Button> listofButton = new ArrayList<Button>();
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add);

         button=new Button(this);

        Intent intent = getIntent();
        createButtons();
        textButtons();

    }

   // Remaining coding stuff....

You probably not going to get the exception.

1 Comment

Still the same :/ Thanks a bunch for helping out

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.