-2

this is my code....
StudentLogin.java class

public class StudentLogin extends Activity implements OnClickListener, OnItemSelectedListener{


    Spinner sp1,sp2;
    EditText reg;
    Button b1,b2,b3;
    String a1,a2,studreg,studdept,studyr;
    SQLiteDatabase db1;


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


        reg=(EditText) findViewById(R.id.editText1);

        sp1=(Spinner)findViewById(R.id.spinner1);
        sp2=(Spinner)findViewById(R.id.spinner2);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub



                Cursor c=db1.rawQuery("Select * from StudReg", null);   // Im getting an error here
                int count=c.getCount();
                int flag=0;
                c.moveToFirst();
                String regno=reg.getText().toString();
                String yr=a1;
                String dept=a2;
                Toast.makeText(getApplicationContext(), regno, Toast.LENGTH_LONG).show();
                if(regno.trim().length()>0&&!yr.equals("Select  Year")&&!dept.equals("Select Dept"))
                {

                    for(int i=0;i<count;i++)
                    {
                        studreg=c.getString(c.getColumnIndex("register"));
                        studdept=c.getString(c.getColumnIndex("deptmt"));
                        studyr=c.getString(c.getColumnIndex("year"));
                        if(regno.equals(studreg)&&yr.equals(studyr)&&dept.equals(studdept))
                        {
                            flag=1;
                            break;
                        }
                        c.moveToNext();
                    }
                    c.close();
                    if(flag==1)
                    {
                        Intent inn=new Intent(StudentLogin.this,StudyMaterials.class);
                        startActivity(inn);
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Invalid User!\nKindly try again..", Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Kindly fill all the fields..", Toast.LENGTH_SHORT).show();
                }

            }
        });
        b2=(Button)findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intt=new Intent(StudentLogin.this,StudentRegister.class);
                startActivity(intt);

            }
        });
        b3=(Button)findViewById(R.id.button3);
        b3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent int1=new Intent(StudentLogin.this,Update.class);
                startActivity(int1);

            }
        });
        sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> p, View v,
                    int a, long l) {
                // TODO Auto-generated method stub
                a1=p.getItemAtPosition(a).toString();
                //Toast.makeText(getApplicationContext(), a1, Toast.LENGTH_LONG).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> p, View v,
                    int a, long l) {
                // TODO Auto-generated method stub
                a2=p.getItemAtPosition(a).toString();
                //Toast.makeText(getApplicationContext(), a2, Toast.LENGTH_LONG).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        List<String> categories = new ArrayList<String>();
        categories.add("Select Department");
        categories.add("CSE");
        categories.add("IT");
        categories.add("CIVIL");
        categories.add("MECH");
        categories.add("ECE");
        categories.add("EEE");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp2.setAdapter(dataAdapter);

        List<String> categories1 = new ArrayList<String>();
        categories1.add("Select  Year");
        categories1.add("I");
        categories1.add("II");
        categories1.add("III");
        categories1.add("IV");
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories1);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(dataAdapter1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.student_login, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemSelected(AdapterView<?> p, View v, int it,
            long l) {
        // TODO Auto-generated method stub


    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

This code validates whether the user is already registered or not.. Please help me solve this error...

Shriram
  • 4,343
  • 8
  • 37
  • 64
Anushyah
  • 1
  • 1

2 Answers2

0

you need to initialize database

 //dbHelper its a helper class for database  
 database = dbHelper.getWritableDatabase();
Madhur
  • 3,303
  • 20
  • 29
0

You have given the table name but you are not saying it on which db the table exists(as db1 is not initialized).So follow @Madhur answer to solve this null pointer exception.Also a suggestion when working with db(better to use loaders instead of working on MainThread).

srinivas
  • 72
  • 1
  • 9