2013-06-24 29 views
0

這是databaseAdapter代碼從數據庫獲取值:獲取「NULL POINTER EXCEPTION」從android中的數據庫中檢索值?

public ArrayList<TaskClass> getAllTaskCategoryWise(String categoryName) 
{ 
ArrayList<TaskClass> getTaskListofSelectedCategory = new ArrayList<TaskClass>(); 
Log.d("query print:",""); 
Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name_Task + " WHERE " + KEY_TASKCATEGORY + " = '"+ categoryName.trim()+ "'"+"" + " ORDER BY " + KEY_TASKDATE +"" + " ASC",null); 
//Cursor cursor = db.rawQuery("SELECT * FROM " + tb_Name_Task,null); 
if(cursor!= null) 
{ 
    Log.d("cursor info:","cursor values:"+cursor); 
    cursor.moveToFirst(); 
    while(!cursor.isAfterLast()) 
     { 
     TaskClass taskClassInstance1 = new TaskClass(); 
     taskClassInstance1.setTask_id(cursor.getInt(0)); 
     taskClassInstance1.setTitle(cursor.getString(1)); 
     taskClassInstance1.setDescription(cursor.getString(2)); 
     taskClassInstance1.setTaskCategory(cursor.getString(3)); 
     taskClassInstance1.setTaskDate(cursor.getString(4)); 
     taskClassInstance1.setStatus(cursor.getInt(5)); 
    // Adding task class instance to list 
     getTaskListofSelectedCategory.add(taskClassInstance1); 
     Log.d("task list:","values:+ taskClassInstance.setTitle(cursor.getString(1))"); 
     cursor.moveToNext(); 
    } 
} 
else 
{ 
    Log.d("values of getAllTaskCatgeory","cursor null"); 
} 

Log.d("retrieving all task for selected category","getAllTaskCategoryWise function"+ cursor); 
cursor.close(); 
return getTaskListofSelectedCategory; 
} 

調用一個函數在這個函數及其返回我空指針異常和IM也使用自定義arrayadapter:custom array adapter code

public void getAllTaskForSelectedCategory(String categorySelection) 
{ 
    Log.d("message for function call","getAllTaskForSelectedCategory function called:"+categorySelection); 
    try 
    {dataBaseAdapterInstance1 = dataBaseAdapterInstance1.openToRead(); 

    taskClassListinstance = dataBaseAdapterInstance1.getAllTaskCategoryWise(categorySelection); 
    Log.d("LISTVALUES OF TASK TABLE:","VALUES:" + taskClassListinstance); 


    listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClassListinstance)); 
    new CustomListAdapter(this,taskClassListinstance).notifyDataSetChanged(); 
    listViewInstance.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent,View view,int position,long id) 
     { 
     TaskClass classinstance = (TaskClass) taskClassListinstance.get(position); 
     Toast.makeText(getApplicationContext(),"values:" + classinstance,Toast.LENGTH_SHORT).show(); 
     Toast.makeText(getApplicationContext(),"id values:"+classinstance.getTask_id(),Toast.LENGTH_SHORT).show(); 

     } 

    }); 
    dataBaseAdapterInstance1.close(); 
    } 
    catch (Exception e) { 
     // TODO: handle exception 
     Log.d("exception in getAllTaskForSelectedCategory","error"); 
     e.printStackTrace(); 
    } 
} 

這「taskClassListinstance = dataBaseAdapterInstance1.getAllTask​​CategoryWise(categorySelection);」返回null,這裏是logcat:

06-01 16:39:11.375: E/AndroidRuntime(14691): FATAL EXCEPTION: main 
06-01 16:39:11.375: E/AndroidRuntime(14691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolist/com.example.todolist.TaskListCategoryWise}: java.lang.NullPointerException 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.os.Looper.loop(Looper.java:137) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.main(ActivityThread.java:5041) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at java.lang.reflect.Method.invokeNative(Native Method) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at java.lang.reflect.Method.invoke(Method.java:511) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at dalvik.system.NativeStart.main(Native Method) 
06-01 16:39:11.375: E/AndroidRuntime(14691): Caused by: java.lang.NullPointerException 
06-01 16:39:11.375: E/AndroidRuntime(14691): at com.example.todolist.TaskListCategoryWise.onCreate(TaskListCategoryWise.java:80) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.Activity.performCreate(Activity.java:5104) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
06-01 16:39:11.375: E/AndroidRuntime(14691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
06-01 16:39:11.375: E/AndroidRuntime(14691): ... 11 more 

taskListCategoryWise.java全碼:

public class TaskListCategoryWise extends Activity 
{ String selectedValueofList; 
    DatabaseAdapter dataBaseAdapterInstance1; 
    ListView listViewInstance; 
    CustomListAdapter instanceCustomAdapter; 
    ArrayList<TaskClass> taskClassListinstance; 
    CheckBox checkBoxInstance; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 

    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tasklist_categorywise); 
     dataBaseAdapterInstance1 = new DatabaseAdapter(this); 
     Button refreshTaskButton = (Button)findViewById(R.id.refreshBtnForTask); 
     listViewInstance = (ListView)findViewById(R.id.listViewForTask); 
     checkBoxInstance = (CheckBox)findViewById(R.id.checkboxForStatus); 

     try 
     { 
     Intent intent1 = getIntent(); 
     Log.d("value of intent:","intent values"+intent1); 
     selectedValueofList = intent1.getExtras().getString("selectedItem"); 
     Log.d("retrieving values of selected listItem:","selected value of listitem:"+selectedValueofList); 


     } 
     catch (Exception e) { 
      // TODO: handle exception 
      Log.d("esception of taskLISt","Exception in retrieving vlaues of previous activity"); 
      e.printStackTrace(); 
     } 
     getAllTaskForSelectedCategory(selectedValueofList); 

     refreshTaskButton.setOnClickListener(new View.OnClickListener() { 

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

     Button addTaskButton = (Button) findViewById(R.id.addBtnForTask); 
     addTaskButton.setOnClickListener(new View.OnClickListener() { 

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

       Intent openTaskList = new Intent(TaskListCategoryWise.this,TaskList.class); 
       openTaskList.putExtra("ListitemValues",selectedValueofList); 
       startActivityForResult(openTaskList,0); 

      } 
     }); 


     checkBoxInstance.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if ((checkBoxInstance).isChecked()) { 
        Toast.makeText(getApplicationContext(), 
         "Bro, try Android :)", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
     } 
    public void getAllTaskForSelectedCategory(String categorySelection) 
    { 
     Log.d("message for function call","getAllTaskForSelectedCategory function called:"+categorySelection); 
     try 
     {dataBaseAdapterInstance1 = dataBaseAdapterInstance1.openToRead(); 

     taskClassListinstance = dataBaseAdapterInstance1.getAllTaskCategoryWise(categorySelection); 
     Log.d("LISTVALUES OF TASK TABLE:","VALUES:" + taskClassListinstance); 


     listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClassListinstance)); 
     new CustomListAdapter(this,taskClassListinstance).notifyDataSetChanged(); 
     listViewInstance.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent,View view,int position,long id) 
      { 
      TaskClass classinstance = (TaskClass) taskClassListinstance.get(position); 
      Toast.makeText(getApplicationContext(),"values:" + classinstance,Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getApplicationContext(),"id values:"+classinstance.getTask_id(),Toast.LENGTH_SHORT).show();} 

     }); 
     dataBaseAdapterInstance1.close(); 
    } 
     catch (Exception e) { 
      // TODO: handle exception 
      Log.d("exception in getAllTaskForSelectedCategory","error"); 
      e.printStackTrace(); 
     } 
    } 



} 
+0

什麼是你的'TaskListCategoryWise.java:80'線? –

+0

checkBoxInstance.setOnClickListener(新View.OnClickListener(){ \t \t \t \t \t \t @Override \t \t \t公共無效的onClick(視圖v){ \t \t \t \t // TODO自動生成方法存根 \t \t ((checkBoxInstance).isChecked())\t \t { \t \t \t \t \t Toast.makeText(getApplicationContext() \t \t \t \t \t「Bro,try Android :)」Toast.LENGTH_LONG) \t \t \t \t} \t \t \t} \t \t}); – nida

+0

是checkBoxInstance之前初始化的嗎? –

回答

0

你必須初始化checkBoxInstance

checkBoxInstance = (CheckBox) findViewById(R.id.your-checkbox-id-from-xml) 

,並在XML

<CheckBox 
    android:id = "@+id/id-here" 
    . other properties here 
    . 
    . 
/> 
+0

已經出現在我的代碼中:checkBoxInstance =(CheckBox)findViewById(R.id.checkboxForStatus); – nida

+0

可以發佈你的完整TaskListCategoryWise.java代碼 –

+0

請參閱編輯的代碼 – nida