2

Below is my code, just trying to display items on a list view but it is giving me null pointer exception. Any ideas? I am just combining 2 strings in the final array and the log cat is pointing to the line with the arrayAdapter at the end.

package com.example.forecastspending;

import java.text.DecimalFormat;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TabHost.TabSpec;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;  
import android.widget.TabHost;
import android.app.Activity;
import android.widget.TextView;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;

public class SavingsForecastActivity extends MainActivity {

ListView recurrenceListView;
Button add_btn;
EditText reccurenceEntry_et;
EditText reccurenceExplanation_et;

// Double[] comparisonArray = new Double[8];
int reccurenceCounter;

String[] reccurenceArray = new String[30];
String[] reccurenceExpArray = new String[30];
String[] finalDisplayArray = new String[30];

private ArrayAdapter arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forecast);

        ListView recurrenceListView = (ListView) findViewById( R.id.recurrenceListView );
        reccurenceEntry_et = (EditText) findViewById( R.id.reccurenceEntry_et );
        reccurenceExplanation_et = (EditText) findViewById( R.id.reccurenceExplanation_et );

        Button add_btn = (Button) findViewById( R.id.add_btn);

        add_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                addRecurrence();
                //calculateMovingAverage();                 
            }
        });

}

    public void addRecurrence(){

        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0         - for private mode
         Editor editor = pref.edit();

         // brings it in from activity 2
         // Double bankBalanceMainAct = Double.parseDouble(pref.getString("bankBalance","0"));

         int reccurenceCounter = pref.getInt("rCount",0);

        double reccurenceInput = Double.valueOf(reccurenceEntry_et.getText().toString());
        String rExpInput = reccurenceExplanation_et.getText().toString();

        editor.putString("rInput",reccurenceInput+"");
        editor.commit();
        String rInput = pref.getString("rInput","0");

        //adds the new cost to the reccurence array to dispaly for user
        reccurenceArray[reccurenceCounter] = rInput;
        //reccurenceExpArray[reccurenceCounter] = rExpInput;
        finalDisplayArray[reccurenceCounter] = rInput + "" +rExpInput;

        reccurenceCounter = reccurenceCounter + 1;
        editor.putInt("rCount",reccurenceCounter);
        editor.commit();

        for(int i = 0; i < finalDisplayArray.length; i++){
                finalDisplayArray[i] = "0";             
        }

        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,   finalDisplayArray);
        recurrenceListView.setAdapter(arrayAdapter);

    }

}

3
  • Post your logcat error Commented Sep 9, 2014 at 5:41
  • Declare recurrenceListView as global means outside onCreate() Commented Sep 9, 2014 at 5:45
  • Just try to remove ListView keyword before recurrenceListView in onCreate(). Commented Sep 9, 2014 at 5:47

1 Answer 1

2

Use

recurrenceListView = (ListView) findViewById( R.id.recurrenceListView );

instead of

ListView recurrenceListView = (ListView) findViewById( R.id.recurrenceListView );

because in onCreate you are new instance of ListView instead of initializing recurrenceListView which you are creating before onCreate.

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

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.