0

I am facing an issue: a value is not getting transferred from one class to another.

The value is picked inside a class, but when retrieving it in another class I am getting null.

Below is the class where the value is set.

public class stockmanager extends Activity{

    String getentry=null;
    Database d=new Database(this);
    StockTable st=new StockTable();


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockmanager);

    final Button AddStock=(Button) findViewById(R.id.button1);
        final EditText entry=(EditText) findViewById(R.id.editText1);
        final Button BroDetail=(Button) findViewById(R.id.button2);

        AddStock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getentry=entry.getText().toString();
                d.db.insert(st.tablename, null,st.insert());


            }
        });
                }
}

In this class I am using the value getentry but I am getting null.

public class StockTable {

final String tablename="StockTable";
private String column1="Stock_ID";
private String column2="StockName";

stockmanager sm=new stockmanager();

final String stocktable = "CREATE TABLE " + tablename + 
            " (" + column1+ " INTEGER PRIMARY KEY , " + column2 + " TEXT) ";

public ContentValues insert(){

    ContentValues cvi=new ContentValues();
    for(int i=0;i<=sm.getentry.length();i++)
    {
        cvi.put(column1, 1);
        cvi.put(column2,sm.getentry);
                    }

    return cvi;
}

public void delete(){


}

Please help me to solve this problem

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Siva
  • 9,043
  • 12
  • 40
  • 63
  • where is db initialized? – Raghunandan Feb 02 '14 at 16:21
  • I have only pasted half code I mean only that code that is giving me the problem. It is initialized in another class but the issue here is getentry is null in stock table class – Siva Feb 02 '14 at 16:25

2 Answers2

1

You have

public class stockmanager extends Activity{

And you are instantiating a activity class

stockmanager sm=new stockmanager().

You should not instantiate a activity class. It is wrong. Instead you can pass the value to the method itself or to the constructor of other class.

You can do as

StockTable st=new StockTable();
st.insert(your params);

Then in StockTable have a insert method that takes params

public ContentValues insert(params){
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • oh thanks raghunandan for your reply.. so can you give idea what is best approach to do this as I need `getentry` in stock table class. – Siva Feb 02 '14 at 16:26
  • ok its better that we pass as params to method..thanks let me try that way – Siva Feb 02 '14 at 16:33
  • @Siva what is the problem in doing that? – Raghunandan Feb 02 '14 at 16:34
  • Actually I am new to both java and android learning both.. when I am doing in the explained I am getting null in `getentry` variable. – Siva Feb 02 '14 at 16:36
  • @Siva Follow the suggestion and try forget the rest – Raghunandan Feb 02 '14 at 16:37
  • Here one problem I can't pass params to `insert` because insert already has some params so I can't pass any new params... Is there anyway out for this – Siva Feb 02 '14 at 16:38
  • @Siva i don't see insert method taking params in stocktable. – Raghunandan Feb 02 '14 at 16:39
  • @Raghunandan.. can you please once confirm that we shouldn't create objects to activity class. right? I am new to android so asking somany questions – Siva Feb 02 '14 at 16:45
  • 1
    @Siva absolutely sure that instatiating activity class is wrong. http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class – Raghunandan Feb 02 '14 at 16:46
1

I don't know what you want to do. But constructor new stockmanager() in StockTable will not fire onCreate() method.

You need to pass your Activity to StockTable class like:

public StockTable(stockmanager yourActivity)
{
 this.st = yourActivity;
}
michal.luszczuk
  • 2,883
  • 1
  • 15
  • 22