vendredi 31 juillet 2015

Getting java.lang.NullPointerException in getView of Adapter class while running the App

I am trying to display rows from my table in SQLite database in custom ListView for that i created an ProductAdapter class . I am getting an error while running the App Following is the error

07-31 14:31:42.548    1968-1968/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.sling.freshecomart.ProductAdapter.getView(ProductAdapter.java:54)
            at android.widget.AbsListView.obtainView(AbsListView.java:2267)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
            at android.widget.ListView.onMeasure(ListView.java:1156)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

following is the line ProductAdapter.java:54

convertView = mInflater.inflate(R.layout.item,null);

ProductAdapter class is

public class ProductAdapter extends BaseAdapter {
    private List<com.sling.freshecomart.Product> mProductList;
    private LayoutInflater mInflater;
    private Activity activity;
    private LayoutInflater inflater=null;
    public ProductAdapter(List<Product> list, Activity a) {
        this.mProductList = list;
        this.activity=a;


        inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return mProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewItem item;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item,null);
            item = new ViewItem();

            item.post_id=(TextView)convertView.findViewById(R.id.post_id);
            item.Name = (TextView)convertView.findViewById(R.id.Name);
            item.Weight = (TextView)convertView.findViewById(R.id.Weight);
            item.Description = (TextView)convertView.findViewById(R.id.Description);
            item.saveValue = (TextView)convertView.findViewById(R.id.saveValue);
            item.mrpValue = (TextView)convertView.findViewById(R.id.mrpValue);
            item.Price = (TextView)convertView.findViewById(R.id.Price);

            convertView.setTag(item);
        } else {
            item = (ViewItem) convertView.getTag();
        }

        com.sling.freshecomart.Product cartProduct = mProductList.get(position);

        item.Name.setText(cartProduct.Name);
        item.Weight.setText(cartProduct.Weight);
        item.Description.setText(cartProduct.Description);
        item.mrpValue.setText(cartProduct.mrpValue);
        item.saveValue.setText(cartProduct.saveValue);
        item.Price.setText(cartProduct.Price);
        return convertView;
    }

    private class ViewItem {
        private TextView post_id,Name,Weight,Description,mrpValue,saveValue,Price;
 }
}

Product class is

public class Product {
    public String post_id,Name,Weight,Description;
    public int mrpValue,saveValue,Price;
    public Drawable productImage;
    public boolean selected;

    public Product(String Name,String Wieght,String Description,int mrpValue,int saveValue,int Price) {
        this.Name = Name;
        this.Weight=Wieght;
        this.Description = Description;
        this.mrpValue=mrpValue;
        this.saveValue=saveValue;
        this.Price = Price;
    }
}

oncreate() of The activity where the list should be displayed

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

        cartDB= new SQLiteHandler(this);
        Cursor res= cartDB.getCartDetails();

        ListView lv_cart = (ListView) findViewById(R.id.ListViewCart);

        List<Product> cartList = new ArrayList<>();
        int NoOfItemsInCart = res.getCount();

        if(res.getCount()==0){
            showMessage("Error","No rows Found");
        }else {
            res.moveToFirst();
            while (NoOfItemsInCart != 0) {

                res.getString(0);
                String CName = res.getString(1);
                String CWeight = res.getString(2);
                String CDescription = res.getString(3);
                int CmrpValue = res.getInt(4);
                int CsaveValue = res.getInt(5);
                int CPrice = res.getInt(6);
                cartList.add(new Product(CName, CWeight, CDescription, CmrpValue, CsaveValue, CPrice));
                res.moveToNext();
                NoOfItemsInCart = NoOfItemsInCart - 1;
            }
            ProductAdapter adapter = new ProductAdapter(cartList, this);
            lv_cart.setAdapter(adapter);
        }

Please help and Thanks in Advance

SQL - update main table using temp table

I have a question about SQL, especially SQLite3. I have two tables, let's name them main_table and temp_table. These tables are based on the same relational schema so they have the same columns but different rows (values).

Now what I want to do:

For each row of the main_table I want to replace it if there is a row in a temp_table with the same ID. Otherwise I want to keep the old row in the table.

I was thinking about using some joins but it does not provides the thing I want.

Would you give me an advice?

EDIT: ADITIONAL INFO:

I would like to avoid writing all columns because those tables conains tens of attributes.

how to pull data file from file explorer in eclipse

I am using Eclipse LUNA" and working on app.I have to pull my database file from data>data folder but when i click on data folder nothing happens nor any project is shown there.Using preview screen Nexus S. Kindly help me as soon as possible. I have seen many answers here but they are for mobile devices while i have to pull database file from eclipse.

while on IDataReader.Read doesn't work with yield return but foreach on reader does

This is a commonly seen ADO.NET pattern to retrieve data from database using a data reader, but strangely doesn't work.

Doesn't work:

public static IEnumerable<IDataRecord> SelectDataRecord<T>(string query, string connString)
                                                          where T : IDbConnection, new()
{
    using (var conn = new T())
    {
        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = query;
            cmd.Connection.ConnectionString = connString;

            conn.Open();
            using (var reader = (DbDataReader)cmd.ExecuteReader())
            {
                // the main part
                while (reader.Read())
                {
                    yield return (IDataRecord)reader;
                }
            }
        }
    }

This does work:

public static IEnumerable<IDataRecord> SelectDataRecord<T>(string query, string connString)
                                                          where T : IDbConnection, new()
{
    using (var conn = new T())
    {
        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = query;
            cmd.Connection.ConnectionString = connString;

            conn.Open();
            using (var reader = (DbDataReader)cmd.ExecuteReader())
            {
                // the main part
                foreach (var item in reader.Cast<IDataRecord>())
                {
                    yield return item;
                }
            }
        }
    }

The only relevant change I see is that in the first code the iterator is returned from while loop while in the second it is returned from a foreach loop.

I call it like:

var result = SelectDataRecord<SQLiteConnection>(query, connString).ToList();

I tried with SQLite .NET connector as well as MySQL connector. The result is the same, i.e. first approach fails, second one succeeds.

Exception

SQLite

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.SQLite.dll. Additional information: No current row

MySQL

An unhandled exception of type 'System.Exception' occurred in MySql.Data.dll. Additional information: No current query in data reader

Is it because of the implementation differences between reader.Read and reader.GetEnumerator in the specific ADO.NET connectors? I couldn't see any noticeable difference when I checked the source of System.Data.SQLite project, GetEnumerator calls Read internally. I am assuming ideally in both cases the yield keyword prevents eager execution of the method and the loops have to be executed only once the enumerator is enumerated externally.

Time consumption of SQLite GROYP queries, Ruby on Rails

I am a beginner in Sqlite3 and Ruby on Rails.

I would like to ask about the expected time of a GROUP query.

The truth is that I have a query with 10 conditions, retrieving data by that takes 166 ms, which is not a problem.

However, for the same query when I have the query and group by condition it takes 15358 ms or 4362 ms.

Are these number expected? Is there a way to improve the time consuming?

How do I get a Sqlite Table attribute from a Class?

I would like to get a Sqlite Table attribute from my class.

I have a method to check if a table exists from here:

var info = database.Connection.GetTableInfo(typeof(Customer).Name);
if (info.Any())
{
    //do stuff with table
}

where Customer is:

[Table("Customer")]
public class Customer
{
  //class stuff
}

Now my method will work fine as it is but I would like to link it to the Table attribute rather than the class name incase I change the table name in future.

How do I go about getting my Table attribute from my class?

Ps. I am using a PCL (Portable Class Library)

How to prepare sql statements and bind parameters?

Unfortunatelly, the documentation completely lacks examples (what is really strange), as if it assumes all its readers to be good programmers. Whereas, I'm quite new to C++ and can not really figure out from the documentation how to really prepare and execute statements. I love the way how it is implemented in PDO for PHP. Normally, I just do it like this:

$s = $db->prepare("SELECT id FROM mytable WHERE id = :id");
$s->bindParam(':id', $id);
$s->execute();

or do it using ? tokens:

 $data = array();
 $data[] = 1;
 $data[] = 2;
 $s = $db->prepare("SELECT id FROM mytable WHERE id = ? or id = ?");
 $s->execute($data);

Now, I have C++ and sqlite3.h in my hands. At this moment, I know just how to connect to database - I do it and get no errors:

sqlite3 * conn;
int rc = sqlite3_open(db_name, &conn);

Please, give some instructions (with clear tiny examples) about how to implement similar things that PDO does in PHP - prepare a statement using named arguments and using ? tokens.