我收到一个错误:使用游标时出现Android java.lang.NullPointerException

时间:2016-02-03 10:16:56

标签: java android

这是我的代码....
StudentLogin.java类

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

    }

}

此代码验证用户是否已注册。 请帮我解决这个错误...

2 个答案:

答案 0 :(得分:0)

您需要初始化数据库

 //dbHelper its a helper class for database  
 database = dbHelper.getWritableDatabase();

答案 1 :(得分:0)

你已经给出了表名,但是你没有说明表存在哪个db(因为db1没有被初始化)。所以请按照@Madhur的答案来解决这个空指针异常。还有一个使用db的建议(更好)使用加载器而不是在MainThread上工作。)