-5

Have raised a Nullpointer exception which is due to my noobness.

So sorry and thanks for very much for pointing out and tolerating it.

Once again thanks.

enter image description here

InputTab.java

 public class InputTab extends Activity{

  Cursor model=null;
  EditText name=null;
  EditText food=null;
  EditText category=null;
  RadioGroup types=null;
 RestaurantHelper helper;

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

   helper = new RestaurantHelper(this);

    name=(EditText)findViewById(R.id.namex);
    food=(EditText)findViewById(R.id.foodx);
    category=(EditText)findViewById(R.id.catx);

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

    save.setOnClickListener(onSave);
}

    View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v) {

          helper.insert(name.getText().toString(),
                        food.getText().toString(),
                        category.getText().toString());
          model.requery();
        }
      };

}

ResturantHelper.java

class RestaurantHelper extends SQLiteOpenHelper {
  private static final String DATABASE_NAME="lunchlist.db";
  private static final int SCHEMA_VERSION=1;

  public RestaurantHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
      //sample spacing name away from id

      db.execSQL("CREATE TABLE gandalf " +
            "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT," +
            " food TEXT, category TEXT);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists
  }

  public Cursor getAll() {
    return(getReadableDatabase()
            .rawQuery("SELECT _id, name, food, category FROM gandalf ORDER BY name",
                      null));
  }

  public void insert(String name, String food,
                     String category) {
    ContentValues cv=new ContentValues();

    cv.put("name", name);
    cv.put("food", food);
    cv.put("category", category);

    getWritableDatabase().insert("gandalf", "name", cv);
  }

  public String getName(Cursor c) {
    return(c.getString(1));
  }

  public String getFood(Cursor c) {
    return(c.getString(2));
  }

  public String getCategory(Cursor c) {
    return(c.getString(3));
  }

}

ListViewTab.java

public class ListViewTab extends Activity{

      Cursor model=null;
      RestaurantAdapter adapter=null;
      private RestaurantHelper helper= new RestaurantHelper(this);

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listcontact);
        ListView list=(ListView)findViewById(R.id.restaurants);

        helper=new RestaurantHelper(this);

        model=helper.getAll();

        adapter=new RestaurantAdapter(model);

        list.setAdapter(adapter);
        startManagingCursor(model);
    }

        class RestaurantHolder {
            private TextView name=null;
            private TextView food=null;
            private TextView category=null;

            RestaurantHolder(View row) {
              name=(TextView)row.findViewById(R.id.namer);
              food=(TextView)row.findViewById(R.id.foodr);
              category=(TextView)row.findViewById(R.id.catr);
            }

            void populateFrom(Cursor c, RestaurantHelper helper) {
              name.setText(helper.getName(c));
              food.setText(helper.getFood(c));
              category.setText(helper.getCategory(c));

            }
          }

        class RestaurantAdapter extends CursorAdapter {
            RestaurantAdapter(Cursor c) {
              super(ListViewTab.this, c);
            }

            @Override
            public void bindView(View row, Context ctxt,
                                 Cursor c) {
              RestaurantHolder holder=(RestaurantHolder)row.getTag();

              holder.populateFrom(c, helper);
            }

            @Override
            public View newView(Context ctxt, Cursor c,
                                 ViewGroup parent) {
              LayoutInflater inflater=getLayoutInflater();
              View row=inflater.inflate(R.layout.row, parent, false);
              RestaurantHolder holder=new RestaurantHolder(row);

              row.setTag(holder);

              return(row);
            }
          }

    }

Main.java

public class Main extends TabActivity {


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

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

    TabSpec tab1 = tabHost.newTabSpec("Input");
    TabSpec tab2 = tabHost.newTabSpec("List of Food");  

    tab1.setIndicator("Food");
    tab1.setContent(new Intent(this,InputTab.class));

    tab2.setIndicator("View Order");
    tab2.setContent(new Intent(this,ListViewTab.class));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    }
}
lonelearner
  • 1,637
  • 4
  • 14
  • 22
  • 1
    Your debugger should have dropped you to this line `model.requery();`, where you would see that `model` is `null`. – Ken Y-N Jun 05 '15 at 04:38
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Ken Y-N Jun 05 '15 at 04:39
  • Initialize your `model` member. – Piyush Jun 05 '15 at 04:43

1 Answers1

1

Your class member model from class InputTab has never initialized. Therefore, you got a NPE at line model.requery();. That's why you saved your data, but right after that, you got a NPE. You don't need any Cursor in this class and, of course, you don't need to requery() anything.

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
  • Dear all, thanks for the feedback, have left out the initializing of model. Have used the cursor.requery() to refresh the listview upon clicking save. However, it does not refresh immediately and only do so when i reenter the programme, is there anything which i could do to achieve that? – lonelearner Jun 09 '15 at 09:32
  • Understand that i could do that with notifyDataSetChanged(), but not sure how to do it – lonelearner Jun 09 '15 at 10:06