1

I keep getting an error for this application every time I press the menu button --> History to start the History.java class. I'm fairly certain it has to do with the Bundle method for sending the two arrays from the TipBookActivity.java class to the History.java class.

Below is the TipBookActivity code:

public class TipBookActivity extends Activity {
/** Called when the activity is first created. */

TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip,sHour;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textTip = (TextView) findViewById(R.id.tvTip);
    textHour = (TextView) findViewById(R.id.tvHour);
    textWage = (TextView) findViewById(R.id.tvWage);
    editTip = (EditText) findViewById(R.id.etTip);
    editHour = (EditText) findViewById(R.id.etHour);
    Button bSubmit = (Button) findViewById(R.id.bSubmit);
    final Bundle bTip = new Bundle();
    final Bundle bHour = new Bundle();
    final ArrayList<String> tipList = new ArrayList<String>();    
    final ArrayList<String> hourList = new ArrayList<String>();
    bSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textHour.setText(editHour.getText().toString());
            textTip.setText(editTip.getText().toString());
            wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
            String tip = String.format("$%.2f",wage);
            textWage.setText(String.valueOf(tip) + " an hour");     
            textHour.setText(editHour.getText() + " Hour(s)");
            textTip.setText("$" + editTip.getText());
            bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
            bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
            tipList.addAll(Arrays.asList(sTip));
            hourList.addAll(Arrays.asList(sHour));
            Intent i = new Intent(TipBookActivity.this,History.class);
            i.putExtras(bTip);
            i.putExtras(bHour);
        }       
    });
}

public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    MenuInflater mMain = getMenuInflater();
    mMain.inflate(R.menu.main_menu,menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.menuHistory:
         startActivity(new Intent("com.smarticle.tipbook.HISTORY"));            
         return true;
    case R.id.menuClear:
        //set up next tutorials
        Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
        display.show();
        return true;
    }
    return false;
}
}

The History class code:

public class History extends Activity{

private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip,sHour;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history_main);
    Bundle bTip = this.getIntent().getExtras();
    Bundle bHour = this.getIntent().getExtras();
    String[] array1 = bTip.getStringArray(sTip);
    String[] array2 = bHour.getStringArray(sHour);
    ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
    ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
    ArrayList<String> tipList = new ArrayList<String>();
    ArrayList<String> hourList = new ArrayList<String>();
    tipList.addAll(Arrays.asList(sTip));
    hourList.addAll(Arrays.asList(sHour));
    listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
    listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
    mainListViewTip.setAdapter(listAdapterTip);
    mainListViewHour.setAdapter(listAdapterHour);
}

}

Any help on identifying the error cause would be greatly appreciated. The code works (in theory, I think), it just won't work in practice. The general idea is to input two numbers into EditText fields, save them as strings, display them as TextViews, set them as an ArrayList, then bundle and send them to the other class to display in a ListView.

3
  • Can you please post the error which you are getting? Commented Jul 27, 2012 at 4:10
  • "FATAL EXCEPTION: main 07-27 E/AndroidRuntime(1076): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarticle.tipbook/com.smarticle.tipbook.History}: java.lang.NullPointerException Caused by: java.lang.NullPointerException It points to line 26 of History.java, where it reads String[] array1 = bTip.getStringArray(sTip); Commented Jul 27, 2012 at 4:11
  • I can, in fact, type numbers into the EditText fields and press submit. It will do the math, display the numbers in the TextView fields and continue running with no error, so this makes me believe the arrays are getting bundled and passed, just not received. Commented Jul 27, 2012 at 4:15

2 Answers 2

1

You are not initializing sTip and sHour Strings in both Activities. so initializing sTip and Shour Strings with any constant value as in both Activities:

String sTip="sTip",sHour="sHour";

and from TipBookActivity you are not passing intent to startActivity so first declare Intent i globally then start your Activity as:

   TextView textTip,textHour,textWage;
    EditText editHour,editTip;
    float wage;
    int precision = 100;
    String sTip,sHour;
    Intent i; // declare here

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
    textHour = (TextView) findViewById(R.id.tvHour);
    textWage = (TextView) findViewById(R.id.tvWage);
    editTip = (EditText) findViewById(R.id.etTip);
    editHour = (EditText) findViewById(R.id.etHour);
    Button bSubmit = (Button) findViewById(R.id.bSubmit);
    final Bundle bTip = new Bundle();
    final Bundle bHour = new Bundle();
    final ArrayList<String> tipList = new ArrayList<String>();    
    final ArrayList<String> hourList = new ArrayList<String>();
    bSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textHour.setText(editHour.getText().toString());
            textTip.setText(editTip.getText().toString());
            wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
            String tip = String.format("$%.2f",wage);
            textWage.setText(String.valueOf(tip) + " an hour");     
            textHour.setText(editHour.getText() + " Hour(s)");
            textTip.setText("$" + editTip.getText());
            bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
            bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
            tipList.addAll(Arrays.asList(sTip));
            hourList.addAll(Arrays.asList(sHour));
            i = new Intent(TipBookActivity.this,History.class);
            i.putExtras(bTip);
            i.putExtras(bHour);
        }       
    });
}
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.menuHistory:
             startActivity(i));        // start Activity here by passing intent    
             return true;
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't affect anything. Same behavior with same error post as the other times.
@Adam : use startActivity(i); instead of startActivity(new Intent("com.smarticle.tipbook.HISTORY")); and you are no passing intent to startActivity so declare Intent i; Globally means after String sTip,sHour; line in your code
Didn't change anything about behavior. It's erroring at the "String[] array1 = bundle.getStrignArray(sTip);" line everytime it's run and the menu --> History button is pressed.
0

Approach you are following is completely wrong, you can put one Bundle at a time to the intent, and you are putting two bundles, sTip, and sHours.

Second Bundle sHours will override the first one, and I think its main cause of the null pointer exception, instead you should put all the values(in your case two String Arrays) to a single bundle. and put that bundle to the Intent.

Do as Follows:

public class TipBookActivity extends Activity {
/** Called when the activity is first created. */

TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip="sTip";
String sHour="sHour";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textTip = (TextView) findViewById(R.id.tvTip);
    textHour = (TextView) findViewById(R.id.tvHour);
    textWage = (TextView) findViewById(R.id.tvWage);
    editTip = (EditText) findViewById(R.id.etTip);
    editHour = (EditText) findViewById(R.id.etHour);
    Button bSubmit = (Button) findViewById(R.id.bSubmit);
    final Bundle bundle= new Bundle();
     final ArrayList<String> tipList = new ArrayList<String>();    
    final ArrayList<String> hourList = new ArrayList<String>();
    bSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textHour.setText(editHour.getText().toString());
            textTip.setText(editTip.getText().toString());
            wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
            String tip = String.format("$%.2f",wage);
            textWage.setText(String.valueOf(tip) + " an hour");     
            textHour.setText(editHour.getText() + " Hour(s)");
            textTip.setText("$" + editTip.getText());
            bundle.putStringArray(sTip,new String[] {editTip.getText().toString()});
            bundle.putStringArray(sHour,new String[] {editHour.getText().toString()});
            tipList.addAll(Arrays.asList(sTip));
            hourList.addAll(Arrays.asList(sHour));
            Intent i = new Intent(TipBookActivity.this,History.class);
            i.putExtras(bundle);

        }       
    });
}

public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    MenuInflater mMain = getMenuInflater();
    mMain.inflate(R.menu.main_menu,menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.menuHistory:
         startActivity(new Intent("com.smarticle.tipbook.HISTORY"));            
         return true;
    case R.id.menuClear:
        //set up next tutorials
        Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
        display.show();
        return true;
    }
    return false;
}
}

and in History Activity:

public class History extends Activity{

private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip="sTip";
String sHour="sHour";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_main);
        Bundle bundle= this.getIntent().getExtras();
        String[] array1 = bundle.getStringArray(sTip);
        String[] array2 = bundle.getStringArray(sHour);
        ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
        ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
        ArrayList<String> tipList = new ArrayList<String>();
        ArrayList<String> hourList = new ArrayList<String>();
        tipList.addAll(Arrays.asList(sTip));
        hourList.addAll(Arrays.asList(sHour));
        listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
        listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
        mainListViewTip.setAdapter(listAdapterTip);
        mainListViewHour.setAdapter(listAdapterHour);
    }

    }

2 Comments

Didn't change anything about behavior. It's erroring at the "String[] array1 = bundle.getStrignArray(sTip);" line everytime it's run and the menu --> History button is pressed.
"FATAL EXCEPTION: main E/AndroidRuntime(1336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarticle.tipbook/com.smarticle.tipbook.History}: java.lang.NullPointerException Caused by: java.lang.NullPointerException at String[] array1 = bundle.getStringArray(sTip);"

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.