vendredi 11 septembre 2015

Nullpointer exception while using SQLite

I am using sqlitedatabase,and i am able to insert data properly,but issue is when i am trying to display inserted data,my app got crash and giving nullpointer exception,can any one tell the what is the issue with my code,following is my snippet code,

Error in this line

 if (c1 != null & c1.getCount() != 0) {

MAinActivity.java

public class MainActivity extends Activity {

    private ListView upcominglist;


    private ListView todays;
    private ListView eventhistory;
    private ImageView addnewevent;

    public ArrayList<ContactListItems> contactList;

    public ContactListItems contactListItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        upcominglist=(ListView)findViewById(R.id.listview_upcoming);
        todays=(ListView)findViewById(R.id.listview_todays);
        eventhistory=(ListView)findViewById(R.id.listview_eventhistory);

        addnewevent=(ImageView)findViewById(R.id.addneweventbutton);

        addnewevent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AddNewEvent.class);
                startActivity(intent);
            }
        });


       contactList = new ArrayList<ContactListItems>();
        contactList.clear();
        String query = "SELECT * FROM PHONE_CONTACTS ";
        Cursor c1 = SqlHandler.selectQuery(query);
        if (c1 != null & c1.getCount() != 0) {
            if (c1.moveToNext()) {
                do {
                    contactListItems = new ContactListItems();
                    contactListItems.setSlno(c1.getString(c1.getColumnIndex("slno")));
                    contactListItems.setNameofevent(c1.getString(c1.getColumnIndex("nameofevent")));
                    contactListItems.setDtofevent(c1.getString(c1.getColumnIndex("dtofevent")));
                    contactListItems.setTimeofevent(c1.getString(c1.getColumnIndex("timeofevent")));
                    contactListItems.setDuration(c1.getString(c1.getColumnIndex("duration")));
                    contactList.add(contactListItems);
                } while (c1.moveToNext());
            }
        }
        else
        {
            c1.close();
        }
       c1.close();

        String first=contactListItems.getSlno();
        System.out.println("First" + first);

        String second=contactListItems.getNameofevent();
        System.out.println("SEcond"+second);

        String third=contactListItems.getDtofevent();
        System.out.println("Third"+third);

        String fourth=contactListItems.getTimeofevent();
        System.out.println("Fourth"+fourth);

        String fifth=contactListItems.getDuration();
        System.out.println("Fifth"+fifth);
    }

Addnewevent.java

public class AddNewEvent extends Activity {

    private int year;
    private int month;
    private int day;
    static final int DATE_PICKER_ID = 1111;

    static final int TIME_PICKER_ID = 11111;

    int flag = 0;

    private ImageView addnewdata;
    private LinearLayout lnr;
    private Button submit;
    private EditText edtnmofevent;
    private EditText edtdtofevent;
    private EditText edttmofevent;
    private EditText edtdurationofevent;
    SqlHandler sqlHandler;

    private ImageView datepicks;
    private ImageView timepicks;


    private Calendar cal;
    private int hour;
    private int min;


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

        sqlHandler = new SqlHandler(getApplicationContext());


        addnewdata = (ImageView) findViewById(R.id.addnewdata);
        submit = (Button) findViewById(R.id.btnsubmit);
        edtnmofevent = (EditText) findViewById(R.id.edtnameofevent);
        edtdtofevent = (EditText) findViewById(R.id.edtdateofevent);
        edttmofevent = (EditText) findViewById(R.id.edttimeofevent);
        edtdurationofevent = (EditText) findViewById(R.id.edtdurationofevent);

        datepicks = (ImageView) findViewById(R.id.calndrdat);

        timepicks = (ImageView) findViewById(R.id.timepickrs);
        cal = Calendar.getInstance();
        hour = cal.get(Calendar.HOUR_OF_DAY);
        min = cal.get(Calendar.MINUTE);

        timepicks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(TIME_PICKER_ID);
            }
        });

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        StringBuilder dateValue1 = new StringBuilder().append(day).append("-").append(month + 1).append("-")
                .append(year).append(" ");

        // for Converting Correct Date format Save into Database
        SimpleDateFormat sdf123 = new SimpleDateFormat("dd-MM-yyyy");
        String abs1 = dateValue1.toString();
        Date testDate1 = null;
        try {
            try {
                testDate1 = sdf123.parse(abs1);
            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ParseException e) {

            e.printStackTrace();
        }
        SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");
        String DateFormat = formatter1.format(testDate1);

        edtdtofevent.setText(DateFormat);

        edtdtofevent.setFocusable(false);
        edtdtofevent.setInputType(InputType.TYPE_NULL);
        datepicks.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                showDialog(DATE_PICKER_ID);
            }
        });

        addnewdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater li = LayoutInflater.from(AddNewEvent.this);
                View promptsView = li.inflate(R.layout.prompts, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        AddNewEvent.this);
                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);

                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {

                                        lnr = (LinearLayout) findViewById(R.id.addnewlinear);
                                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                                LinearLayout.LayoutParams.WRAP_CONTENT);
                                        lp.setMargins(25, 0, 0, 0);
                                        TextView valueTV = new TextView(AddNewEvent.this);
                                        // valueTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                                        valueTV.setText(userInput.getText());
                                        valueTV.setLayoutParams(lp);
                                        valueTV.setTextSize(18);
                                        valueTV.setTextColor(Color.parseColor("#2d6cae"));
                                        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                LinearLayout.LayoutParams.WRAP_CONTENT);
                                        lp1.setMargins(25, 0, 25, 0);
                                        lp1.height = 50;
                                        EditText edtvalues = new EditText(AddNewEvent.this);
                                        edtvalues.setBackgroundResource(R.drawable.rect_edt);
                                        edtvalues.setLayoutParams(lp1);
                                        lnr.addView(valueTV);
                                        lnr.addView(edtvalues);
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                AlertDialog alertDialog = alertDialogBuilder.create();

                alertDialog.show();

            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddNewEvent.this, EventDetails.class);
                startActivity(intent);
                String nameofevent = edtnmofevent.getText().toString();
                String dtofevent = edtdtofevent.getText().toString();
                String timeofevent = edttmofevent.getText().toString();
                String duration = edtdurationofevent.getText().toString();
                String query = "INSERT INTO PHONE_CONTACTS(nameofevent,dtofevent,timeofevent,duration) values ('"
                        + nameofevent + "','" + dtofevent + "','" + timeofevent + "','" + duration + "')";
                sqlHandler.executeQuery(query);
                System.out.println("Querys" + query);
            }
        });
    }

Aucun commentaire:

Enregistrer un commentaire