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
Sufian
  • 6,405
  • 16
  • 66
  • 120
Nych
  • 78
  • 1
  • 3
  • 10
  • some where you are calling `getResources()` method using context which is null. to find issue add full log with question – ρяσѕρєя K Feb 09 '15 at 04:28
  • 2
    possible duplicate of [NullPointerException in ContextWrapper.getResources()](http://stackoverflow.com/questions/24591637/nullpointerexception-in-contextwrapper-getresources) – PM 77-1 Feb 09 '15 at 04:29
  • @ρяσѕρєяK : In `createButtons()` OP has `int resID = getResources().getIdentifier(buttonID, "id", getPackageName());` – PM 77-1 Feb 09 '15 at 04: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. – Larry Schiefer Feb 09 '15 at 04:33
  • I'm a bit new at this, so please forgive me if I sound a bit lost. – Nych Feb 09 '15 at 04:38
  • There is 2 activities, the main activity and the addActivity. Main activity consist of one button, when that button is pressed, it opens up the addActivity. This is the main activity's code http://puu.sh/fFLv1/26c17af067.png – Nych Feb 09 '15 at 04:41
  • That code appears as though it is ok; but there's definitely something odd going on with your use of getResources(). It doesn't line up with this image. – Larry Schiefer Feb 09 '15 at 04:47
  • I'm open to suggestions if there are other ways I could retrieve the button IDs – Nych Feb 09 '15 at 04:52
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Sufian Jul 19 '16 at 07:03

2 Answers2

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.

Anand Singh
  • 5,672
  • 2
  • 23
  • 33
  • Yeah, I've tried this. Still the same error. Thanks a bunch for helping out though. – Nych Feb 09 '15 at 05:03
  • can I get code of ExerciseLoader class? I'll try in my system to execute the code. – Anand Singh Feb 09 '15 at 05:05
  • knock yourself out. ExerciseLoader.java:http://puu.sh/fFORT/195b75a28d.png Exercise.java:http://puu.sh/fFOWf/a32e76ff16.png – Nych Feb 09 '15 at 05:11
  • please refer this http://stackoverflow.com/questions/13400318/how-to-create-multiple-buttons-in-android link to Create multiple buttons in android. – Anand Singh Feb 09 '15 at 05:21
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.

A.R.
  • 2,631
  • 1
  • 19
  • 22